001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.pacl;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.security.Permission;
020    import java.security.PermissionCollection;
021    import java.security.Policy;
022    
023    import java.util.Collections;
024    import java.util.Enumeration;
025    
026    /**
027     * @author Raymond Augé
028     */
029    public class PortalPermissionCollection extends PermissionCollection {
030    
031            public PortalPermissionCollection(
032                    PACLPolicy paclPolicy, PermissionCollection permissionCollection) {
033    
034                    _paclPolicy = paclPolicy;
035                    _permissionCollection = permissionCollection;
036            }
037    
038            @Override
039            public void add(Permission permission) {
040                    throw new SecurityException();
041            }
042    
043            @Override
044            public Enumeration<Permission> elements() {
045                    return Collections.enumeration(Collections.<Permission>emptyList());
046            }
047    
048            public ClassLoader getClassLoader() {
049                    return _paclPolicy.getClassLoader();
050            }
051    
052            public PACLPolicy getPACLPolicy() {
053                    return _paclPolicy;
054            }
055    
056            public Policy getPolicy() {
057                    return _paclPolicy.getPolicy();
058            }
059    
060            @Override
061            public boolean implies(Permission permission) {
062                    if (!_paclPolicy.isActive()) {
063                            return true;
064                    }
065    
066                    if (permission instanceof PACLUtil.Permission) {
067                            throw new PACLUtil.Exception(_paclPolicy);
068                    }
069    
070                    if (_permissionCollection.implies(permission) ||
071                            _paclPolicy.implies(permission)) {
072    
073                            return true;
074                    }
075    
076                    return false;
077            }
078    
079            @Override
080            public String toString() {
081                    Class<?> clazz = getClass();
082    
083                    String className = clazz.getSimpleName();
084    
085                    return className.concat(StringPool.POUND).concat(
086                            _paclPolicy.toString());
087            }
088    
089            private PACLPolicy _paclPolicy;
090            private PermissionCollection _permissionCollection;
091    
092    }