UserPermissions.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: UserPermissions.java,v 1.3 2005/10/10 18:02:49 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.auth;
  22. import java.beans.PropertyChangeListener;
  23. import java.beans.PropertyChangeSupport;
  24. /**
  25.  * This is a singleton that marks the set of permissions for a given logged in user.
  26.  * It is one of the optional results of a successful login operation.
  27.  * The purpose of this class is to provide a central location and client side bridge
  28.  * to the server side permissions and user roles (see J2EE role based authorization).
  29.  * This class is used by gui widgets and actions to determine visibility and enabled
  30.  * status and thus a UI can adapt itself to users with a lower set of privileges.
  31.  *
  32.  * This class is not meant as a secure barrier! It is only a thin layer to supplant the
  33.  * server side permissions. This class can be compromized by the user and thus its purpose
  34.  * is only to help UI flow and navigation and not to prevent attack against a client side
  35.  * UI. A server implementation must ALWAYS recheck permissions sent by the client regardless
  36.  * of the client.
  37.  *
  38.  * @author Shai Almog
  39.  */
  40. public class UserPermissions {
  41.     private static final UserPermissions INSTANCE = new UserPermissions();
  42.     private PropertyChangeSupport propertyChange = new PropertyChangeSupport(this);
  43.     private String[] roles;
  44.     
  45.     /** Creates a new instance of UserPermissions */
  46.     private UserPermissions() {
  47.     }
  48.     
  49.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  50.         propertyChange.addPropertyChangeListener(listener);
  51.     }
  52.     public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
  53.         propertyChange.addPropertyChangeListener(name, listener);
  54.     }
  55.     
  56.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  57.         propertyChange.removePropertyChangeListener(listener);
  58.     }
  59.     public void removePropertyChangeListener(String name, PropertyChangeListener listener) {
  60.         propertyChange.removePropertyChangeListener(name, listener);
  61.     }
  62.     /**
  63.      * Returns the singleton instance of this class. A singleton is used to simplify access for
  64.      * the permissions from every point in the application.
  65.      */
  66.     public static UserPermissions getInstance() {
  67.         return INSTANCE;
  68.     }
  69.     
  70.     /**
  71.      * Returns the roles of the currently logged in user
  72.      */
  73.     public String[] getRoles() {
  74.         return roles;
  75.     }
  76.     
  77.     /**
  78.      * Returns true if the user is in the given role (case sensitive).
  79.      */
  80.     public boolean isUserInRole(String role) {
  81.         if(roles != null) {
  82.             for(int iter = 0 ; iter < roles.length ; iter++) {
  83.                 if(roles[iter].equals(role)) {
  84.                     return true;
  85.                 }
  86.             }
  87.         } 
  88.         return false;
  89.     }
  90.     /**
  91.      * Returns true if the user is in one of the given roles (case sensitive).
  92.      */
  93.     public boolean isUserInARole(String[] roles) {
  94.         for(int iter = 0 ; iter < roles.length ; iter++) {
  95.             if(isUserInRole(roles[iter])) {
  96.                 return true;
  97.             }
  98.         }
  99.         return false;
  100.     }
  101.     /**
  102.      * Returns true if the user is in all of the given roles (case sensitive).
  103.      */
  104.     public boolean isUserInRoles(String[] roles) {
  105.         for(int iter = 0 ; iter < roles.length ; iter++) {
  106.             if(!isUserInRole(roles[iter])) {
  107.                 return false;
  108.             }
  109.         }
  110.         return true;
  111.     }
  112.     
  113.     void setRoles(String[] roles) {
  114.         String[] oldValue = this.roles;
  115.         this.roles = roles;
  116.         propertyChange.firePropertyChange("roles", oldValue, roles);
  117.     }
  118. }