View Javadoc

1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package net.sf.fridaymvc.security.accesscontroller;
18  
19  import java.util.Iterator;
20  import java.util.Set;
21  
22  import net.sf.fridaymvc.RunData;
23  
24  /***
25   * @author <a href="mailto:arto.pastinen@ofw.fi">Arto Pastinen</a>
26   * @version $Id: AccessController.java,v 1.2 2004/11/23 20:35:58 artsi Exp $
27   */
28  
29  public class AccessController {
30      public boolean isLogged() {
31          return (RunData.getInstance().getUser() == null) ? false : true;
32      }
33      
34      public boolean hasRole(String roleName) {
35          User user = RunData.getInstance().getUser();
36          if(user == null) return false;
37          Set roles = user.getRoles();
38          for (Iterator iter = roles.iterator(); iter.hasNext();) {
39              String tmp = ((Role) iter.next()).getName();                    
40              if(tmp != null && tmp.equals(roleName)) return true;            
41          }
42          return false;
43      }
44      
45      public User getUser() {
46          return RunData.getInstance().getUser();
47      }
48      
49      public boolean hasPermission(String permissionName) {
50          User user = RunData.getInstance().getUser();
51          if(user == null) return false;
52          Set roles = user.getRoles();
53          if(roles == null) return false;
54          for (Iterator iter = roles.iterator(); iter.hasNext();) {
55              Role role = (Role) iter.next();
56              Set permissions = role.getPermissions();
57              if(permissions == null) continue;
58              for (Iterator iterator = permissions.iterator(); iterator.hasNext();) {
59                  Permission permission = (Permission) iterator.next();
60                  if(permission.getName().equals(permissionName)) return true;
61              }
62          }
63          return false;
64      }
65  }