001/*
002 * (C) Copyright 2013 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 *     Benjamin Jalon
016 */
017
018package org.nuxeo.ecm.platform.computedgroups;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl;
027
028/**
029 * Configurable Group Computer based on metadata of the user.
030 *
031 * @since 5.7.3
032 */
033public class UserMetadataGroupComputer extends AbstractGroupComputer {
034
035    public static final Log log = LogFactory.getLog(UserMetadataGroupComputer.class);
036
037    private String groupPattern;
038
039    private String xpath;
040
041    public UserMetadataGroupComputer(String xpath, String groupPattern) {
042        this.xpath = xpath;
043        this.groupPattern = groupPattern;
044
045        if (xpath == null || xpath.isEmpty() || groupPattern == null || groupPattern.isEmpty()) {
046            throw new NuxeoException("Bad configuration");
047        }
048
049    }
050
051    @Override
052    public List<String> getAllGroupIds() {
053        return new ArrayList<String>();
054    }
055
056    @Override
057    public List<String> getGroupMembers(String groupId) {
058        return new ArrayList<String>();
059    }
060
061    @Override
062    public List<String> getGroupsForUser(NuxeoPrincipalImpl user) {
063        String value = (String) user.getModel().getPropertyValue(xpath);
064
065        if (value == null || "".equals(value.trim())) {
066            return new ArrayList<String>();
067        }
068
069        ArrayList<String> result = new ArrayList<String>();
070        result.add(String.format(groupPattern, value));
071
072        return result;
073    }
074
075    @Override
076    public List<String> getParentsGroupNames(String arg0) {
077        return new ArrayList<String>();
078    }
079
080    @Override
081    public List<String> getSubGroupsNames(String arg0) {
082        return new ArrayList<String>();
083    }
084
085    @Override
086    public boolean hasGroup(String groupId) {
087        return false;
088    }
089}