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.