/** * Indicates whether this user credentials has at least one authority through * its user authority groups. */ public boolean hasAuthorities() { for ( UserAuthorityGroup group : userAuthorityGroups ) { if ( group != null && group.getAuthorities() != null && !group.getAuthorities().isEmpty() ) { return true; } } return false; }
/** * Returns a set of the aggregated authorities for all user authority groups * of this user credentials. */ public Set<String> getAllAuthorities() { Set<String> authorities = new HashSet<>(); for ( UserAuthorityGroup group : userAuthorityGroups ) { authorities.addAll( group.getAuthorities() ); } return authorities; }
public static Collection<GrantedAuthority> getGrantedAuthorities( UserCredentials credentials ) { Set<GrantedAuthority> authorities = new HashSet<>(); for ( UserAuthorityGroup group : credentials.getUserAuthorityGroups() ) { for ( String authority : group.getAuthorities() ) { authorities.add( new SimpleGrantedAuthority( authority ) ); } } return authorities; } }
/** * Indicates whether this user credentials can issue the given user authority * group. First the given authority group must not be null. Second this * user credentials must not contain the given authority group. Third * the authority group must be a subset of the aggregated user authorities * of this user credentials, or this user credentials must have the ALL * authority. * * @param group the user authority group. * @param canGrantOwnUserAuthorityGroups indicates whether this users can grant * its own authority groups to others. */ public boolean canIssueUserRole( UserAuthorityGroup group, boolean canGrantOwnUserAuthorityGroups ) { if ( group == null ) { return false; } final Set<String> authorities = getAllAuthorities(); if ( authorities.contains( UserAuthorityGroup.AUTHORITY_ALL ) ) { return true; } if ( !canGrantOwnUserAuthorityGroups && userAuthorityGroups.contains( group ) ) { return false; } return authorities.containsAll( group.getAuthorities() ); }
public static UserAuthorityGroup createUserAuthorityGroup( char uniqueCharacter, String... auths ) { UserAuthorityGroup role = new UserAuthorityGroup(); role.setAutoFields(); role.setUid( BASE_UID + uniqueCharacter ); role.setName( "UserAuthorityGroup" + uniqueCharacter ); for ( String auth : auths ) { role.getAuthorities().add( auth ); } return role; }
userAuthorityGroup.getAuthorities().addAll( authorities );
public MockCurrentUserService( boolean superUserFlag, Set<OrganisationUnit> organisationUnits, Set<OrganisationUnit> dataViewOrganisationUnits, String... auths ) { UserAuthorityGroup userRole = new UserAuthorityGroup(); userRole.setAutoFields(); userRole.getAuthorities().addAll( Arrays.asList( auths ) ); this.superUserFlag = superUserFlag; UserCredentials credentials = new UserCredentials(); credentials.setUsername( "currentUser" ); credentials.getUserAuthorityGroups().add( userRole ); credentials.setAutoFields(); User user = new User(); user.setFirstName( "Current" ); user.setSurname( "User" ); user.setOrganisationUnits( organisationUnits ); user.setDataViewOrganisationUnits( dataViewOrganisationUnits ); user.setUserCredentials( credentials ); user.setAutoFields(); credentials.setUserInfo( user ); this.currentUser = user; }