1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.security;
20
21
22
23
24
25 public abstract class SecurityContext {
26
27 public final static int AUTH_MODE_NONE = -1;
28 public final static int AUTH_MODE_SINGLE = 0;
29 public final static int AUTH_MODE_ALL = 1;
30 public final static int AUTH_MODE_ANY = 2;
31 public final static int AUTH_MODE_NOT = 3;
32
33 private String[] roles;
34 private int authMode = AUTH_MODE_NONE;
35
36 public abstract String getAuthType();
37
38 public abstract String getRemoteUser();
39
40 public abstract boolean ifGranted(String role);
41
42 boolean ifSingleGranted() {
43 return ifGranted(roles[0]);
44 }
45
46 boolean ifAllGranted() {
47 boolean isAuthorized = false;
48 for (int i = 0; i < roles.length; i++) {
49 String role = roles[i];
50 if(ifGranted(role)) {
51 isAuthorized = true;
52 } else {
53 isAuthorized = false;
54 break;
55 }
56 }
57 return isAuthorized;
58 }
59
60 boolean ifAnyGranted() {
61 boolean isAuthorized = false;
62 for (int i = 0; i < roles.length; i++) {
63 String role = roles[i];
64 if(ifGranted(role)) {
65 isAuthorized = true;
66 break;
67 }
68 }
69 return isAuthorized;
70 }
71
72 boolean ifNotGranted() {
73 boolean isAuthorized = false;
74 for (int i = 0; i < roles.length; i++) {
75 String role = roles[i];
76 if(ifGranted(role)) {
77 isAuthorized = false;
78 break;
79 } else {
80 isAuthorized = true;
81 }
82 }
83 return isAuthorized;
84 }
85
86 boolean inAuthMode() {
87 return authMode != AUTH_MODE_NONE;
88 }
89
90 int getAuthMode() {
91 return authMode;
92 }
93 void setAuthMode(int authMode) {
94 this.authMode = authMode;
95 }
96
97 String[] getRoles() {
98 return roles;
99 }
100 void setRoles(String[] roles) {
101 this.roles = roles;
102 }
103 }