View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.security;
20  
21  /**
22   * 
23   * @author cagatay
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 }