Search in sources :

Example 1 with AttributeDefinition

use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.

the class TenantServiceImplTest method getAttribute2Definition.

private AttributeDefinition getAttribute2Definition() {
    AttributePermission permission = new AttributePermission(APP_NAME);
    permission.allow(AttributePermission.ANY_ACTION);
    AttributeDefinition def = new AttributeDefinition();
    def.setName(ATTRIB2_NAME);
    def.setMetadata(Collections.<String, Object>singletonMap(LABEL_KEY, ATTRIB2_LABEL));
    def.addPermission(permission);
    def.setDefaultValue(DEFAULT_ATTRIB_VALUE);
    return def;
}
Also used : AttributePermission(org.craftercms.profile.api.AttributePermission) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition)

Example 2 with AttributeDefinition

use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.

the class TenantServiceImplTest method testAddAttributeDefinitions.

@Test
public void testAddAttributeDefinitions() throws Exception {
    AttributeDefinition def = getAttribute2Definition();
    List<AttributeDefinition> defsToAdd = Collections.singletonList(def);
    Tenant expected = getTenant1();
    expected.getAttributeDefinitions().add(def);
    Map<String, Object> expectedPushParams = new HashMap<>();
    expectedPushParams.put("attributeDefinitions", Collections.singletonMap("$each", defsToAdd));
    Tenant actual = tenantService.addAttributeDefinitions(TENANT1_NAME, defsToAdd);
    assertEqualTenants(expected, actual);
    verify(profileRepository).updateAllWithDefaultValue(TENANT1_NAME, ATTRIB2_NAME, DEFAULT_ATTRIB_VALUE);
    verify(tenantRepository).findByName(TENANT1_NAME);
    verify(tenantRepository).update(TENANT1_ID.toString(), "{$push: #}", false, false, expectedPushParams);
}
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 3 with AttributeDefinition

use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.

the class TenantServiceImplTest method testUpdateAttributeDefinitions.

@Test
public void testUpdateAttributeDefinitions() throws Exception {
    AttributeDefinition def = getAttribute2Definition();
    def.setName(ATTRIB1_NAME);
    Tenant expected = getTenant1();
    expected.getAttributeDefinitions().clear();
    expected.getAttributeDefinitions().add(def);
    Map<String, Object> expectedSetParams = new HashMap<>();
    expectedSetParams.put("attributeDefinitions.0", def);
    Tenant actual = tenantService.updateAttributeDefinitions(TENANT1_NAME, Collections.singletonList(def));
    assertEqualTenants(expected, actual);
    verify(tenantRepository).findByName(TENANT1_NAME);
    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 4 with AttributeDefinition

use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.

the class ProfileServiceImpl method createProfile.

@Override
public Profile createProfile(String tenantName, String username, String password, String email, boolean enabled, Set<String> roles, Map<String, Object> attributes, String verificationUrl) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    if (!EmailUtils.validateEmail(email)) {
        throw new InvalidEmailAddressException(email);
    }
    try {
        Tenant tenant = getTenant(tenantName);
        Date now = new Date();
        Profile profile = new Profile();
        profile.setTenant(tenantName);
        profile.setUsername(username);
        profile.setPassword(CryptoUtils.hashPassword(password));
        profile.setEmail(email);
        profile.setCreatedOn(now);
        profile.setLastModified(now);
        profile.setVerified(false);
        boolean emailNewProfiles = tenant.isVerifyNewProfiles();
        if (!emailNewProfiles || StringUtils.isEmpty(verificationUrl)) {
            profile.setEnabled(enabled);
        }
        if (CollectionUtils.isNotEmpty(roles)) {
            profile.setRoles(roles);
        }
        for (AttributeDefinition definition : tenant.getAttributeDefinitions()) {
            if (definition.getDefaultValue() != null) {
                profile.setAttribute(definition.getName(), definition.getDefaultValue());
            }
        }
        if (MapUtils.isNotEmpty(attributes)) {
            rejectAttributesIfActionNotAllowed(tenant, attributes.keySet(), AttributeAction.WRITE_ATTRIBUTE);
            profile.getAttributes().putAll(attributes);
        }
        profileRepository.insert(profile);
        logger.debug(LOG_KEY_PROFILE_CREATED, profile);
        if (emailNewProfiles && StringUtils.isNotEmpty(verificationUrl)) {
            VerificationToken token = verificationService.createToken(profile);
            verificationService.sendEmail(token, profile, verificationUrl, newProfileEmailFromAddress, newProfileEmailSubject, newProfileEmailTemplateName);
        }
        return profile;
    } catch (DuplicateKeyException e) {
        throw new ProfileExistsException(tenantName, username);
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_PROFILE_ERROR, e, username, tenantName);
    }
}
Also used : ProfileExistsException(org.craftercms.profile.exceptions.ProfileExistsException) Tenant(org.craftercms.profile.api.Tenant) InvalidEmailAddressException(org.craftercms.profile.exceptions.InvalidEmailAddressException) VerificationToken(org.craftercms.profile.api.VerificationToken) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Date(java.util.Date) Profile(org.craftercms.profile.api.Profile) DuplicateKeyException(org.craftercms.commons.mongo.DuplicateKeyException)

Example 5 with AttributeDefinition

use of org.craftercms.profile.api.AttributeDefinition in project profile by craftercms.

the class ProfileServiceImpl method validateQuery.

protected void validateQuery(Tenant tenant, String query) throws ProfileException {
    if (QUERY_TENANT_PATTERN.matcher(query).find()) {
        throw new InvalidQueryException(ERROR_KEY_TENANT_NOT_ALLOWED);
    }
    if (QUERY_WHERE_PATTERN.matcher(query).find()) {
        throw new InvalidQueryException(ERROR_KEY_WHERE_NOT_ALLOWED);
    }
    for (AttributeDefinition definition : tenant.getAttributeDefinitions()) {
        if (!attributePermissionEvaluator.isAllowed(definition, AttributeAction.READ_ATTRIBUTE.toString())) {
            String attributeName = definition.getName();
            Pattern pattern = Pattern.compile(String.format(QUERY_ATTRIBUTE_PATTERN_FORMAT, attributeName));
            if (pattern.matcher(query).find()) {
                throw new InvalidQueryException(ERROR_KEY_ATTRIBUTE_NOT_ALLOWED, attributeName);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) InvalidQueryException(org.craftercms.profile.exceptions.InvalidQueryException)

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