0
votes

I have created 2 custom roles for my project. I want to assign a Role to the users during the registration process. How can I assign a role by default to the user during the registration.

Also, how can I create 2 registration forms, if I have 2 different roles. I need 2 types of registrations for different types of roles.

1

1 Answers

1
votes

In the save method of your UserServicePlugin implementation, you can add roles when you create the user. For example, assuming you have a model called User that implements Deadbolt's Subject interface,

@Override
public Object save(final AuthUser authUser)
{
    final boolean isExistingUser = User.existsByAuthUserIdentity(authUser);
    Long id = null;
    if (!isExistingUser)
    {
        User user = User.create(authUser);
        id = user.id;

        user.giveRole("some role name");
    }
    return id;
}

The giveRole method of is something like this.

public void giveRole(String roleName)
{
    SecurityRole role = SecurityRole.findByRoleName(roleName);
    if (!roles.contains(role))
    {
        roles = new ArrayList<>(roles);
        roles.add(role);
        update();
        Ebean.saveAssociation(this, "roles");
    }
}

This assumes you're using Ebean and Java, but the mechanics are the same if you're using Scala or a different database tool.

For having different roles assigned on user creation, move the bit where you assign roles out of the save method and into (for example) the controller methods through which these registrations come.