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.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    }