001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.security.jaas;
016    
017    import java.io.Serializable;
018    
019    import java.security.Principal;
020    import java.security.acl.Group;
021    
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortalGroup
031            extends PortalPrincipal implements Group, Serializable {
032    
033            public PortalGroup(String groupName) {
034                    super(groupName);
035            }
036    
037            @Override
038            public boolean addMember(Principal user) {
039                    if (!_members.containsKey(user)) {
040                            _members.put(user, user);
041    
042                            return true;
043                    }
044                    else {
045                            return false;
046                    }
047            }
048    
049            @Override
050            public boolean isMember(Principal member) {
051                    if (_members.containsKey(member)) {
052                            return true;
053                    }
054    
055                    for (Principal principal : _members.values()) {
056                            if (principal instanceof Group) {
057                                    Group group = (Group)principal;
058    
059                                    if (group.isMember(member)) {
060                                            return true;
061                                    }
062                            }
063                    }
064    
065                    return false;
066            }
067    
068            @Override
069            public Enumeration<Principal> members() {
070                    return Collections.enumeration(_members.values());
071            }
072    
073            @Override
074            public boolean removeMember(Principal user) {
075                    Principal principal = _members.remove(user);
076    
077                    if (principal != null) {
078                            return true;
079                    }
080                    else {
081                            return false;
082                    }
083            }
084    
085            private Map<Principal, Principal> _members =
086                    new HashMap<Principal, Principal>();
087    
088    }