1
1
package restx .security ;
2
2
3
3
import com .google .common .base .Optional ;
4
+ import restx .factory .Component ;
4
5
5
6
import java .util .Arrays ;
6
7
import java .util .Map ;
11
12
* Provides a set of useful permissions, including the OPEN permission which is the only one that can allow access
12
13
* to a resource without being authenticated.
13
14
*/
14
- public class Permissions {
15
+ @ Component
16
+ public class PermissionFactory {
15
17
private static final Pattern ROLE_PARAM_INTERPOLATOR_REGEX = Pattern .compile ("\\ {(.+?)\\ }" );
16
18
17
19
private static final Permission OPEN = new Permission () {
@@ -40,23 +42,30 @@ public String toString() {
40
42
/**
41
43
* This is the only permission that can allow access to a resource without being authenticated.
42
44
*/
43
- public static Permission open () {
45
+ public Permission open () {
44
46
return OPEN ;
45
47
}
46
48
47
49
/**
48
50
* This is the most basic permission which is true as soon as a principal is authenticated.
49
51
*/
50
- public static Permission isAuthenticated () {
52
+ public Permission isAuthenticated () {
51
53
return IS_AUTHENTICATED ;
52
54
}
53
55
56
+ public boolean isOpen (Permission permission ) {
57
+ return permission == open ();
58
+ }
59
+
60
+ public boolean isIsAuthenticated (Permission permission ) {
61
+ return permission == isAuthenticated ();
62
+ }
54
63
55
64
/**
56
65
* This permission is true as soon as the principal has the given role
57
66
* @param role the role to check
58
67
*/
59
- public static Permission hasRole (final String role ) {
68
+ public Permission hasRole (final String role ) {
60
69
return new Permission () {
61
70
public final String TO_STRING = "HAS_ROLE[" + role + "]" ;
62
71
@@ -81,7 +90,7 @@ public String toString() {
81
90
};
82
91
}
83
92
84
- protected static String interpolateRole (String role , Map <String , String > roleInterpolationMap ) {
93
+ protected String interpolateRole (String role , Map <String , String > roleInterpolationMap ) {
85
94
Matcher matcher = ROLE_PARAM_INTERPOLATOR_REGEX .matcher (role );
86
95
StringBuffer interpolatedRole = new StringBuffer ();
87
96
while (matcher .find ()){
@@ -99,7 +108,7 @@ protected static String interpolateRole(String role, Map<String, String> roleInt
99
108
/**
100
109
* A compound permission which is true if any of the underlying permissions is true
101
110
*/
102
- public static Permission anyOf (final Permission ... permissions ) {
111
+ public Permission anyOf (final Permission ... permissions ) {
103
112
return new Permission () {
104
113
@ Override
105
114
public Optional <? extends Permission > has (RestxPrincipal principal , Map <String , String > roleInterpolationMap ) {
@@ -123,7 +132,7 @@ public String toString() {
123
132
/**
124
133
* A compound permission which is true if all underlying permissions are true
125
134
*/
126
- public static Permission allOf (final Permission ... permissions ) {
135
+ public Permission allOf (final Permission ... permissions ) {
127
136
return new Permission () {
128
137
@ Override
129
138
public Optional <? extends Permission > has (RestxPrincipal principal , Map <String , String > roleInterpolationMap ) {
0 commit comments