001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 */
016package org.nuxeo.ecm.user.invite;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.PropertyException;
022import org.nuxeo.runtime.api.Framework;
023
024/**
025 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
026 */
027
028public class RegistrationRules {
029    public static final String FACET_REGISTRATION_CONFIGURATION = "RegistrationConfiguration";
030
031    public static final String SCHEMA_REGISTRATION_RULES = "registrationconfiguration";
032
033    public static final String FIELD_ALLOW_USER_CREATION = SCHEMA_REGISTRATION_RULES + ":" + "allowUserCreation";
034
035    public static final String FIELD_ALLOW_DIRECT_VALIDATION = SCHEMA_REGISTRATION_RULES + ":"
036            + "allowDirectValidationForExistingUser";
037
038    public static final String FIELD_FORCE_RIGHT = SCHEMA_REGISTRATION_RULES + ":" + "forceRightAssignment";
039
040    public static final String FIELD_CONFIGURATION_NAME = SCHEMA_REGISTRATION_RULES + ":" + "name";
041
042    public static final String FIELD_DISPLAY_LOCAL_TAB = SCHEMA_REGISTRATION_RULES + ":"
043            + "displayLocalRegistrationTab";
044
045    protected DocumentModel requestContainer;
046
047    private static final Log log = LogFactory.getLog(RegistrationRules.class);
048
049    public RegistrationRules(DocumentModel requestContainer) {
050        this.requestContainer = requestContainer;
051    }
052
053    public boolean allowUserCreation() {
054        try {
055            return (Boolean) requestContainer.getPropertyValue(FIELD_ALLOW_USER_CREATION);
056        } catch (PropertyException e) {
057            log.warn("Unable to fetch AllowUserCreation flag using default value: " + e.getMessage());
058            return true;
059        }
060    }
061
062    public boolean allowDirectValidationForExistingUser() {
063        try {
064            return (Boolean) requestContainer.getPropertyValue(FIELD_ALLOW_DIRECT_VALIDATION);
065        } catch (PropertyException e) {
066            log.warn("Unable to fetch AllowDirectValidation flag using default value: " + e.getMessage());
067            return false;
068        }
069    }
070
071    public boolean isForcingRight() {
072        try {
073            return (Boolean) requestContainer.getPropertyValue(FIELD_FORCE_RIGHT);
074        } catch (PropertyException e) {
075            log.warn("Unable to fetch ForceRight flag using default value: " + e.getMessage());
076            return false;
077        }
078    }
079
080    public boolean isDisplayLocalTab() {
081        try {
082            return (Boolean) requestContainer.getPropertyValue(FIELD_DISPLAY_LOCAL_TAB);
083        } catch (PropertyException e) {
084            log.warn("Unable to fetch display local tab flag using default value: " + e.getMessage());
085            return true;
086        }
087    }
088
089    public boolean allowDirectValidationForNonExistingUser() {
090        return Framework.isBooleanPropertyTrue("nuxeo.user.registration.force.validation.non.existing");
091    }
092}