Search in sources :

Example 6 with Tenant

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

the class ProfileServiceImpl method getProfileCountByQuery.

@Override
public long getProfileCountByQuery(String tenantName, String query) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    Tenant tenant = getTenant(tenantName);
    try {
        return profileRepository.count(getFinalQuery(tenant, query));
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_GET_PROFILE_COUNT_BY_QUERY_ERROR, e, tenant, query);
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Example 7 with Tenant

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

the class ProfileServiceImpl method getProfilesByQuery.

@Override
public List<Profile> getProfilesByQuery(String tenantName, String query, String sortBy, SortOrder sortOrder, Integer start, Integer count, String... attributesToReturn) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    Tenant tenant = getTenant(tenantName);
    try {
        List<Profile> profiles = IterableUtils.toList(profileRepository.findByQuery(getFinalQuery(tenant, query), sortBy, sortOrder, start, count, attributesToReturn));
        filterNonReadableAttributes(tenant, profiles);
        return profiles;
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_GET_PROFILES_BY_QUERY_ERROR, e, tenant, query);
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Profile(org.craftercms.profile.api.Profile)

Example 8 with Tenant

use of org.craftercms.profile.api.Tenant 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 9 with Tenant

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

the class ProfileServiceImplTest method getTenant1.

private Tenant getTenant1() {
    Tenant tenant = new Tenant();
    tenant.setName(TENANT1_NAME);
    tenant.setVerifyNewProfiles(true);
    tenant.setAttributeDefinitions(getAttributeDefinitions());
    return tenant;
}
Also used : Tenant(org.craftercms.profile.api.Tenant)

Example 10 with Tenant

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

the class ProfileServiceImplTest method getTenant2.

private Tenant getTenant2() {
    Tenant tenant = new Tenant();
    tenant.setName(TENANT2_NAME);
    tenant.setVerifyNewProfiles(false);
    tenant.setAttributeDefinitions(getAttributeDefinitions());
    return tenant;
}
Also used : Tenant(org.craftercms.profile.api.Tenant)

Aggregations

Tenant (org.craftercms.profile.api.Tenant)46 Test (org.junit.Test)21 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)12 AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)8 TenantUpdater (org.craftercms.profile.utils.db.TenantUpdater)8 HashMap (java.util.HashMap)7 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)7 Mockito.anyString (org.mockito.Mockito.anyString)7 MongoDataException (org.craftercms.commons.mongo.MongoDataException)5 Profile (org.craftercms.profile.api.Profile)5 ResourceNotFoundException (org.craftercms.profile.management.exceptions.ResourceNotFoundException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)1 UpdateHelper (org.craftercms.commons.mongo.UpdateHelper)1