View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jetspeed.services.security;
19  
20  import java.util.HashMap;
21  import java.util.Iterator;
22  
23  import org.apache.jetspeed.om.security.BaseJetspeedGroup;
24  import org.apache.jetspeed.om.security.BaseJetspeedGroupRole;
25  import org.apache.jetspeed.om.security.Group;
26  import org.apache.jetspeed.om.security.GroupRole;
27  import org.apache.jetspeed.om.security.Role;
28  
29  /***
30   * Cached ACL - default implementation cached ACL containing role/permission.
31   *
32   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
33   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 
34   * @version $Id: CachedAcl.java,v 1.5 2004/02/23 03:58:11 jford Exp $
35   */
36  public class CachedAcl
37  {
38  	protected HashMap groupRoles = new HashMap();
39  	protected String userName;
40  
41  	/***
42  	 * @param userName
43  	 */
44  	public CachedAcl(String userName)
45  	{
46  		this.userName = userName;
47  	}
48  
49  	/***
50  	 * @param role
51  	 */
52  	public void addRole(Role role)
53  	{
54  		Group group = new BaseJetspeedGroup();
55  		group.setName(GroupManagement.DEFAULT_GROUP_NAME);
56  		addRole(role, group);
57  	}
58  
59  	/***
60  	 * @param role
61  	 * @param group
62  	 */
63  	public void addRole(Role role, Group group)
64  	{
65  		GroupRole gr = new BaseJetspeedGroupRole();
66  		gr.setRole(role);
67  		gr.setGroup(group);
68  		String key = getGroupRoleKey(group.getName(), role.getName());
69  		groupRoles.put(key, gr);
70  	}
71  
72  	/***
73  	 * @param roleName
74  	 * @return
75  	 */
76  	public Role getRole(String roleName)
77  	{
78  		return getRole(roleName, GroupManagement.DEFAULT_GROUP_NAME);
79  	}
80  
81  	/***
82  	 * @param roleName
83  	 * @param groupName
84  	 * @return
85  	 */
86  	public Role getRole(String roleName, String groupName)
87  	{
88  		GroupRole gr = (GroupRole) groupRoles.get(getGroupRoleKey(groupName, roleName));
89  		return gr != null ? gr.getRole() : null;
90  	}
91  
92  	/***
93  	 * @return
94  	 */
95  	public Iterator getRoles()
96  	{
97  		return groupRoles.values().iterator();
98  	}
99  
100 	/***
101 	 * @return
102 	 */
103 	public String getUserName()
104 	{
105 		return this.userName;
106 	}
107 
108 	/***
109 	 * @param roleName
110 	 * @return
111 	 */
112 	public boolean hasRole(String roleName)
113 	{
114 		return hasRole(roleName, GroupManagement.DEFAULT_GROUP_NAME);
115 	}
116 
117 	/***
118 	 * @param roleName
119 	 * @param groupName
120 	 * @return
121 	 */
122 	public boolean hasRole(String roleName, String groupName)
123 	{
124 		return groupRoles.containsKey(getGroupRoleKey(groupName, roleName));
125 	}
126 
127 	/***
128 	 * @param roleName
129 	 */
130 	public void removeRole(String roleName)
131 	{
132 		removeRole(roleName, GroupManagement.DEFAULT_GROUP_NAME);
133 	}
134 	
135 	/***
136 	 * @param roleName
137 	 * @param groupName
138 	 */
139 	public void removeRole(String roleName, String groupName)
140 	{
141 		groupRoles.remove(getGroupRoleKey(groupName, roleName));
142 	}
143 
144 	/***
145 	 * @param grouproles
146 	 */
147 	public void setRoles(Iterator grouproles)
148 	{
149 		while (grouproles.hasNext())
150 		{
151 			GroupRole grouprole = (GroupRole) grouproles.next();
152 			String key = getGroupRoleKey(grouprole.getGroup().getName(), grouprole.getRole().getName());
153 			this.groupRoles.put(key, grouprole);
154 		}
155 	}
156 
157 	/***
158 	 * @param groupName
159 	 * @param roleName
160 	 * @return
161 	 */
162 	private String getGroupRoleKey(String groupName, String roleName)
163 	{
164 		StringBuffer key = new StringBuffer();
165 		key.append(groupName);
166 		key.append(roleName);
167 		
168 		return key.toString();
169 	}
170 
171 }