001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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.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 *     George Lefter
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.security;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.runtime.model.ComponentInstance;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030/**
031 * A service that provided the UI visible permissions for different document types.
032 *
033 * @author <a href='mailto:glefter@nuxeo.com'>George Lefter</a>
034 * @deprecated use the PermissionProvider that is part of the core SecurityService instead
035 */
036@Deprecated
037public class UIPermissionService extends DefaultComponent {
038
039    public static final String NAME = "org.nuxeo.ecm.webapp.security.UIPermissionService";
040
041    private static final Log log = LogFactory.getLog(UIPermissionService.class);
042
043    private final Map<String, String[]> permissionMap = new HashMap<String, String[]>();
044
045    private String[] defaultPermissionList = new String[0];
046
047    @Override
048    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
049        if (extensionPoint.equals("uiPermissions")) {
050            UIPermissionListDescriptor desc = (UIPermissionListDescriptor) contribution;
051            if (desc.isDefault) {
052                defaultPermissionList = desc.permissions;
053            } else {
054                permissionMap.put(desc.documentType, desc.permissions);
055            }
056        } else {
057            log.error("unknown extension point: " + extensionPoint);
058        }
059    }
060
061    @Override
062    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
063        if (extensionPoint.equals("uiPermissions")) {
064            UIPermissionListDescriptor desc = (UIPermissionListDescriptor) contribution;
065            permissionMap.remove(desc.documentType);
066        } else {
067            log.error("unknown extension point: " + extensionPoint);
068        }
069    }
070
071    /**
072     * Retrieves the visible permissions for a document type.
073     *
074     * @param documentType the type of document for which to retrieve permissions, or null to retrieve the default
075     *            permissions
076     * @return the list of permissions for the specified document type
077     */
078    public String[] getUIPermissions(String documentType) {
079        String[] permissions = permissionMap.get(documentType);
080        if (documentType == null || permissions == null) {
081            return defaultPermissionList;
082        } else {
083            return permissions;
084        }
085    }
086
087}