/** Grant all permissions for the role names on this application */ protected void grantAppRoles( UsergridAuthorizationInfo info, EntityManager em, UUID applicationId, TokenInfo token, Set<String> rolenames ) throws Exception { Map<String, Role> app_roles = em.getRolesWithTitles( rolenames ); for ( String rolename : rolenames ) { if ( ( app_roles != null ) && ( token != null ) ) { Role role = app_roles.get( rolename ); if ( ( role != null ) && ( role.getInactivity() > 0 ) && ( token.getInactive() > role .getInactivity() ) ) { continue; } } Set<String> permissions = em.getRolePermissions( rolename ); grant( info, applicationId, permissions ); role( info, "application-role:".concat( applicationId.toString() ).concat( ":" ).concat( rolename ) ); } } }
@Override public Map<String, Role> getRolesWithTitles( Set<String> roleNames ) throws Exception { Map<String, Role> rolesWithTitles = new HashMap<String, Role>(); Map<String, Object> nameResults = null; if ( roleNames != null ) { nameResults = getDictionaryElementValues( getApplicationRef(), DICTIONARY_ROLENAMES, roleNames.toArray( new String[roleNames.size()] ) ); } else { nameResults = cast( getDictionaryAsMap( getApplicationRef(), DICTIONARY_ROLENAMES ) ); roleNames = nameResults.keySet(); } Map<String, Object> timeResults = getDictionaryElementValues( getApplicationRef(), DICTIONARY_ROLETIMES, roleNames.toArray( new String[roleNames.size()] ) ); for ( String roleName : roleNames ) { String savedTitle = string( nameResults.get( roleName ) ); // no title, skip the role if ( savedTitle == null ) { continue; } Role newRole = new Role(); newRole.setName( roleName ); newRole.setTitle( savedTitle ); newRole.setInactivity( getLong( timeResults.get( roleName ) ) ); rolesWithTitles.put( roleName, newRole ); } return rolesWithTitles; }
@Override public ServiceResults deleteItemById( ServiceContext context, UUID id ) throws Exception { Role role = em.get( context.getOwner(), Role.class ); ServiceResults results = getItemById( context, id ); if ( !results.isEmpty() ) { em.removeUserFromRole( id, role.getRoleName() ); } return results; }
assertEquals( name1, existing.getName() ); assertEquals( title1, existing.getTitle() ); assertEquals( inactivity1, existing.getInactivity().longValue() ); assertEquals( name2, existing.getName() ); assertEquals( title2, existing.getTitle() ); assertEquals( inactivity2, existing.getInactivity().longValue() );
/** * Create the role with the action and info and test it's created successfully * * @param action the action to take */ private void createAndTestRoles( ServiceAction action, String roleName, String roleTitle, long inactivity ) throws Exception { app.put( "name", roleName ); app.put( "title", roleTitle ); app.put( "inactivity", inactivity ); // test creating a new role ServiceResults results = app.testRequest( action, 1, "roles" ); // check the results Entity roleEntity = results.getEntities().get( 0 ); assertEquals( roleName, roleEntity.getProperty( "name" ) ); assertEquals( roleTitle, roleEntity.getProperty( "title" ) ); assertEquals( inactivity, roleEntity.getProperty( "inactivity" ) ); // check the role is correct at the application level Map<String, Role> roles = app.getRolesWithTitles( Collections.singleton( roleName ) ); Role role = roles.get( roleName ); assertNotNull( role ); assertEquals( roleName, role.getName() ); assertEquals( roleTitle, role.getTitle() ); assertEquals( inactivity, role.getInactivity().longValue() ); }
@Override public ServiceResults deleteItemByName( ServiceContext context, String name ) throws Exception { Role role = em.get( context.getOwner(), Role.class ); ServiceResults results = getItemByName( context, name ); if ( !results.isEmpty() ) { em.removeUserFromRole( results.getId(), role.getRoleName() ); } return results; } }
@Override public ServiceResults postItemByName( ServiceContext context, String name ) throws Exception { Role role = em.get( context.getOwner(), Role.class ); Entity entity = sm.getService( "/users" ).getEntity( context.getRequest(), name ); if ( entity != null ) { em.addUserToRole( entity.getUuid(), role.getRoleName() ); } return new ServiceResults( this, context, Type.COLLECTION, Results.fromRef( entity ), null, null ); }
@Override public ServiceResults postItemById( ServiceContext context, UUID id ) throws Exception { Role role = em.get( context.getOwner(), Role.class ); Entity entity = sm.getService( "/users" ).getEntity( context.getRequest(), id ); if ( entity != null ) { em.addUserToRole( entity.getUuid(), role.getRoleName() ); } return new ServiceResults( this, context, Type.COLLECTION, Results.fromRef( entity ), null, null ); }