View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/GrantedPrivileges.java,v 1.8 2004/10/25 20:41:49 hkollmann Exp $
3    * $Revision: 1.8 $
4    * $Date: 2004/10/25 20:41:49 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.config;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.util.StringUtil;
30  
31  import java.util.Vector;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  
36  
37  /***
38   * <p>
39   * This class represents a "granted-privileges"-tag in dbforms-config.xml
40   * </p>
41   *
42   * @author Joachim Peer
43   */
44  public class GrantedPrivileges implements java.io.Serializable {
45     private static Log logCat = LogFactory.getLog(GrantedPrivileges.class
46                                                   .getName()); // logging category for this class
47  
48     /*** DOCUMENT ME! */
49     public static final int PRIVILEG_SELECT = 0;
50  
51     /*** DOCUMENT ME! */
52     public static final int PRIVILEG_INSERT = 1;
53  
54     /*** DOCUMENT ME! */
55     public static final int PRIVILEG_UPDATE = 2;
56  
57     /*** DOCUMENT ME! */
58     public static final int PRIVILEG_DELETE = 3;
59     private Vector[]        grantedRoles;
60  
61     /***
62      * Creates a new GrantedPrivileges object.
63      */
64     public GrantedPrivileges() {
65        grantedRoles = new Vector[4];
66  
67        //conditions = new Hashtable();
68     }
69  
70     /***
71      * DOCUMENT ME!
72      *
73      * @param delete DOCUMENT ME!
74      */
75     public void setDelete(String delete) {
76        logCat.info("delete");
77        grantedRoles[PRIVILEG_DELETE] = StringUtil.splitString(delete, ",;~");
78     }
79  
80  
81     /***
82      * DOCUMENT ME!
83      *
84      * @param insert DOCUMENT ME!
85      */
86     public void setInsert(String insert) {
87        logCat.info("insert");
88        grantedRoles[PRIVILEG_INSERT] = StringUtil.splitString(insert, ",;~");
89     }
90  
91  
92     /***
93      * DOCUMENT ME!
94      *
95      * @param select DOCUMENT ME!
96      */
97     public void setSelect(String select) {
98        logCat.info("select");
99        grantedRoles[PRIVILEG_SELECT] = StringUtil.splitString(select, ",;~");
100    }
101 
102 
103    /***
104     * DOCUMENT ME!
105     *
106     * @param update DOCUMENT ME!
107     */
108    public void setUpdate(String update) {
109       logCat.info("update");
110       grantedRoles[PRIVILEG_UPDATE] = StringUtil.splitString(update, ",;~");
111    }
112 
113 
114    /***
115     * DOCUMENT ME!
116     *
117     * @param request DOCUMENT ME!
118     * @param privileg DOCUMENT ME!
119     *
120     * @return DOCUMENT ME!
121     */
122    public boolean hasUserPrivileg(HttpServletRequest request,
123                                   int                privileg) {
124       if (grantedRoles[privileg] == null) {
125          return true; // if no constraints specified -> wildcard access ;-)
126       }
127 
128       for (int i = 0; i < grantedRoles[privileg].size(); i++) {
129          String aGrantedRole = (String) grantedRoles[privileg].elementAt(i);
130 
131          if (request.isUserInRole(aGrantedRole)) {
132             return true; // if the user is InRole(aGrantedRole) then let him go on :=)
133          }
134       }
135 
136       return false; // otherwise we must deny the operation
137    }
138 }