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.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            @Override
039            public boolean addMember(Principal user) {
040                    if (!_members.containsKey(user)) {
041                            _members.put(user, user);
042    
043                            return true;
044                    }
045                    else {
046                            return false;
047                    }
048            }
049    
050            @Override
051            public boolean isMember(Principal member) {
052                    boolean isMember = _members.containsKey(member);
053    
054                    if (!isMember) {
055                            Iterator<Principal> itr = _members.values().iterator();
056    
057                            while (!isMember && itr.hasNext()) {
058                                    Principal principal = itr.next();
059    
060                                    if (principal instanceof Group) {
061                                            Group group = (Group)principal;
062    
063                                            isMember = group.isMember(member);
064                                    }
065                            }
066                    }
067    
068                    return isMember;
069            }
070    
071            @Override
072            public Enumeration<Principal> members() {
073                    return Collections.enumeration(_members.values());
074            }
075    
076            @Override
077            public boolean removeMember(Principal user) {
078                    Principal principal = _members.remove(user);
079    
080                    if (principal != null) {
081                            return true;
082                    }
083                    else {
084                            return false;
085                    }
086            }
087    
088            private Map<Principal, Principal> _members =
089                    new HashMap<Principal, Principal>();
090    
091    }