Skip to content

User Initializer

Squizy provides a mechanism to bootstrap initial users when the application starts. This is useful for creating an admin account or seeding default users for the first deployment.

Production use

While this mechanism can be used in production for initial bootstrap, prefer dedicated database migration tools (Flyway, Liquibase) for managing user data long-term.

SquizyGeneratedUsersDetailsProvider

Implement the SquizyGeneratedUsersDetailsProvider interface and register it as a Spring bean to define which users should be created at startup:

java
@Component
public class InitialUsersProvider implements SquizyGeneratedUsersDetailsProvider {

    @Override
    public List<GeneratedUserDetails> getInitializationUsersDetails() {
        return List.of(
                new GeneratedUserDetails("Admin", "[email protected]", "Admin"),
                new GeneratedUserDetails("Demo User", "[email protected]", "User")
        );
    }
}

GeneratedUserDetails

Each GeneratedUserDetails specifies:

ParameterDescription
nameDisplay name of the user
emailEmail address (used as unique identifier)
rolesOne or more role names to assign (must already exist)

How It Works

  1. On application startup, Squizy checks for a SquizyGeneratedUsersDetailsProvider bean
  2. If present, it calls getInitializationUsersDetails() to retrieve the list of users to create
  3. For each user:
    • If the user does not exist (by email), it is created with the specified name and roles
    • If the user already exists, its name and roles are updated to match the provider (roles are additive — existing roles are preserved)
  4. The referenced roles must already exist in the database (created by the permissions initializer)

WARNING

If a role specified in GeneratedUserDetails does not exist, the application will fail to start with an IllegalArgumentException.

Auto-Assigning Roles on First Login

SquizyGeneratedUsersDetailsProvider also supports auto-assigning roles to users who log in for the first time via OAuth2:

java
@Component
public class InitialUsersProvider implements SquizyGeneratedUsersDetailsProvider {

    @Override
    public List<GeneratedUserDetails> getInitializationUsersDetails() {
        return List.of(
                new GeneratedUserDetails("Admin", "[email protected]", "Admin")
        );
    }

    @Override
    public List<String> getOnDemandNewUserRoles(String email) {
        // Assign User role to every new user on first login
        return List.of("User");
    }
}

The getOnDemandNewUserRoles method is called when a new user authenticates via OAuth2 for the first time. Return an empty list (the default) to create users with no roles.