• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 15205088843

23 May 2025 07:43AM UTC coverage: 65.083% (+0.01%) from 65.069%
15205088843

push

github

rkorytkowski
TRUNK-6300: Adding Windows test, cleaning up logs, adjusting variable name

2 of 2 new or added lines in 2 files covered. (100.0%)

336 existing lines in 18 files now uncovered.

23379 of 35922 relevant lines covered (65.08%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.25
/api/src/main/java/org/openmrs/Role.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs;
11

12
import javax.persistence.Cacheable;
13
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.Set;
16

17
import org.hibernate.annotations.Cache;
18
import org.hibernate.annotations.CacheConcurrencyStrategy;
19
import org.hibernate.envers.Audited;
20
import org.openmrs.util.RoleConstants;
21
import org.slf4j.Logger;
22
import org.slf4j.LoggerFactory;
23

24
/**
25
 * A Role is just an aggregater of {@link Privilege}s. {@link User}s contain a number of roles
26
 * (Users DO NOT contain any privileges directly) Roles can be grouped by inheriting other roles. If
27
 * a user is given Role A that inherits from Role B, the user has all rights/abilities for both Role
28
 * A's privileges and for Role B's privileges.
29
 *
30
 * @see Privilege
31
 */
32
@Cacheable
33
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
34
@Audited
35
public class Role extends BaseChangeableOpenmrsMetadata {
36
        
37
        public static final long serialVersionUID = 1234233L;
38
        
39
        private static final Logger log = LoggerFactory.getLogger(Role.class);
1✔
40
        
41
        // Fields
42
        
43
        private String role;
44
        
45
        private Set<Privilege> privileges;
46
        
47
        private Set<Role> inheritedRoles;
48
        
49
        private Set<Role> childRoles;
50
        
51
        // Constructors
52
        
53
        /** default constructor */
54
        public Role() {
1✔
55
        }
1✔
56
        
57
        /** constructor with id */
58
        public Role(String role) {
1✔
59
                this.role = role;
1✔
60
        }
1✔
61
        
62
        /** constructor with all database required properties */
63
        public Role(String role, String description) {
1✔
64
                this.role = role;
1✔
65
                setDescription(description);
1✔
66
        }
1✔
67
        
68
        /**
69
         * @return Returns the privileges.
70
         */
71
        public Set<Privilege> getPrivileges() {
72
                return privileges;
1✔
73
        }
74
        
75
        /**
76
         * @param privileges The privileges to set.
77
         */
78
        public void setPrivileges(Set<Privilege> privileges) {
79
                this.privileges = privileges;
1✔
80
        }
1✔
81
        
82
        @Override
83
        public String getName() {
84
                return this.getRole();
1✔
85
        }
86
        
87
        /**
88
         * Adds the given Privilege to the list of privileges
89
         *
90
         * @param privilege Privilege to add
91
         */
92
        public void addPrivilege(Privilege privilege) {
93
                if (privileges == null) {
1✔
94
                        privileges = new HashSet<>();
1✔
95
                }
96
                if (privilege != null && !containsPrivilege(privileges, privilege.getPrivilege())) {
1✔
97
                        privileges.add(privilege);
1✔
98
                }
99
        }
1✔
100
        
101
        private boolean containsPrivilege(Collection<Privilege> privileges, String privilegeName) {
102
                for (Privilege privilege : privileges) {
1✔
103
                        if (privilege.getPrivilege().equals(privilegeName)) {
1✔
104
                                return true;
1✔
105
                        }
106
                }
1✔
107
                return false;
1✔
108
        }
109
        
110
        /**
111
         * Removes the given Privilege from the list of privileges
112
         *
113
         * @param privilege Privilege to remove
114
         */
115
        public void removePrivilege(Privilege privilege) {
116
                if (privileges != null) {
1✔
117
                        privileges.remove(privilege);
1✔
118
                }
119
        }
1✔
120
        
121
        /**
122
         * @return Returns the role.
123
         */
124
        public String getRole() {
125
                return role;
1✔
126
        }
127
        
128
        /**
129
         * @param role The role to set.
130
         */
131
        public void setRole(String role) {
132
                this.role = role;
1✔
133
        }
1✔
134
        
135
        /**
136
         * @see java.lang.Object#toString()
137
         */
138
        @Override
139
        public String toString() {
UNCOV
140
                return this.role;
×
141
        }
142
        
143
        /**
144
         * Looks for the given <code>privilegeName</code> privilege name in this roles privileges. This
145
         * method does not recurse through the inherited roles
146
         *
147
         * @param privilegeName String name of a privilege
148
         * @return true/false whether this role has the given privilege
149
         * <strong>Should</strong> return false if not found
150
         * <strong>Should</strong> return true if found
151
         * <strong>Should</strong> not fail given null parameter
152
         * <strong>Should</strong> return true for any privilegeName if super user
153
         */
154
        public boolean hasPrivilege(String privilegeName) {
155
                
156
                if (RoleConstants.SUPERUSER.equals(this.role)) {
1✔
157
                        return true;
1✔
158
                }
159
                
160
                if (privileges != null) {
1✔
161
                        for (Privilege p : privileges) {
1✔
162
                                if (p.getPrivilege().equalsIgnoreCase(privilegeName)) {
1✔
163
                                        return true;
1✔
164
                                }
165
                        }
1✔
166
                }
167
                
168
                return false;
1✔
169
        }
170
        
171
        /**
172
         * @return Returns the inheritedRoles.
173
         */
174
        public Set<Role> getInheritedRoles() {
175
                if (inheritedRoles == null) {
1✔
176
                        inheritedRoles = new HashSet<>();
1✔
177
                }
178
                return inheritedRoles;
1✔
179
        }
180
        
181
        /**
182
         * @param inheritedRoles The inheritedRoles to set.
183
         */
184
        public void setInheritedRoles(Set<Role> inheritedRoles) {
185
                this.inheritedRoles = inheritedRoles;
1✔
186
        }
1✔
187
        
188
        /**
189
         * Convenience method to test whether or not this role extends/ inherits from any other roles
190
         *
191
         * @return true/false whether this role inherits from other roles
192
         */
193
        public boolean inheritsRoles() {
194
                return getInheritedRoles() != null && !getInheritedRoles().isEmpty();
1✔
195
        }
196
        
197
        /**
198
         * Recursive (if need be) method to return all parent roles of this role
199
         *
200
         * <strong>Should</strong> only return parent roles
201
         * @return Return this role's parents
202
         */
203
        public Set<Role> getAllParentRoles() {
204
                Set<Role> parents = new HashSet<>();
1✔
205
                if (inheritsRoles()) {
1✔
206
                        parents.addAll(this.recurseOverParents(parents));
1✔
207
                }
208
                return parents;
1✔
209
        }
210
        
211
        /**
212
         * Returns the full set of roles be looping over inherited roles. Duplicate roles are dropped.
213
         *
214
         * @param total Roles already looped over
215
         * @return Set&lt;Role&gt; Current and inherited roles
216
         */
217
        public Set<Role> recurseOverParents(final Set<Role> total) {
218
                if (!this.inheritsRoles()) {
1✔
UNCOV
219
                        return total;
×
220
                }
221

222
                Set<Role> allRoles = new HashSet<>(total);
1✔
223
                Set<Role> myRoles = new HashSet<>(this.getInheritedRoles());
1✔
224
                myRoles.removeAll(total);
1✔
225
                // prevent an obvious looping problem
226
                myRoles.remove(this); 
1✔
227
                allRoles.addAll(myRoles);
1✔
228
                
229
                for (Role r : myRoles) {
1✔
230
                        if (r.inheritsRoles()) {
1✔
231
                                allRoles.addAll(r.recurseOverParents(allRoles));
1✔
232
                        }
233
                }
1✔
234
                
235
                log.debug("Total roles: {}", allRoles);
1✔
236
                
237
                return allRoles;
1✔
238
        }
239
        
240
        /**
241
         * @since 1.5
242
         * @see org.openmrs.OpenmrsObject#getId()
243
         */
244
        @Override
245
        public Integer getId() {
UNCOV
246
                throw new UnsupportedOperationException();
×
247
        }
248
        
249
        /**
250
         * @since 1.5
251
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
252
         */
253
        @Override
254
        public void setId(Integer id) {
UNCOV
255
                throw new UnsupportedOperationException();
×
256
        }
257
        
258
        /**
259
         * @since 1.9
260
         * @return immediate children
261
         */
262
        public Set<Role> getChildRoles() {
263
                if (childRoles == null) {
1✔
264
                        childRoles = new HashSet<>();
1✔
265
                }
266
                return childRoles;
1✔
267
        }
268
        
269
        /**
270
         * @since 1.9
271
         * @param childRoles the immediate children to set
272
         */
273
        public void setChildRoles(Set<Role> childRoles) {
274
                this.childRoles = childRoles;
1✔
275
        }
1✔
276
        
277
        /**
278
         * Convenience method to test whether or not this role is a parent of another role
279
         *
280
         * @return true/false whether this role is a parent of another role
281
         * @since 1.9
282
         */
283
        public boolean hasChildRoles() {
284
                return getChildRoles() != null && !getChildRoles().isEmpty();
1✔
285
        }
286
        
287
        /**
288
         * Recursive (if need be) method to return all child roles of this role
289
         *
290
         * <strong>Should</strong> only return child roles
291
         * @return this role's children
292
         * @since 1.9
293
         */
294
        public Set<Role> getAllChildRoles() {
295
                Set<Role> children = new HashSet<>();
1✔
296
                if (hasChildRoles()) {
1✔
297
                        children.addAll(this.recurseOverChildren(children));
1✔
298
                }
299
                return children;
1✔
300
        }
301
        
302
        /**
303
         * Returns the full set of child roles be looping over children. Duplicate roles are dropped.
304
         *
305
         * @param total Roles already looped over
306
         * @return Set&lt;Role&gt; Current and child roles
307
         * @since 1.9
308
         */
309
        public Set<Role> recurseOverChildren(final Set<Role> total) {
310
                if (!this.hasChildRoles()) {
1✔
UNCOV
311
                        return total;
×
312
                }
313

314
                Set<Role> allRoles = new HashSet<>(total);
1✔
315

316
                Set<Role> myRoles = new HashSet<>(this.getChildRoles());
1✔
317
                myRoles.removeAll(total);
1✔
318
                // prevent an obvious looping problem
319
                myRoles.remove(this); 
1✔
320
                allRoles.addAll(myRoles);
1✔
321
                
322
                for (Role r : myRoles) {
1✔
323
                        if (r.hasChildRoles()) {
1✔
324
                                allRoles.addAll(r.recurseOverChildren(allRoles));
1✔
325
                        }
326
                }
1✔
327
                
328
                log.debug("Total roles: {}", allRoles);
1✔
329
                
330
                return allRoles;
1✔
331
        }
332
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc