Search in sources :

Example 6 with NodeFactory

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;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) User(org.structr.web.entity.User) HashSet(java.util.HashSet) Set(java.util.Set) NodeFactory(org.structr.core.graph.NodeFactory) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) Principal(org.structr.core.entity.Principal) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

NodeFactory (org.structr.core.graph.NodeFactory)6 Relationship (org.structr.api.graph.Relationship)3 RelationshipFactory (org.structr.core.graph.RelationshipFactory)3 FrameworkException (org.structr.common.error.FrameworkException)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 PropertyMap (org.structr.core.property.PropertyMap)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 DatabaseService (org.structr.api.DatabaseService)1 Node (org.structr.api.graph.Node)1 NodeDataContainer (org.structr.cloud.message.NodeDataContainer)1 RelationshipDataContainer (org.structr.cloud.message.RelationshipDataContainer)1 Principal (org.structr.core.entity.Principal)1 NodeInterface (org.structr.core.graph.NodeInterface)1 RelationshipInterface (org.structr.core.graph.RelationshipInterface)1 Tx (org.structr.core.graph.Tx)1 PropertyKey (org.structr.core.property.PropertyKey)1 File (org.structr.dynamic.File)1