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