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

openmrs / openmrs-core / 33871974617

04 Sep 2026 12:16PM UTC coverage: 64.837% (+0.01%) from 64.827%
33871974617

push

github

ibacher
TRUNK-6698 & TRUNK-6776: Cache @Authorized metadata and add tests for class hierarchy (#6508)

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

31 existing lines in 9 files now uncovered.

25046 of 38629 relevant lines covered (64.84%)

0.65 hits per line

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

0.0
/api/src/main/java/org/openmrs/annotation/AuthorizedAnnotationAttributes.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.annotation;
11

12
import java.lang.annotation.Annotation;
13
import java.lang.reflect.Field;
14
import java.lang.reflect.Method;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.HashSet;
18
import java.util.Set;
19

20
/**
21
 * Annotation attributes metadata implementation used for authorization method interception.
22
 * <p>
23
 * This <code>Attributes</code> implementation will return security configuration for classes
24
 * described using the <code>Secured</code> Java 5 annotation.
25
 * <p>
26
 * The <code>SecurityAnnotationAttributes</code> implementation can be used to configure a
27
 * <code>MethodDefinitionAttributes</code> and <code>MethodSecurityInterceptor</code> bean
28
 * definition (see below).
29
 * <p>
30
 * For example:
31
 * 
32
 * <pre>
33
 * &lt;bean id="attributes" 
34
 *     class="org.acegisecurity.annotation.SecurityAnnotationAttributes"/&gt;
35
 * 
36
 * &lt;bean id="objectDefinitionSource" 
37
 *     class="org.acegisecurity.intercept.method.MethodDefinitionAttributes"&gt;
38
 *     &lt;property name="attributes"&gt;
39
 *         &lt;ref local="attributes"/&gt;
40
 *     &lt;/property&gt;
41
 * &lt;/bean&gt;
42
 * 
43
 * &lt;bean id="securityInterceptor" 
44
 *     class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
45
 *      . . .
46
 *      &lt;property name="objectDefinitionSource"&gt;
47
 *          &lt;ref local="objectDefinitionSource"/&gt;
48
 *      &lt;/property&gt;
49
 * &lt;/bean&gt;
50
 * </pre>
51
 * <p>
52
 * These security annotations are similar to the Commons Attributes approach, however they are
53
 * using Java 5 language-level metadata support.
54
 * 
55
 * @see org.openmrs.annotation.Authorized
56
 */
UNCOV
57
public class AuthorizedAnnotationAttributes {
×
58
        
59
        private static final String UNSUPPORTED_OPERATION = "Unsupported operation";
60
        
61
        /**
62
         * Get the <code>Secured</code> attributes for a given target class.
63
         * 
64
         * @param target The target method
65
         * @return Collection of <code>SecurityConfig</code>
66
         */
67
        public Collection<String> getAttributes(Class<?> target) {
68
                Set<String> attributes = new HashSet<>();
×
69
                for (Annotation annotation : target.getAnnotations()) {
×
70
                        // check for Secured annotations
71
                        if (annotation instanceof Authorized) {
×
72
                                Authorized attr = (Authorized) annotation;
×
73
                                Collections.addAll(attributes, attr.value());
×
74
                                break;
×
75
                        }
76
                }
77
                return attributes;
×
78
        }
79
        
80
        /**
81
         * Get the <code>Secured</code> attributes for a given target method.
82
         * 
83
         * @param method The target method
84
         * @return Collection of <code>SecurityConfig</code>
85
         */
86
        public Collection<String> getAttributes(Method method) {
UNCOV
87
                Set<String> attributes = new HashSet<>();
×
88
                
UNCOV
89
                for (Annotation annotation : method.getAnnotations()) {
×
90
                        // check for Secured annotations
UNCOV
91
                        if (annotation instanceof Authorized) {
×
UNCOV
92
                                Authorized attr = (Authorized) annotation;
×
UNCOV
93
                                Collections.addAll(attributes, attr.value());
×
UNCOV
94
                                break;
×
95
                        }
96
                }
UNCOV
97
                return attributes;
×
98
        }
99
        
100
        /**
101
         * Returns whether or not to require that the user have all of the privileges in order to be
102
         * "authorized" for this class
103
         * 
104
         * @param target the class to act on
105
         * @return boolean true/false whether to "and" privileges together
106
         * @see org.openmrs.annotation.Authorized#requireAll()
107
         */
108
        public boolean getRequireAll(Class<?> target) {
109
                for (Annotation annotation : target.getAnnotations()) {
×
110
                        // check for Secured annotations
111
                        if (annotation instanceof Authorized) {
×
112
                                Authorized attr = (Authorized) annotation;
×
113
                                return attr.requireAll();
×
114
                        }
115
                }
116
                return false;
×
117
        }
118
        
119
        /**
120
         * Returns whether or not to require that the user have all of the privileges in order to be
121
         * "authorized" for this method
122
         * 
123
         * @param method
124
         * @return boolean true/false whether to "and" privileges together
125
         * @see org.openmrs.annotation.Authorized#requireAll()
126
         */
127
        public boolean getRequireAll(Method method) {
UNCOV
128
                for (Annotation annotation : method.getAnnotations()) {
×
129
                        // check for Secured annotations
UNCOV
130
                        if (annotation instanceof Authorized) {
×
UNCOV
131
                                Authorized attr = (Authorized) annotation;
×
UNCOV
132
                                return attr.requireAll();
×
133
                        }
134
                }
UNCOV
135
                return false;
×
136
        }
137
        
138
        /**
139
         * Determine if this method has the @Authorized annotation even on it
140
         * 
141
         * @param method
142
         * @return boolean true/false whether this method is annotated for OpenMRS
143
         */
144
        public boolean hasAuthorizedAnnotation(Method method) {
UNCOV
145
                for (Annotation annotation : method.getAnnotations()) {
×
146
                        // check for Secured annotations
UNCOV
147
                        if (annotation instanceof Authorized) {
×
UNCOV
148
                                return true;
×
149
                        }
150
                }
151
                
UNCOV
152
                return false;
×
153
        }
154
        
155
        public Collection<?> getAttributes(Class<?> clazz, Class<?> filter) {
156
                throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
×
157
        }
158
        
159
        public Collection<?> getAttributes(Method method, Class<?> clazz) {
160
                throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
×
161
        }
162
        
163
        public Collection<?> getAttributes(Field field) {
164
                throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
×
165
        }
166
        
167
        public Collection<?> getAttributes(Field field, Class<?> clazz) {
168
                throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
×
169
        }
170
        
171
}
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