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

openmrs / openmrs-core / 41717837886

03 Nov 2026 08:59PM UTC coverage: 68.971% (+0.05%) from 68.922%
41717837886

push

github

ibacher
TRUNK-6863: Remove the generation token before clearing a node's cache locally (#6791)

1 of 1 new or added line in 1 file covered. (100.0%)

413 existing lines in 30 files now uncovered.

26445 of 38342 relevant lines covered (68.97%)

0.69 hits per line

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

93.1
/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.CollectionUtil;
21
import org.openmrs.util.RoleConstants;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

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

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

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

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

© 2026 Coveralls, Inc