11package restx .security ;
22
33import com .google .common .base .Optional ;
4+ import restx .factory .Component ;
45
56import java .util .Arrays ;
67import java .util .Map ;
1112 * Provides a set of useful permissions, including the OPEN permission which is the only one that can allow access
1213 * to a resource without being authenticated.
1314 */
14- public class Permissions {
15+ @ Component
16+ public class PermissionFactory {
1517 private static final Pattern ROLE_PARAM_INTERPOLATOR_REGEX = Pattern .compile ("\\ {(.+?)\\ }" );
1618
1719 private static final Permission OPEN = new Permission () {
@@ -40,23 +42,30 @@ public String toString() {
4042 /**
4143 * This is the only permission that can allow access to a resource without being authenticated.
4244 */
43- public static Permission open () {
45+ public Permission open () {
4446 return OPEN ;
4547 }
4648
4749 /**
4850 * This is the most basic permission which is true as soon as a principal is authenticated.
4951 */
50- public static Permission isAuthenticated () {
52+ public Permission isAuthenticated () {
5153 return IS_AUTHENTICATED ;
5254 }
5355
56+ public boolean isOpen (Permission permission ) {
57+ return permission == open ();
58+ }
59+
60+ public boolean isIsAuthenticated (Permission permission ) {
61+ return permission == isAuthenticated ();
62+ }
5463
5564 /**
5665 * This permission is true as soon as the principal has the given role
5766 * @param role the role to check
5867 */
59- public static Permission hasRole (final String role ) {
68+ public Permission hasRole (final String role ) {
6069 return new Permission () {
6170 public final String TO_STRING = "HAS_ROLE[" + role + "]" ;
6271
@@ -81,7 +90,7 @@ public String toString() {
8190 };
8291 }
8392
84- protected static String interpolateRole (String role , Map <String , String > roleInterpolationMap ) {
93+ protected String interpolateRole (String role , Map <String , String > roleInterpolationMap ) {
8594 Matcher matcher = ROLE_PARAM_INTERPOLATOR_REGEX .matcher (role );
8695 StringBuffer interpolatedRole = new StringBuffer ();
8796 while (matcher .find ()){
@@ -99,7 +108,7 @@ protected static String interpolateRole(String role, Map<String, String> roleInt
99108 /**
100109 * A compound permission which is true if any of the underlying permissions is true
101110 */
102- public static Permission anyOf (final Permission ... permissions ) {
111+ public Permission anyOf (final Permission ... permissions ) {
103112 return new Permission () {
104113 @ Override
105114 public Optional <? extends Permission > has (RestxPrincipal principal , Map <String , String > roleInterpolationMap ) {
@@ -123,7 +132,7 @@ public String toString() {
123132 /**
124133 * A compound permission which is true if all underlying permissions are true
125134 */
126- public static Permission allOf (final Permission ... permissions ) {
135+ public Permission allOf (final Permission ... permissions ) {
127136 return new Permission () {
128137 @ Override
129138 public Optional <? extends Permission > has (RestxPrincipal principal , Map <String , String > roleInterpolationMap ) {
0 commit comments