/** side effect: converts headEntity into an Entity if it is an EntityRef! */ private Entity getHeadEntity() throws Exception { Entity entity = null; if ( headEntity instanceof Entity ) { entity = ( Entity ) headEntity; } else { entity = em.get( headEntity ); headEntity = entity; } return entity; }
@Override public User getAppUserByIdentifier( UUID applicationId, Identifier identifier ) throws Exception { EntityManager em = emf.getEntityManager(applicationId); return em.get( em.getUserByIdentifier( identifier ), User.class ); }
private Set<CredentialsInfo> readUserPasswordHistory( UUID appId, UUID ownerId ) throws Exception { EntityManager em = emf.getEntityManager( appId ); Entity owner = em.get( new SimpleEntityRef("user", ownerId )); @SuppressWarnings("unchecked") Set<CredentialsInfo> credInfo = (Set<CredentialsInfo>)em.getDictionaryElementValue(owner, DICTIONARY_CREDENTIALS, USER_PASSWORD_HISTORY); return credInfo; }
public ServiceResults getApplicationEntity( ServiceContext context ) throws Exception { checkPermissionsForPath( context, "/" ); Entity entity = em.get( em.getApplicationRef() ); Results r = Results.fromEntity( entity ); Map<String, Object> collections = em.getApplicationCollectionMetadata(); // Set<String> collections = em.getApplicationCollections(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
@Override public ServiceResults deleteItemById( ServiceContext context, UUID id ) throws Exception { User user = em.get( context.getOwner(), User.class ); ServiceResults results = getItemById( context, id ); if ( !results.isEmpty() ) { em.removeUserFromRole( user.getUuid(), results.getEntity().getName() ); ScopedCache scopedCache = cacheFactory.getScopedCache(new CacheScope(em.getApplication().asId())); scopedCache.invalidate(); localShiroCache.invalidateAll(); } return results; }
@Override public OrganizationInfo getOrganizationByUuid( UUID id ) throws Exception { EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Entity entity = em.get( new SimpleEntityRef( Group.ENTITY_TYPE, id ) ); if ( entity == null ) { return null; } Map<Object, Object> properties = em.getDictionaryAsMap( entity, ORGANIZATION_PROPERTIES_DICTIONARY ); OrganizationInfo orgInfo = new OrganizationInfo( entity.getProperties() ); orgInfo.setProperties( properties ); return orgInfo; }
@Override public void setAppUserCredentialsInfo( final UUID applicationId, final UUID userId, final CredentialsInfo credentialsInfo ) throws Exception { Preconditions.checkNotNull( applicationId, "applicationId is required" ); Preconditions.checkNotNull( userId, "userId is required" ); Preconditions.checkNotNull( credentialsInfo, "credentialsInfo is required" ); final User user = emf.getEntityManager( applicationId ).get(userId, User.class); if(user == null){ throw new EntityNotFoundException( "User with id " + userId + " cannot be found" ); } writeUserPassword(applicationId, user, credentialsInfo); }
@Override public ServiceResults postItemByName( ServiceContext context, String name ) throws Exception { User user = em.get( context.getOwner(), User.class ); Entity entity = sm.getService( "/roles" ).getEntity( context.getRequest(), name ); if ( entity != null ) { em.addUserToRole( user.getUuid(), entity.getName() ); ScopedCache scopedCache = cacheFactory.getScopedCache(new CacheScope(em.getApplication().asId())); scopedCache.invalidate(); localShiroCache.invalidateAll(); } return new ServiceResults( this, context, Type.COLLECTION, Results.fromRef( entity ), null, null ); }
@Override public void setOrganizationProps( UUID orgId, Map<String, Object> props ) throws Exception { EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Group org = em.get( orgId, Group.class ); if ( org == null ) { throw new EntityNotFoundException( String.format( "Could not find organization with id %s", orgId.toString() ) ); } org.setProperties( props ); em.update(org); }
/** * Returns the Import Entity that stores all meta-data for the particular import Job * * @param jobExecution the import job details * @return Import Entity */ @Override public Import getImportEntity(final JobExecution jobExecution) throws Exception { UUID importId = (UUID) jobExecution.getJobData().getProperty(IMPORT_ID); EntityManager importManager = emf.getEntityManager(emf.getManagementAppId()); return importManager.get(importId, Import.class); }
@Override public ApplicationInfo getApplicationInfo( UUID applicationId ) throws Exception { if ( applicationId == null ) { return null; } EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Entity entity = em.get(new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationId)); if ( entity != null ) { return new ApplicationInfo( applicationId, entity.getName() ); } return null; }
@Override public ApplicationInfo getDeletedApplicationInfo(UUID applicationId) throws Exception { if ( applicationId == null ) { return null; } EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); final Entity entity = em.get( new SimpleEntityRef( CpNamingUtils.DELETED_APPLICATION_INFO, applicationId ) ); if ( entity != null ) { return new ApplicationInfo( applicationId, entity.getName() ); } return null; }
@Override public boolean verifyAdminUserPassword( UUID userId, String password ) throws Exception { if ( ( userId == null ) || ( password == null ) ) { return false; } User user = emf.getEntityManager( smf.getManagementAppId() ).get( userId, User.class ); return verify( smf.getManagementAppId(), user.getUuid(), password ); }
@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; }
@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 void setAdminUserPassword( UUID userId, String oldPassword, String newPassword ) throws Exception { if ( ( userId == null ) || ( oldPassword == null ) || ( newPassword == null ) ) { return; } User user = emf.getEntityManager( smf.getManagementAppId() ).get( userId, User.class ); if ( !verify( smf.getManagementAppId(), user.getUuid(), oldPassword ) ) { throw new IncorrectPasswordException( "Old password does not match" ); } setAdminUserPassword(userId, newPassword); }
public Entity getEntityFromPrincipal( AuthPrincipalInfo principal ) throws Exception { EntityManager em = emf.getEntityManager( principal.getApplicationId() != null ? principal.getApplicationId() : smf.getManagementAppId() ); return em.get(new SimpleEntityRef( principal.getType().getEntityType(), principal.getUuid())); }
private Entity getApplicationInfo( UUID appId ) throws Exception { Map<String, UUID> apps = setup.getEmf().getApplications(); if(apps.containsValue(appId)){ return setup.getEmf().getManagementEntityManager().get(appId); }else{ fail("no app " + appId); return null; } } }
@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 ); }
private User createUser() throws Exception { String rando = RandomStringUtils.randomAlphanumeric( 10 ); String username = "user_" + rando; String email = username + "@example.com"; Map<String, Object> properties = new HashMap<String, Object>() {{ put( "username", username ); put( "email", email ); }}; EntityManager em = setup.getEmf().getEntityManager( setup.getEmf().getManagementAppId() ); Entity entity = em.create( "user", properties ); return em.get( new SimpleEntityRef( User.ENTITY_TYPE, entity.getUuid() ), User.class ); }