001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 */
018package org.nuxeo.ecm.user.invite;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.PropertyException;
024import org.nuxeo.runtime.api.Framework;
025
026/**
027 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
028 */
029
030public class RegistrationRules {
031    public static final String FACET_REGISTRATION_CONFIGURATION = "RegistrationConfiguration";
032
033    public static final String SCHEMA_REGISTRATION_RULES = "registrationconfiguration";
034
035    public static final String FIELD_ALLOW_USER_CREATION = SCHEMA_REGISTRATION_RULES + ":" + "allowUserCreation";
036
037    public static final String FIELD_ALLOW_DIRECT_VALIDATION = SCHEMA_REGISTRATION_RULES + ":"
038            + "allowDirectValidationForExistingUser";
039
040    public static final String FIELD_FORCE_RIGHT = SCHEMA_REGISTRATION_RULES + ":" + "forceRightAssignment";
041
042    public static final String FIELD_CONFIGURATION_NAME = SCHEMA_REGISTRATION_RULES + ":" + "name";
043
044    public static final String FIELD_DISPLAY_LOCAL_TAB = SCHEMA_REGISTRATION_RULES + ":"
045            + "displayLocalRegistrationTab";
046
047    public static final String FORCE_VALIDATION_NON_EXISTING_USER_PROPERTY = "nuxeo.user.registration.force.validation.non.existing";
048
049    protected DocumentModel requestContainer;
050
051    private static final Log log = LogFactory.getLog(RegistrationRules.class);
052
053    public RegistrationRules(DocumentModel requestContainer) {
054        this.requestContainer = requestContainer;
055    }
056
057    public boolean allowUserCreation() {
058        try {
059            return (Boolean) requestContainer.getPropertyValue(FIELD_ALLOW_USER_CREATION);
060        } catch (PropertyException e) {
061            log.warn("Unable to fetch AllowUserCreation flag using default value: " + e.getMessage());
062            return true;
063        }
064    }
065
066    public boolean allowDirectValidationForExistingUser() {
067        try {
068            return (Boolean) requestContainer.getPropertyValue(FIELD_ALLOW_DIRECT_VALIDATION);
069        } catch (PropertyException e) {
070            log.warn("Unable to fetch AllowDirectValidation flag using default value: " + e.getMessage());
071            return false;
072        }
073    }
074
075    public boolean isForcingRight() {
076        try {
077            return (Boolean) requestContainer.getPropertyValue(FIELD_FORCE_RIGHT);
078        } catch (PropertyException e) {
079            log.warn("Unable to fetch ForceRight flag using default value: " + e.getMessage());
080            return false;
081        }
082    }
083
084    public boolean isDisplayLocalTab() {
085        try {
086            return (Boolean) requestContainer.getPropertyValue(FIELD_DISPLAY_LOCAL_TAB);
087        } catch (PropertyException e) {
088            log.warn("Unable to fetch display local tab flag using default value: " + e.getMessage());
089            return true;
090        }
091    }
092
093    public boolean allowDirectValidationForNonExistingUser() {
094        return Framework.isBooleanPropertyTrue(FORCE_VALIDATION_NON_EXISTING_USER_PROPERTY);
095    }
096}