001    /**
002     * Copyright (c) 2000-2011 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.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    }