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