/** @deprecated for legacy use */ private static void addLegacyMetadata( Entity entity, Map<String, Object> metadata ) { if ( entity.getProperty( CONTENT_TYPE ) != null ) { metadata.put( CONTENT_TYPE, entity.getProperty( CONTENT_TYPE ) ); } if ( entity.getProperty( CONTENT_LENGTH ) != null ) { metadata.put( CONTENT_LENGTH, entity.getProperty( CONTENT_LENGTH ) ); } if ( entity.getProperty( CONTENT_DISPOSITION ) != null ) { metadata.put( CONTENT_DISPOSITION, entity.getProperty( CONTENT_DISPOSITION ) ); } if ( entity.getProperty( E_TAG ) != null ) { metadata.put( E_TAG, entity.getProperty( E_TAG ) ); } if ( entity.getProperty( CHECKSUM ) != null ) { metadata.put( CHECKSUM, entity.getProperty( CHECKSUM ) ); } }
public static boolean isUserActivated( Entity user ) { if ( user == null ) { return false; } return getBoolean( user.getProperty( PROPERTY_ACTIVATED ) ); }
public static boolean isUserDisabled( Entity user ) { if ( user == null ) { return false; } return getBoolean( user.getProperty( PROPERTY_DISABLED ) ); } }
@Override public Object getProperty( EntityRef entityRef, String propertyName ) throws Exception { Entity entity = get( entityRef ); return entity.getProperty( propertyName ); }
public UserInfo getUserInfo( UUID applicationId, Entity entity ) { if ( entity == null ) { return null; } return new UserInfo( applicationId, entity.getUuid(), ( String ) entity.getProperty( "username" ), entity.getName(), ( String ) entity.getProperty( "email" ), ConversionUtils.getBoolean( entity.getProperty( "confirmed" ) ), ConversionUtils.getBoolean( entity.getProperty( "activated" ) ), ConversionUtils.getBoolean( entity.getProperty( "disabled" ) ), entity.getDynamicProperties(), ConversionUtils.getBoolean( entity.getProperty( "admin" ))); }
@Override public int compare( Entity o1, Entity o2 ) { boolean o1Boolean = ( Boolean ) o1.getProperty( "boolean" ); boolean o2Boolean = ( Boolean ) o2.getProperty( "boolean" ); if ( o1Boolean != o2Boolean ) { if ( o1Boolean ) { return -1; } return 1; } int o1Index = ( Integer ) o1.getProperty( "index" ); int o2Index = ( Integer ) o2.getProperty( "index" ); if ( o1Index > o2Index ) { return 1; } else if ( o2Index > o1Index ) { return -1; } return 0; } } );
/** Invoked when our actor is a map */ private void handleDynamicPayload( Map<String, String> actor, Entity user, ServicePayload payload ) { // create a new actor object if ( actor == null ) { actor = new HashMap<String, String>(); payload.setProperty( Activity.PROPERTY_ACTOR, actor ); } if ( user != null ) { if ( actor.get( User.PROPERTY_UUID ) == null && user.getUuid() != null ) { actor.put( User.PROPERTY_UUID, user.getUuid().toString() ); } if ( actor.get( User.PROPERTY_EMAIL ) == null && user.getProperty( User.PROPERTY_EMAIL ) != null ) { actor.put( User.PROPERTY_EMAIL, user.getProperty( User.PROPERTY_EMAIL ).toString() ); } } }
@Override public int compare( Entity o1, Entity o2 ) { long o1Index = ( Long ) o1.getProperty( "created" ); long o2Index = ( Long ) o2.getProperty( "created" ); if ( o1Index > o2Index ) { return 1; } else if ( o2Index > o1Index ) { return -1; } boolean o1Boolean = ( Boolean ) o1.getProperty( "intersect" ); boolean o2Boolean = ( Boolean ) o2.getProperty( "intersect" ); if ( o1Boolean != o2Boolean ) { if ( o1Boolean ) { return -1; } return 1; } return 0; } } );
/** side-effect: sets file-metadata on the entity if not present */ public static Map<String, Object> getFileMetadata( Entity entity ) { Map<String, Object> metadata = ( Map<String, Object> ) entity.getProperty( AssetUtils.FILE_METADATA ); if ( metadata == null ) { metadata = new HashMap<String, Object>(); entity.setProperty( AssetUtils.FILE_METADATA, metadata ); addLegacyMetadata( entity, metadata ); } // must always have a last modified. if not in the metadata, grab the entity's if ( metadata.get( LAST_MODIFIED ) == null ) { metadata.put( LAST_MODIFIED, entity.getModified() ); } return metadata; }
public static SimpleRoleRef forRoleEntity( Entity role ) { if ( role == null ) { return null; } UUID groupId = ( UUID ) role.getProperty( "group" ); String name = role.getName(); return new SimpleRoleRef( groupId, name ); }
public Results findForProperty( String propertyName, Object propertyValue, int count ) { if ( propertyValue == null ) { return new Results(); } List<Entity> l = getEntities(); if ( l == null ) { return new Results(); } List<Entity> found = new ArrayList<Entity>(); for ( Entity e : l ) { if ( propertyValue.equals( e.getProperty( propertyName ) ) ) { found.add( e ); if ( ( count > 0 ) && ( found.size() == count ) ) { break; } } } return Results.fromEntities(found); }
@Override public OrganizationInfo getOrganizationForApplication( UUID applicationInfoId ) throws Exception { Entity entity = getOrganizationEntityForApplication(applicationInfoId); return entity != null ? new OrganizationInfo( entity.getUuid(), ( String ) entity.getProperty( "path" ) ) : null; }
/** Invoked to set values when our actor is an activity object */ private void handleDynamicPayload( ActivityObject actor, Entity user, ServicePayload payload ) { // create a new actor object if ( actor == null ) { actor = new ActivityObject(); payload.setProperty( Activity.PROPERTY_ACTOR, actor ); } if ( user != null ) { if ( actor.getId() == null && user.getUuid() != null ) { actor.setUuid( user.getUuid() ); // TODO TN should this also populate id? } if ( actor.getDynamicProperties().get( User.PROPERTY_EMAIL ) == null && user.getProperty( User.PROPERTY_EMAIL ) != null ) { actor.getDynamicProperties() .put( User.PROPERTY_EMAIL, user.getProperty( User.PROPERTY_EMAIL ).toString() ); } } }
@Override public OrganizationConfig getOrganizationConfigByUuid( UUID id ) throws Exception { EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Entity entity = em.get( new SimpleEntityRef( Group.ENTITY_TYPE, id ) ); if ( entity == null ) { return getOrganizationConfigDefaultsOnly(); } Map<Object, Object> entityProperties = em.getDictionaryAsMap(entity, ORGANIZATION_CONFIG_DICTIONARY); return new OrganizationConfig( orgConfigProperties, (UUID)entity.getProperty(PROPERTY_UUID), (String)entity.getProperty(PROPERTY_PATH), entityProperties, false); }
@Override public List<OrganizationInfo> getOrganizations( UUID startResult, int count ) throws Exception { // still need the bimap to search for existing BiMap<UUID, String> organizations = HashBiMap.create(); EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results results = em.getCollection(em.getApplicationRef(), Schema.COLLECTION_GROUPS, startResult, count, Level.ALL_PROPERTIES, false); List<OrganizationInfo> orgs = new ArrayList<>( results.size() ); OrganizationInfo orgInfo; for ( Entity entity : results.getEntities() ) { String path = ( String ) entity.getProperty( PROPERTY_PATH ); if ( organizations.containsValue( path ) ) { path += "DUPLICATE"; } orgInfo = new OrganizationInfo( entity.getUuid(), path ); orgs.add( orgInfo ); organizations.put( entity.getUuid(), path ); } return orgs; }
@Test(expected = IllegalArgumentException.class) public void noPermissionsOnPost() throws Exception { app.put( "name", "manager" ); app.put( "title", "Manager Title" ); app.put( "inactivity", 600000l ); // test creating a new role ServiceResults results = app.testRequest( ServiceAction.POST, 1, "roles" ); // check the results Entity roleEntity = results.getEntities().get( 0 ); assertEquals( "manager", roleEntity.getProperty( "name" ) ); assertEquals( "Manager Title", roleEntity.getProperty( "title" ) ); assertEquals( 600000l, roleEntity.getProperty( "inactivity" ) ); app.put( "misspelledpermission", "access:/**" ); app.invokeService( ServiceAction.POST, "roles", "manager", "permissions" ); }
@Test(expected = IllegalArgumentException.class) public void noPermissionsOnPut() throws Exception { app.put( "name", "manager" ); app.put( "title", "Manager Title" ); app.put( "inactivity", 600000l ); // test creating a new role ServiceResults results = app.testRequest( ServiceAction.POST, 1, "roles" ); // check the results Entity roleEntity = results.getEntities().get( 0 ); assertEquals( "manager", roleEntity.getProperty( "name" ) ); assertEquals( "Manager Title", roleEntity.getProperty( "title" ) ); assertEquals( 600000l, roleEntity.getProperty( "inactivity" ) ); app.put( "misspelledpermission", "access:/**" ); // now grant permissions app.invokeService( ServiceAction.PUT, "roles", "manager", "permissions" ); }
@Test public void testImmutableForcedPropChange() throws Exception { logger.info( "EntityDaoTest.testProperties" ); EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "one" ); Entity saved = em.create( "thing", properties ); Entity thingOne = em.get( new SimpleEntityRef( "thing", saved.getUuid() ) ); assertNotNull( "entity should not be null", thingOne ); assertEquals( "one", thingOne.getProperty( "name" ).toString() ); em.setProperty( thingOne, "name", "two", true ); Entity thingTwo = em.get( new SimpleEntityRef( "thing", saved.getUuid() ) ); assertEquals( "two", thingTwo.getProperty( "name" ) ); }
@Override public UserInfo verifyMongoCredentials( String name, String nonce, String key ) throws Exception { Entity user = findUserEntity( smf.getManagementAppId(), name ); if ( user == null ) { return null; } String mongo_pwd = readUserMongoPassword( smf.getManagementAppId(), user.getUuid(), user.getType() ).getSecret(); if ( mongo_pwd == null ) { throw new IncorrectPasswordException( "Your mongo password has not be set" ); } String expected_key = DigestUtils.md5Hex( nonce + user.getProperty( "username" ) + mongo_pwd ); if ( !expected_key.equalsIgnoreCase( key ) ) { throw new IncorrectPasswordException(); } UserInfo userInfo = new UserInfo( smf.getManagementAppId(), user.getProperties() ); if ( !userInfo.isActivated() ) { throw new UnactivatedAdminUserException(); } if ( userInfo.isDisabled() ) { throw new DisabledAdminUserException(); } return userInfo; }
@Test public void testEntityClasses() throws Exception { logger.info( "testEntityClasses" ); Schema mapper = Schema.getDefaultSchema(); assertEquals( "group", mapper.getEntityType( Group.class ) ); assertEquals( User.class, mapper.getEntityClass( "user" ) ); Entity entity = EntityFactory.newEntity( null, "user" ); assertEquals( User.class, entity.getClass() ); User user = ( User ) entity; user.setUsername( "testuser" ); assertEquals( user.getUsername(), user.getProperty( "username" ) ); user.setProperty( "username", "blahblah" ); assertEquals( "blahblah", user.getUsername() ); entity = EntityFactory.newEntity( null, "foobar" ); assertEquals( DynamicEntity.class, entity.getClass() ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( Schema.PROPERTY_UUID, new UUID( 1, 2 ) ); properties.put( "foo", "bar" ); entity.setProperties( properties ); assertEquals( new UUID( 1, 2 ), entity.getUuid() ); assertEquals( new UUID( 1, 2 ), entity.getProperty( Schema.PROPERTY_UUID ) ); assertEquals( "bar", entity.getProperty( "foo" ) ); }