1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.security.jaas;
21  
22  import java.io.Serializable;
23  
24  import java.security.Principal;
25  import java.security.acl.Group;
26  
27  import java.util.Collections;
28  import java.util.Enumeration;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  /**
34   * <a href="PortalGroup.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class PortalGroup
40      extends PortalPrincipal implements Group, Serializable {
41  
42      public PortalGroup(String groupName) {
43          super(groupName);
44      }
45  
46      public boolean addMember(Principal user) {
47          if (!_members.containsKey(user)) {
48              _members.put(user, user);
49  
50              return true;
51          }
52          else {
53              return false;
54          }
55      }
56  
57      public boolean isMember(Principal member) {
58          boolean isMember = _members.containsKey(member);
59  
60          if (!isMember) {
61              Iterator<Principal> itr = _members.values().iterator();
62  
63              while (!isMember && itr.hasNext()) {
64                  Principal principal = itr.next();
65  
66                  if (principal instanceof Group) {
67                      Group group = (Group)principal;
68  
69                      isMember = group.isMember(member);
70                  }
71              }
72          }
73  
74          return isMember;
75      }
76  
77      public Enumeration<Principal> members() {
78          return Collections.enumeration(_members.values());
79      }
80  
81      public boolean removeMember(Principal user) {
82          Principal principal = _members.remove(user);
83  
84          if (principal != null) {
85              return true;
86          }
87          else {
88              return false;
89          }
90      }
91  
92      private Map<Principal, Principal> _members =
93          new HashMap<Principal, Principal>();
94  
95  }