KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > User


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb;
68
69 import org.hsqldb.HsqlNameManager.HsqlName;
70 import org.hsqldb.lib.HashSet;
71 import org.hsqldb.lib.IntValueHashMap;
72
73 // fredt@users 20021103 - patch 1.7.2 - fix bug in revokeAll()
74
// fredt@users 20021103 - patch 1.7.2 - allow for drop table, etc.
75
// when tables are dropped or renamed, changes are reflected in the
76
// permissions held in User objects.
77
// boucherb@users 200208-200212 - doc 1.7.2 - update
78
// boucherb@users 200208-200212 - patch 1.7.2 - metadata
79
// unsaved@users - patch 1.8.0 moved right managament to new classes
80

81 /**
82  * A User Object holds the name, password for a
83  * particular database user.<p>
84  *
85  * Enhanced in successive versions of HSQLDB.
86  *
87  * @author Thomas Mueller (Hypersonic SQL Group)
88  * @version 1.8.0
89  * @since Hypersonic SQL
90  */

91 public class User {
92
93     /** true if this user is the sys user. */
94     private boolean isSys;
95
96     /** true if this user is the public user. */
97     private boolean isPublic;
98
99     /** user name. */
100     private String JavaDoc sName;
101
102     /** password. */
103     private String JavaDoc sPassword;
104
105     /** default schema when new Sessions started (defaults to PUBLIC schema) */
106     private HsqlName initialSchema = null;
107
108     /** grantee object. */
109     private Grantee grantee;
110
111     /**
112      * Constructor
113      */

114     User(String JavaDoc name, String JavaDoc password,
115             Grantee inGrantee) throws HsqlException {
116
117         sName = name;
118         grantee = inGrantee;
119
120         boolean granteeOk = grantee != null
121                             || GranteeManager.isReserved(name);
122
123         if (!granteeOk) {
124             Trace.doAssert(false,
125                            Trace.getMessage(Trace.MISSING_GRANTEE) + ": "
126                            + name);
127         }
128
129         setPassword(password);
130
131         isSys = name.equals(GranteeManager.SYSTEM_AUTHORIZATION_NAME);
132         isPublic = name.equals(GranteeManager.PUBLIC_ROLE_NAME);
133     }
134
135     String JavaDoc getName() {
136         return sName;
137     }
138
139     void setPassword(String JavaDoc password) throws HsqlException {
140
141         // TODO:
142
// checkComplexity(password);
143
// requires: UserManager.createSAUser(), UserManager.createPublicUser()
144
sPassword = password;
145     }
146
147     /**
148      * Checks if this object's password attibute equals
149      * specified argument, else throws.
150      */

151     void checkPassword(String JavaDoc test) throws HsqlException {
152         Trace.check(test.equals(sPassword), Trace.ACCESS_IS_DENIED);
153     }
154
155     /**
156      * Returns true if this User object is for a user with the
157      * database administrator role.
158      */

159     boolean isSys() {
160         return isSys;
161     }
162
163     /**
164      * Returns the initial schema for the user
165      */

166     HsqlName getInitialSchema() {
167         return initialSchema;
168     }
169
170     /**
171      * This class does not have access to the SchemaManager, therefore
172      * caller should verify that the given schemaName exists.
173      *
174      * @param schemaName Name of an existing schema. Null value allowed,
175      * which means use the DB default session schema.
176      */

177     void setInitialSchema(HsqlName schema) {
178         initialSchema = schema;
179     }
180
181     /**
182      * Returns true if this User object represents the PUBLIC user
183      */

184     boolean isPublic() {
185         return isPublic;
186     }
187
188     /**
189      * Returns the ALTER USER DDL character sequence that preserves the
190      * this user's current password value and mode. <p>
191      *
192      * @return the DDL
193      */

194     String JavaDoc getAlterUserDDL() {
195
196         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
197
198         sb.append(Token.T_ALTER).append(' ');
199         sb.append(Token.T_USER).append(' ');
200         sb.append(sName).append(' ');
201         sb.append(Token.T_SET).append(' ');
202         sb.append(Token.T_PASSWORD).append(' ');
203         sb.append('"').append(sPassword).append('"');
204
205         return sb.toString();
206     }
207
208     /**
209      * returns the DDL string
210      * sequence that creates this user.
211      *
212      */

213     String JavaDoc getCreateUserDDL() {
214
215         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
216
217         sb.append(Token.T_CREATE).append(' ');
218         sb.append(Token.T_USER).append(' ');
219         sb.append(sName).append(' ');
220         sb.append(Token.T_PASSWORD).append(' ');
221         sb.append('"').append(sPassword).append('"');
222
223         return sb.toString();
224     }
225
226     /**
227      * Retrieves the redo log character sequence for connecting
228      * this user
229      *
230      * @return the redo log character sequence for connecting
231      * this user
232      */

233     public String JavaDoc getConnectStatement() {
234
235         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
236
237         sb.append(Token.T_CONNECT).append(' ');
238         sb.append(Token.T_USER).append(' ');
239         sb.append(sName);
240
241         return sb.toString();
242     }
243
244     /**
245      * Retrieves the Grantee object for this User.
246      */

247     Grantee getGrantee() {
248         return grantee;
249     }
250
251     /**
252      * Sets the Grantee object for this User.
253      * This is done in the constructor for all users except the special
254      * users SYSTEM and PUBLIC, which have to be set up before the
255      * Managers are initialized.
256      */

257     void setGrantee(Grantee inGrantee) throws HsqlException {
258
259         if (grantee != null) {
260             Trace.doAssert(false,
261                            Trace.getMessage(Trace.CHANGE_GRANTEE) + ": "
262                            + sName);
263         }
264
265         grantee = inGrantee;
266     }
267
268     // Legacy wrappers
269

270     /**
271      * Returns true if this User object is for a user with the
272      * database administrator role.
273      */

274     boolean isAdmin() {
275         return grantee.isAdmin();
276     }
277
278     /**
279      * Retrieves a string[] whose elements are the names, of the rights
280      * explicitly granted with the GRANT command to this <code>User</code>
281      * object on the <code>Table</code> object identified by the
282      * <code>name</code> argument.
283      * * @return array of Strings naming the rights granted to this
284      * <code>User</code> object on the <code>Table</code> object
285      * identified by the <code>name</code> argument.
286      * @param name a <code>Table</code> object identifier
287      *
288      */

289     String JavaDoc[] listGrantedTablePrivileges(HsqlName name) {
290         return grantee.listGrantedTablePrivileges(name);
291     }
292
293     /**
294      * Retrieves the distinct set of Java <code>Class</code> FQNs
295      * for which this <code>User</code> object has been
296      * granted <code>ALL</code> (the Class execution privilege). <p>
297      * @param andToPublic if <code>true</code>, then the set includes the
298      * names of classes accessible to this <code>User</code> object
299      * through grants to its <code>PUBLIC</code> <code>User</code>
300      * object attribute, else only direct grants are inlcuded.
301      * @return the distinct set of Java Class FQNs for which this
302      * this <code>User</code> object has been granted
303      * <code>ALL</code>.
304      *
305      */

306     HashSet getGrantedClassNames(boolean andToPublic) throws HsqlException {
307         return grantee.getGrantedClassNames(andToPublic);
308     }
309
310     /**
311      * Retrieves the map object that represents the rights that have been
312      * granted on database objects. <p>
313      *
314      * The map has keys and values with the following interpretation: <P>
315      *
316      * <UL>
317      * <LI> The keys are generally (but not limited to) objects having
318      * an attribute or value equal to the name of an actual database
319      * object.
320      *
321      * <LI> Specifically, the keys act as database object identifiers.
322      *
323      * <LI> The values are always Integer objects, each formed by combining
324      * a set of flags, one for each of the access rights defined in
325      * UserManager: {SELECT, INSERT, UPDATE and DELETE}.
326      * </UL>
327      */

328     IntValueHashMap getRights() {
329         return grantee.getRights();
330     }
331
332     /**
333      * Checks that this User object is for a user with the
334      * database administrator role. Otherwise it throws.
335      */

336     void checkAdmin() throws HsqlException {
337         grantee.checkAdmin();
338     }
339
340     /**
341      * Checks if any of the rights represented by the rights
342      * argument have been granted on the specified database object. <p>
343      *
344      * This is done by checking that a mapping exists in the rights map
345      * from the dbobject argument for at least one of the rights
346      * contained in the rights argument. Otherwise, it throws.
347      */

348     void check(HsqlName dbobject, int rights) throws HsqlException {
349         grantee.check(dbobject, rights);
350     }
351
352     void check(String JavaDoc dbobject) throws HsqlException {
353         grantee.check(dbobject);
354     }
355
356     /**
357      * Returns true if any of the rights represented by the
358      * rights argument has been granted on the database object identified
359      * by the dbobject argument. <p>
360      *
361      * This is done by checking that a mapping exists in the rights map
362      * from the dbobject argument for at least one of the rights
363      * contained in the rights argument.
364      */

365     boolean isAccessible(HsqlName dbobject, int rights) throws HsqlException {
366         return grantee.isAccessible(dbobject, rights);
367     }
368
369     /**
370      * Returns true if any right at all has been granted to this User object
371      * on the database object identified by the dbobject argument.
372      */

373     boolean isAccessible(String JavaDoc dbobject) throws HsqlException {
374         return grantee.isAccessible(dbobject);
375     }
376
377     boolean isAccessible(HsqlName dbobject) throws HsqlException {
378         return grantee.isAccessible(dbobject);
379     }
380 }
381
Popular Tags