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;
}
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);
}
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);
}
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);
}
}
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);
}
}
}
}
Aggregations