0

I started with the tutorial site play-authenticate-usage. I Have play 2.1, deadbolt 2.1 and play authenticate 1.0

Unfortunately I am a JDBC guy and new to JPA and annotations.

Everything works fine, I understand how to use the @Restrict for roles. What I don't see is how to programmatically assign a user an additional role. I expected to see a function in the User class, something along the line of User.addRole( String role )

I tried the followingin class User but it didn't work (no errors, just didn't update tables)...

Change public List<? extends Role> to public List<SecurityRole>

public void addRole( String roleName ) 
{
    SecurityRole grRole = SecurityRole.findByRoleName( Application.NEW_ROLE );

    this.getRoles().add( grRole );
    this.save();
    this.saveManyToManyAssociations("roles");
}

thanks, Chet

Chet
  • 96
  • 8

1 Answers1

0

This seems to work, modifications are to model/auth/User...

Change public List<? extends Role> to public List<SecurityRole>

public void addRole( String roleName )
{
    SecurityRole newRole = SecurityRole.findByRoleName( roleName );
    if ( newRole == null )
    {
        newRole = new SecurityRole();
        newRole.roleName = roleName;
        newRole.save();
    }

    if ( ! this.getRoles().contains( newRole ) )
    {
        this.getRoles().add( newRole );
        this.save();
        this.saveManyToManyAssociations("roles");
    }
} 
Chet
  • 96
  • 8