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