User assigned roles list

Hello, how I can get list of assigned roles to user( In code ) and then check if user have specific role providing role name or code?

Hi,
The entity that stores user assignments is io.jmix.securitydata.entity.RoleAssignmentEntity.

  • username - username of the User
  • roleCode - code of the role (either stored in the database or defined in code).
  • roleType - either io.jmix.security.role.assignment.RoleAssignmentRoleType#ROW_LEVEL or io.jmix.security.role.assignment.RoleAssignmentRoleType#RESOURCE

You can query this entity with DataManager.

2 Likes

In case you are interested in roles of the currently authenticated user, use the CurrentAuthentication bean as follows:

@Autowired
private CurrentAuthentication currentAuthentication;

public boolean hasFullAccessRole() {
    return currentAuthentication.getAuthentication().getAuthorities().stream()
            .anyMatch(grantedAuthority -> 
                  grantedAuthority.getAuthority().equals("system-full-access"));
}
3 Likes