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