Search in sources :

Example 6 with AttributeDefinition

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);
}
Also used : AttributePermission(org.craftercms.profile.api.AttributePermission) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition)

Example 7 with AttributeDefinition

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);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) VerificationToken(org.craftercms.profile.api.VerificationToken) ObjectId(org.bson.types.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date) Before(org.junit.Before)

Example 8 with AttributeDefinition

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);
}
Also used : Tenant(org.craftercms.profile.api.Tenant) HashMap(java.util.HashMap) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 9 with AttributeDefinition

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;
}
Also used : Tenant(org.craftercms.profile.api.Tenant) TenantUpdater(org.craftercms.profile.utils.db.TenantUpdater) Collection(java.util.Collection) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException)

Example 10 with AttributeDefinition

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);
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition)

Aggregations

AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)25 Tenant (org.craftercms.profile.api.Tenant)8 AttributePermission (org.craftercms.profile.api.AttributePermission)7 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)3 Mockito.anyString (org.mockito.Mockito.anyString)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Profile (org.craftercms.profile.api.Profile)2 VerificationToken (org.craftercms.profile.api.VerificationToken)2 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)2 TenantUpdater (org.craftercms.profile.utils.db.TenantUpdater)2 Collection (java.util.Collection)1 Pattern (java.util.regex.Pattern)1 ObjectId (org.bson.types.ObjectId)1 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)1 MongoDataException (org.craftercms.commons.mongo.MongoDataException)1 AttributeNotDefinedException (org.craftercms.profile.exceptions.AttributeNotDefinedException)1 InvalidEmailAddressException (org.craftercms.profile.exceptions.InvalidEmailAddressException)1