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

openmrs / openmrs-core / 43502855768

13 Nov 2026 08:33PM UTC coverage: 67.328% (+0.4%) from 66.969%
43502855768

push

github

ibacher
TRUNK-6616: Count obs version history statements without the privilege check

getObsVersionHistory_shouldLoadTheWholeChainInTwoQueries counted the
statements of the service call, which include its privilege check. When
test setup has cleared the role privilege cache in the same transaction,
that check reads roles from the database, so the count depended on test
order. Measure the DAO call instead.

26896 of 39948 relevant lines covered (67.33%)

0.67 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() {
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
                                }
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 ✔
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() {
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) {
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
         * Child roles are read-only: saving a role never writes them. To make a role inherit from this one,
272
         * add this role to that role's {@link #getInheritedRoles() inherited roles} and save that role.
273
         *
274
         * @since 1.9
275
         * @param childRoles the immediate children to set
276
         */
277
        public void setChildRoles(Set<Role> childRoles) {
278
                this.childRoles = CollectionUtil.setCollection(this.childRoles, childRoles);
1 ✔
279
        }
1 ✔
280
        
281
        /**
282
         * Convenience method to test whether or not this role is a parent of another role
283
         *
284
         * @return true/false whether this role is a parent of another role
285
         * @since 1.9
286
         */
287
        public boolean hasChildRoles() {
288
                return getChildRoles() != null && !getChildRoles().isEmpty();
1 ✔
289
        }
290
        
291
        /**
292
         * Recursive (if need be) method to return all child roles of this role
293
         *
294
         * <strong>Should</strong> only return child roles
295
         * @return this role's children
296
         * @since 1.9
297
         */
298
        public Set<Role> getAllChildRoles() {
299
                Set<Role> children = new HashSet<>();
1 ✔
300
                if (hasChildRoles()) {
1 ✔
301
                        children.addAll(this.recurseOverChildren(children));
1 ✔
302
                }
303
                return children;
1 ✔
304
        }
305
        
306
        /**
307
         * Returns the full set of child roles be looping over children. Duplicate roles are dropped.
308
         *
309
         * @param total Roles already looped over
310
         * @return Set&lt;Role&gt; Current and child roles
311
         * @since 1.9
312
         */
313
        public Set<Role> recurseOverChildren(final Set<Role> total) {
314
                if (!this.hasChildRoles()) {
1 ✔
315
                        return total;
×
316
                }
317

318
                Set<Role> allRoles = new HashSet<>(total);
1 ✔
319

320
                Set<Role> myRoles = new HashSet<>(this.getChildRoles());
1 ✔
321
                myRoles.removeAll(total);
1 ✔
322
                // prevent an obvious looping problem
323
                myRoles.remove(this); 
1 ✔
324
                allRoles.addAll(myRoles);
1 ✔
325
                
326
                for (Role r : myRoles) {
1 ✔
327
                        if (r.hasChildRoles()) {
1 ✔
328
                                allRoles.addAll(r.recurseOverChildren(allRoles));
1 ✔
329
                        }
330
                }
1 ✔
331
                
332
                log.debug("Total roles: {}", allRoles);
1 ✔
333
                
334
                return allRoles;
1 ✔
335
        }
336
}
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