use of org.structr.core.graph.NodeFactory in project structr by structr.
the class RegistrationResource method createUser.
/**
* Create a new user.
*
* If a {@link Principal} is found, convert that object to a {@link Principal} object.
* If autoCreate is true, auto-create a new user, even if no matching person is found.
*
* @param securityContext
* @param credentialKey
* @param credentialValue
* @param propertySet
* @param autoCreate
* @param userClass
* @return user
*/
public static Principal createUser(final SecurityContext securityContext, final PropertyKey credentialKey, final String credentialValue, final Map<String, Object> propertySet, final boolean autoCreate, final Class userClass, final String confKey) {
final PropertyKey<String> confirmationKeyKey = StructrApp.key(User.class, "confirmationKey");
Principal user = null;
try {
// First, search for a person with that e-mail address
user = AuthHelper.getPrincipalForCredential(credentialKey, credentialValue);
if (user != null) {
user = new NodeFactory<Principal>(securityContext).instantiate(user.getNode());
// convert to user
user.unlockSystemPropertiesOnce();
final PropertyMap changedProperties = new PropertyMap();
changedProperties.put(AbstractNode.type, User.class.getSimpleName());
changedProperties.put(confirmationKeyKey, confKey);
user.setProperties(securityContext, changedProperties);
} else if (autoCreate) {
final App app = StructrApp.getInstance(securityContext);
// Clear properties set by us from the user-defined props
propertySet.remove(credentialKey.jsonName());
propertySet.remove("confirmationKey");
PropertyMap props = PropertyMap.inputTypeToJavaType(securityContext, Principal.class, propertySet);
// Remove any property which is not included in configuration
// eMail is mandatory and necessary
final String customAttributesString = "eMail" + "," + Settings.RegistrationCustomAttributes.getValue();
final List<String> customAttributes = Arrays.asList(customAttributesString.split("[ ,]+"));
final Set<PropertyKey> propsToRemove = new HashSet<>();
for (final PropertyKey key : props.keySet()) {
if (!customAttributes.contains(key.jsonName())) {
propsToRemove.add(key);
}
}
for (final PropertyKey propToRemove : propsToRemove) {
props.remove(propToRemove);
}
props.put(credentialKey, credentialValue);
props.put(confirmationKeyKey, confKey);
user = (Principal) app.create(userClass, props);
} else {
logger.info("User self-registration is not configured yet, cannot create new user.");
}
} catch (FrameworkException ex) {
logger.error("", ex);
}
return user;
}
Aggregations