Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

root/openmrs/trunk/src/api/org/openmrs/Role.java

Revision 4095, 5.9 kB (checked in by catullus, 4 months ago)

Set the svn:eol-style property to CRLF.

  • Property svn:eol-style set to CRLF
Line 
1 /**
2  * The contents of this file are subject to the OpenMRS Public License
3  * Version 1.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  * http://license.openmrs.org
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9  * License for the specific language governing rights and limitations
10  * under the License.
11  *
12  * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
13  */
14 package org.openmrs;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.openmrs.util.OpenmrsConstants;
22
23 /**
24  * A Role is just an aggregater of {@link Privilege}s.  {@link User}s contain
25  * a number of roles (Users DO NOT contain any privileges directly)
26  *
27  * Roles can be grouped by inheriting other roles.  If a user is given Role A
28  * that inherits from Role B, the user has all rights/abilities for both Role A's
29  * privileges and for Role B's privileges.
30  *
31  * @see Privilege
32  */
33 public class Role implements java.io.Serializable {
34
35         public static final long serialVersionUID = 1234233L;
36         private static Log log = LogFactory.getLog(Role.class);
37
38         // Fields
39
40         private String role;
41         private String description;
42         private Set<Privilege> privileges;
43         private Set<Role> inheritedRoles;
44
45         // Constructors
46
47         /** default constructor */
48         public Role() {
49         }
50
51         /** constructor with id */
52         public Role(String role) {
53                 this.role = role;
54         }
55        
56         /** constructor with all database required properties */
57         public Role(String role, String description) {
58                 this.role = role;
59                 this.description = description;
60         }
61
62         /**
63          * @see java.lang.Object#equals(java.lang.Object)
64          */
65         public boolean equals(Object obj) {
66                 if (obj == null || !(obj instanceof Role) || role == null) return false;
67                 return role.equals(((Role)obj).getRole());
68         }
69        
70         /**
71          * @see java.lang.Object#hashCode()
72          */
73         public int hashCode() {
74                 if (this.getRole() == null) return super.hashCode();
75                 return this.getRole().hashCode();
76         }
77
78         // Property accessors
79
80         /**
81          * @return Returns the description.
82          */
83         public String getDescription() {
84                 return description;
85         }
86
87         /**
88          * @param description The description to set.
89          */
90         public void setDescription(String description) {
91                 this.description = description;
92         }
93
94         /**
95          * @return Returns the privileges.
96          */
97         public Set<Privilege> getPrivileges() {
98                 return privileges;
99         }
100
101         /**
102          * @param privileges The privileges to set.
103          */
104         public void setPrivileges(Set<Privilege> privileges) {
105                 this.privileges = privileges;
106         }
107
108         /**
109          * Adds the given Privilege to the list of privileges
110          * @param privilege Privilege to add
111          */
112         public void addPrivilege(Privilege privilege) {
113                 if (privileges == null)
114                         privileges = new HashSet<Privilege>();
115                 if (!privileges.contains(privilege) && privilege != null)
116                         privileges.add(privilege);
117         }
118        
119         /**
120          * Removes the given Privilege from the list of privileges
121          * @param privilegen Privilege to remove
122          */
123         public void removePrivilege(Privilege privilege) {
124                 if (privileges != null)
125                         privileges.remove(privilege);
126         }
127        
128         /**
129          * @return Returns the role.
130          */
131         public String getRole() {
132                 return role;
133         }
134
135         /**
136          * @param role The role to set.
137          */
138         public void setRole(String role) {
139                 this.role = role;
140         }
141        
142         /**
143          * @see java.lang.Object#toString()
144          */
145         public String toString() {
146                 return this.role;
147         }
148        
149         /**
150          * Looks for the given <code>privilegeName</code> privilege name in this
151          * roles privileges.  This method does not recurse through the
152          * inherited roles
153          *
154          * @param privilegeName String name of a privilege
155          * @return true/false whether this role has the given privilege
156          */
157         public boolean hasPrivilege(String privilegeName) {
158                
159                 if (OpenmrsConstants.SUPERUSER_ROLE.equals(this.role))
160                         return true;
161                
162                 if (privileges != null) {
163                         for (Privilege p : privileges) {
164                                 if (p.getPrivilege().equals(privilegeName))
165                                         return true;
166                         }
167                 }
168                
169                 return false;
170         }
171
172         /**
173          * @return Returns the parentRoles.
174          */
175         public Set<Role> getInheritedRoles() {
176                 return inheritedRoles;
177         }
178
179         /**
180          * @param parentRoles The parentRoles to set.
181          */
182         public void setInheritedRoles(Set<Role> inheritedRoles) {
183                 this.inheritedRoles = inheritedRoles;
184         }
185        
186         /**
187          * Convenience method to test whether or not this role extends/
188          * inherits from any other roles
189          *
190          * @return true/false whether this role inherits from other roles
191          */
192         public boolean inheritsRoles() {
193                 return (getInheritedRoles() != null && getInheritedRoles().size() > 0);
194         }
195        
196         /**
197          * Recursive (if need be) method to return all parent roles of this role
198          * 
199          * @return Return this role's parents
200          */
201         public Set<Role> getAllParentRoles() {
202                 Set<Role> parents = new HashSet<Role>();
203                 if (inheritsRoles()) {
204                         parents.addAll(this.recurseOverParents(parents));
205                 }
206                 return parents;
207         }
208        
209         /**
210          * Returns the full set of roles be looping over inherited roles.
211          * Duplicate roles are dropped.
212          *
213          * @param children Roles already looped over
214          * @return Set<Role> Current and inherited roles
215          */
216         public Set<Role> recurseOverParents(final Set<Role> children) {
217                 if (!this.inheritsRoles()) return children;
218                
219                 Set<Role> allRoles = new HashSet<Role>();       //total roles (parents + children)
220                 Set<Role> myRoles = new HashSet<Role>();        //new roles
221                 allRoles.addAll(children);
222                
223                 myRoles.addAll(this.getInheritedRoles());
224                 myRoles.removeAll(children);
225                 myRoles.remove(this);   //prevent an obvious looping problem
226                 allRoles.addAll(myRoles);
227                
228                 for (Role r : myRoles) {
229                         if (r.inheritsRoles())
230                                 allRoles.addAll(r.recurseOverParents(allRoles));
231                 }
232                
233                 if (log.isDebugEnabled())
234                         log.debug("Total roles: " + allRoles);
235                
236                 return allRoles;
237         }
238
239 }
Note: See TracBrowser for help on using the browser.