View Javadoc

1   /*
2    * RolesPermissionsAdapter.java
3    *
4    * Created on December 7, 2002 2:25 PM
5    */
6   
7   package net.sf.navigator.menu;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import java.util.regex.Pattern;
11  
12  /**
13   * This class used container-managed security to check access
14   * to menus.  The roles are set in menu-config.xml.
15   *
16   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
17   */
18  public class RolesPermissionsAdapter implements PermissionsAdapter {
19      private Pattern delimiters = Pattern.compile("(?<!\\\\),");
20      private HttpServletRequest request;
21  
22      public RolesPermissionsAdapter(HttpServletRequest request) {
23          this.request = request;
24      }
25  
26      /**
27       * If the menu is allowed, this should return true.
28       *
29       * @return whether or not the menu is allowed.
30       */
31      public boolean isAllowed(MenuComponent menu) {
32          if (menu.getRoles() == null) {
33              return true; // no roles define, allow everyone
34          } else {
35              // Get the list of roles this menu allows
36              String[] allowedRoles = delimiters.split(menu.getRoles());
37              for (int i=0; i < allowedRoles.length; i++) {
38                  if (request.isUserInRole(allowedRoles[i])) {
39                      return true;
40                  }
41              }
42          }
43          return false;
44      }
45  
46  
47  }