use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.
the class ProfileServiceImplTest method getAttributeDefinitions.
private List<AttributeDefinition> getAttributeDefinitions() {
AttributePermission anyAppCanDoAnything = new AttributePermission(AttributePermission.ANY_APPLICATION);
anyAppCanDoAnything.allow(AttributePermission.ANY_ACTION);
AttributeDefinition firstNameDefinition = new AttributeDefinition();
firstNameDefinition.setName(ATTRIB_NAME_FIRST_NAME);
firstNameDefinition.addPermission(anyAppCanDoAnything);
AttributeDefinition lastNameDefinition = new AttributeDefinition();
lastNameDefinition.setName(ATTRIB_NAME_LAST_NAME);
lastNameDefinition.addPermission(anyAppCanDoAnything);
AttributeDefinition genderDefinition = new AttributeDefinition();
genderDefinition.setName(ATTRIB_NAME_GENDER);
genderDefinition.addPermission(anyAppCanDoAnything);
genderDefinition.setDefaultValue(GENDER);
AttributeDefinition privateDefinition = new AttributeDefinition();
privateDefinition.setName(ATTRIB_NAME_PRIVATE);
return Arrays.asList(firstNameDefinition, lastNameDefinition, genderDefinition, privateDefinition);
}
use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.
the class ProfileServiceImplTest method setUp.
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(tenantPermissionEvaluator.isAllowed(anyString(), anyString())).thenReturn(true);
when(attributePermissionEvaluator.isAllowed(any(AttributeDefinition.class), anyString())).thenReturn(true);
when(attributePermissionEvaluator.isAllowed(eq(new AttributeDefinition(ATTRIB_NAME_PRIVATE)), anyString())).thenReturn(false);
when(tenantService.getTenant(TENANT1_NAME)).thenReturn(getTenant1());
when(tenantService.getTenant(TENANT2_NAME)).thenReturn(getTenant2());
when(authenticationService.getTicket(TICKET_ID)).thenReturn(getTicket());
doAnswer(new Answer() {
@Override
public Object answer(final InvocationOnMock invocation) throws Throwable {
Profile profile = (Profile) invocation.getArguments()[0];
profile.setId(new ObjectId());
return null;
}
}).when(profileRepository).insert(any(Profile.class));
when(profileRepository.findOneByQuery(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY), new String[0])).thenReturn(getTenant1Profile());
when(profileRepository.findById(PROFILE1_ID.toString(), new String[0])).thenReturn(getTenant1Profile());
when(profileRepository.findById(PROFILE1_ID.toString(), NO_ATTRIBUTE)).thenReturn(getTenant1ProfileNoAttributes());
when(profileRepository.findById(PROFILE1_ID.toString(), ATTRIB_NAME_FIRST_NAME)).thenReturn(getTenant1ProfileNoLastName());
when(profileRepository.findById(PROFILE2_ID.toString(), new String[0])).thenReturn(getTenant2Profile());
when(profileRepository.findByQuery(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY), SORT_BY, SortOrder.ASC, START, COUNT, new String[0])).thenReturn(getAllTenant1Profiles());
when(profileRepository.findByTenantAndUsername(TENANT1_NAME, USERNAME1, new String[0])).thenReturn(getTenant1Profile());
when(profileRepository.findByIds(TENANT1_PROFILE_IDS, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
when(profileRepository.findRange(TENANT1_NAME, SORT_BY, SortOrder.ASC, START, COUNT)).thenReturn(getAllTenant1Profiles());
when(profileRepository.findByTenantAndRole(TENANT1_NAME, ROLE1, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
when(profileRepository.findByTenantAndAttributeValue(TENANT1_NAME, ATTRIB_NAME_FIRST_NAME, FIRST_NAME, SORT_BY, SortOrder.ASC)).thenReturn(getAllTenant1Profiles());
when(profileRepository.countByTenant(TENANT1_NAME)).thenReturn(10L);
when(profileRepository.count(String.format(ProfileServiceImpl.QUERY_FINAL_FORMAT, TENANT1_NAME, QUERY))).thenReturn(1L);
when(verificationService.createToken(any(Profile.class))).then(new Answer<VerificationToken>() {
@Override
public VerificationToken answer(final InvocationOnMock invocation) throws Throwable {
Profile profile = (Profile) invocation.getArguments()[0];
VerificationToken token = new VerificationToken();
token.setId(VERIFICATION_TOKEN_ID1);
token.setTenant(profile.getTenant());
token.setProfileId(profile.getId().toString());
token.setTimestamp(new Date());
return token;
}
});
VerificationToken token1 = new VerificationToken();
token1.setId(VERIFICATION_TOKEN_ID1);
token1.setTenant(TENANT1_NAME);
token1.setProfileId(PROFILE1_ID.toString());
token1.setTimestamp(new Date());
VerificationToken token2 = new VerificationToken();
token2.setId(VERIFICATION_TOKEN_ID2);
token2.setTenant(TENANT2_NAME);
token2.setProfileId(PROFILE2_ID.toString());
token2.setTimestamp(new Date());
when(verificationService.getToken(VERIFICATION_TOKEN_ID1)).thenReturn(token1);
when(verificationService.getToken(VERIFICATION_TOKEN_ID2)).thenReturn(token2);
profileService = new ProfileServiceImpl();
profileService.setTenantPermissionEvaluator(tenantPermissionEvaluator);
profileService.setAttributePermissionEvaluator(attributePermissionEvaluator);
profileService.setProfileRepository(profileRepository);
profileService.setTenantService(tenantService);
profileService.setVerificationService(verificationService);
profileService.setNewProfileEmailFromAddress(VERIFICATION_FROM_ADDRESS);
profileService.setNewProfileEmailSubject(VERIFICATION_SUBJECT);
profileService.setNewProfileEmailTemplateName(VERIFICATION_TEMPLATE_NAME);
profileService.setResetPwdEmailFromAddress(RESET_PASSWORD_FROM_ADDRESS);
profileService.setResetPwdEmailSubject(RESET_PASSWORD_SUBJECT);
profileService.setResetPwdEmailTemplateName(RESET_PASSWORD_TEMPLATE_NAME);
}
use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.
the class TenantServiceImplTest method testUpdateTenant.
@Test
public void testUpdateTenant() throws Exception {
AttributeDefinition def1 = new AttributeDefinition();
def1.setName(ATTRIB1_NAME);
AttributeDefinition def2 = new AttributeDefinition();
def2.setName(ATTRIB2_NAME);
def2.setDefaultValue(DEFAULT_ATTRIB_VALUE);
Tenant expected = getTenant1();
expected.getAvailableRoles().remove(ROLE1);
expected.getAttributeDefinitions().remove(def1);
expected.getAttributeDefinitions().add(def2);
Map<String, Object> expectedSetParams = new HashMap<>();
expectedSetParams.put("verifyNewProfiles", expected.isVerifyNewProfiles());
expectedSetParams.put("availableRoles", expected.getAvailableRoles());
expectedSetParams.put("ssoEnabled", expected.isSsoEnabled());
expectedSetParams.put("attributeDefinitions", expected.getAttributeDefinitions());
Tenant actual = tenantService.updateTenant(expected);
assertEqualTenants(expected, actual);
verify(profileRepository).removeRoleFromAll(TENANT1_NAME, ROLE1);
verify(profileRepository).removeAttributeFromAll(TENANT1_NAME, ATTRIB1_NAME);
verify(profileRepository).updateAllWithDefaultValue(TENANT1_NAME, ATTRIB2_NAME, DEFAULT_ATTRIB_VALUE);
verify(tenantRepository).update(TENANT1_ID.toString(), "{$set: #}", false, false, expectedSetParams);
}
use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.
the class TenantServiceImpl method updateTenant.
@Override
public Tenant updateTenant(final Tenant tenant) throws ProfileException {
final String tenantName = tenant.getName();
Tenant updatedTenant = updateTenant(tenant.getName(), new UpdateCallback() {
@Override
public void doWithTenant(TenantUpdater tenantUpdater) throws ProfileException {
Tenant originalTenant = tenantUpdater.getTenant();
Collection<String> originalRoles = originalTenant.getAvailableRoles();
Collection<String> newRoles = tenant.getAvailableRoles();
Collection<AttributeDefinition> originalDefinitions = originalTenant.getAttributeDefinitions();
Collection<AttributeDefinition> newDefinitions = tenant.getAttributeDefinitions();
Collection<String> removedRoles = CollectionUtils.subtract(originalRoles, newRoles);
Collection<AttributeDefinition> removedDefinitions = CollectionUtils.subtract(originalDefinitions, newDefinitions);
for (String removedRole : removedRoles) {
removeRoleFromProfiles(tenantName, removedRole);
}
for (AttributeDefinition removedDefinition : removedDefinitions) {
removeAttributeFromProfiles(tenantName, removedDefinition.getName());
}
tenantUpdater.setVerifyNewProfiles(tenant.isVerifyNewProfiles());
tenantUpdater.setSsoEnabled(tenant.isSsoEnabled());
tenantUpdater.setAvailableRoles(tenant.getAvailableRoles());
tenantUpdater.setAttributeDefinitions(tenant.getAttributeDefinitions());
}
});
for (AttributeDefinition definition : updatedTenant.getAttributeDefinitions()) {
addDefaultValue(tenantName, definition.getName(), definition.getDefaultValue());
}
return updatedTenant;
}
use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.
the class TenantUpdater method removeAttributeDefinitions.
public void removeAttributeDefinitions(Collection<String> attributeNames) {
List<AttributeDefinition> attributeDefinitions = tenant.getAttributeDefinitions();
List<String> pullValues = new ArrayList<>();
for (String attributeName : attributeNames) {
for (Iterator<AttributeDefinition> iter = attributeDefinitions.iterator(); iter.hasNext(); ) {
AttributeDefinition definition = iter.next();
if (definition.getName().equals(attributeName)) {
iter.remove();
pullValues.add(attributeName);
break;
}
}
}
updateHelper.pullAllDocuments("attributeDefinitions", "name", pullValues);
}
Aggregations