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 *     Martin Pernollet
016 */
017
018package org.nuxeo.ecm.platform.groups.audit.service.acl.excel;
019
020import java.util.Set;
021import org.nuxeo.ecm.core.api.security.SecurityConstants;
022import com.google.common.collect.BiMap;
023import com.google.common.collect.HashBiMap;
024
025/**
026 * Holds a short name translation for each existing ACL. The implementation based on {@link HashBiMap} ensures no two
027 * short name can coexist. Any attempt to register a permission short name with an existing one will lead to an
028 * {@link IllegalArgumentException}.
029 *
030 * @author Martin Pernollet <mpernollet@nuxeo.com>
031 */
032public class AclNameShortner {
033    protected BiMap<String, String> mapping;
034    {
035        mapping = HashBiMap.create();
036
037        mapping.put(SecurityConstants.EVERYTHING, "A");
038
039        mapping.put(SecurityConstants.BROWSE, "B");
040
041        mapping.put(SecurityConstants.READ, "R");
042        mapping.put(SecurityConstants.READ_CHILDREN, "RC");
043        mapping.put(SecurityConstants.READ_LIFE_CYCLE, "RL");
044        mapping.put(SecurityConstants.READ_PROPERTIES, "RP");
045        mapping.put(SecurityConstants.READ_SECURITY, "RS");
046        mapping.put(SecurityConstants.READ_VERSION, "RV");
047
048        mapping.put(SecurityConstants.READ_WRITE, "RW");
049
050        mapping.put(SecurityConstants.WRITE, "W");
051        mapping.put(SecurityConstants.WRITE_LIFE_CYCLE, "WL");
052        mapping.put(SecurityConstants.WRITE_PROPERTIES, "WP");
053        mapping.put(SecurityConstants.WRITE_SECURITY, "WS");
054        mapping.put(SecurityConstants.WRITE_VERSION, "WV");
055
056        mapping.put(SecurityConstants.ADD_CHILDREN, "AC");
057        mapping.put(SecurityConstants.REMOVE_CHILDREN, "DC");
058
059        mapping.put(SecurityConstants.MANAGE_WORKFLOWS, "MW");
060        mapping.put(SecurityConstants.VIEW_WORKLFOW, "VW");
061
062        mapping.put(SecurityConstants.RESTRICTED_READ, "RR");
063        mapping.put(SecurityConstants.UNLOCK, "U");
064        mapping.put(SecurityConstants.VERSION, "V");
065        mapping.put(SecurityConstants.REMOVE, "RE");
066    }
067
068    /**
069     * Return the short name of a given permission.
070     *
071     * @throws an IllegalArgumentException if the permission is unknown.
072     */
073    public String getShortName(String permission) {
074        if (!mapping.containsKey(permission)) {
075            // Generate one with capitalized letters
076            String s = permission.replaceAll("[a-z\\s]", "");
077            String shortName = s;
078            int index = 1;
079            while (mapping.values().contains(shortName)) {
080                shortName = s + index;
081                index++;
082            }
083            mapping.put(permission, shortName);
084        }
085
086        return mapping.get(permission);
087    }
088
089    public String getFullName(String shortname) {
090        return mapping.inverse().get(shortname);
091    }
092
093    public void register(String permission, String shortname) {
094        mapping.put(permission, shortname);
095    }
096
097    public BiMap<String, String> getMapping() {
098        return mapping;
099    }
100
101    public Set<String> getShortNames() {
102        return getMapping().inverse().keySet();
103    }
104
105    public Set<String> getFullNames() {
106        return getMapping().keySet();
107    }
108}