001    /**
002     * Copyright (c) 2000-2010 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.Iterator;
026    import java.util.Map;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PortalGroup
032            extends PortalPrincipal implements Group, Serializable {
033    
034            public PortalGroup(String groupName) {
035                    super(groupName);
036            }
037    
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            public boolean isMember(Principal member) {
050                    boolean isMember = _members.containsKey(member);
051    
052                    if (!isMember) {
053                            Iterator<Principal> itr = _members.values().iterator();
054    
055                            while (!isMember && itr.hasNext()) {
056                                    Principal principal = itr.next();
057    
058                                    if (principal instanceof Group) {
059                                            Group group = (Group)principal;
060    
061                                            isMember = group.isMember(member);
062                                    }
063                            }
064                    }
065    
066                    return isMember;
067            }
068    
069            public Enumeration<Principal> members() {
070                    return Collections.enumeration(_members.values());
071            }
072    
073            public boolean removeMember(Principal user) {
074                    Principal principal = _members.remove(user);
075    
076                    if (principal != null) {
077                            return true;
078                    }
079                    else {
080                            return false;
081                    }
082            }
083    
084            private Map<Principal, Principal> _members =
085                    new HashMap<Principal, Principal>();
086    
087    }