1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.security.jaas;
16  
17  import java.io.Serializable;
18  
19  import java.security.Principal;
20  import java.security.acl.Group;
21  
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  /**
29   * <a href="PortalGroup.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class PortalGroup
34      extends PortalPrincipal implements Group, Serializable {
35  
36      public PortalGroup(String groupName) {
37          super(groupName);
38      }
39  
40      public boolean addMember(Principal user) {
41          if (!_members.containsKey(user)) {
42              _members.put(user, user);
43  
44              return true;
45          }
46          else {
47              return false;
48          }
49      }
50  
51      public boolean isMember(Principal member) {
52          boolean isMember = _members.containsKey(member);
53  
54          if (!isMember) {
55              Iterator<Principal> itr = _members.values().iterator();
56  
57              while (!isMember && itr.hasNext()) {
58                  Principal principal = itr.next();
59  
60                  if (principal instanceof Group) {
61                      Group group = (Group)principal;
62  
63                      isMember = group.isMember(member);
64                  }
65              }
66          }
67  
68          return isMember;
69      }
70  
71      public Enumeration<Principal> members() {
72          return Collections.enumeration(_members.values());
73      }
74  
75      public boolean removeMember(Principal user) {
76          Principal principal = _members.remove(user);
77  
78          if (principal != null) {
79              return true;
80          }
81          else {
82              return false;
83          }
84      }
85  
86      private Map<Principal, Principal> _members =
87          new HashMap<Principal, Principal>();
88  
89  }