KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > PrivManager


1 /**
2  * com.mckoi.database.interpret.PrivManager 21 Aug 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * Handler for grant/revoke queries for setting up grant information in the
33  * database.
34  *
35  * @author Tobias Downer
36  */

37
38 public class PrivManager extends Statement {
39
40   
41   // ---------- Implemented from Statement ----------
42

43   public void prepare() throws DatabaseException {
44     // Nothing to do here
45
}
46
47   public Table evaluate() throws DatabaseException {
48     
49     DatabaseQueryContext context = new DatabaseQueryContext(database);
50
51     String JavaDoc command_type = (String JavaDoc) cmd.getObject("command");
52
53     ArrayList JavaDoc priv_list = (ArrayList JavaDoc) cmd.getObject("priv_list");
54     String JavaDoc priv_object = (String JavaDoc) cmd.getObject("priv_object");
55     
56     int grant_object;
57     String JavaDoc grant_param;
58     
59     // Parse the priv object,
60
if (priv_object.startsWith("T:")) {
61       // Granting to a table object
62
String JavaDoc table_name_str = priv_object.substring(2);
63       TableName table_name = database.resolveTableName(table_name_str);
64       // Check the table exists
65
if (!database.tableExists(table_name)) {
66         throw new DatabaseException("Table '" +
67                                     table_name + "' doesn't exist.");
68       }
69       grant_object = GrantManager.TABLE;
70       grant_param = table_name.toString();
71     }
72     else if (priv_object.startsWith("S:")) {
73       // Granting to a schema object
74
String JavaDoc schema_name_str = priv_object.substring(2);
75       SchemaDef schema_name = database.resolveSchemaName(schema_name_str);
76       // Check the schema exists
77
if (schema_name == null ||
78           !database.schemaExists(schema_name.toString())) {
79         schema_name_str = schema_name == null ? schema_name_str :
80                                                 schema_name.toString();
81         throw new DatabaseException("Schema '" + schema_name_str +
82                                     "' doesn't exist.");
83       }
84       grant_object = GrantManager.SCHEMA;
85       grant_param = schema_name.toString();
86     }
87     else {
88       throw new Error JavaDoc("Priv object formatting error.");
89     }
90     
91     if (command_type.equals("GRANT")) {
92       ArrayList JavaDoc grant_to = (ArrayList JavaDoc) cmd.getObject("grant_to");
93       boolean grant_option = cmd.getBoolean("grant_option");
94
95       // Get the grant manager.
96
GrantManager manager = context.getGrantManager();
97
98       // Get the grant options this user has on the given object.
99
Privileges options_privs = manager.userGrantOptions(
100                                grant_object, grant_param, user.getUserName());
101
102       // Is the user permitted to give out these privs?
103
Privileges grant_privs = Privileges.EMPTY_PRIVS;
104       for (int i = 0; i < priv_list.size(); ++i) {
105         String JavaDoc priv = ((String JavaDoc) priv_list.get(i)).toUpperCase();
106         int priv_bit;
107         if (priv.equals("ALL")) {
108           if (grant_object == GrantManager.TABLE) {
109             priv_bit = Privileges.TABLE_ALL_PRIVS.toInt();
110           }
111           else if (grant_object == GrantManager.SCHEMA) {
112             priv_bit = Privileges.SCHEMA_ALL_PRIVS.toInt();
113           }
114           else {
115             throw new Error JavaDoc("Unrecognised grant object.");
116           }
117         }
118         else {
119           priv_bit = Privileges.parseString(priv);
120         }
121         if (!options_privs.permits(priv_bit)) {
122           throw new UserAccessException(
123                 "User is not permitted to grant '" + priv +
124                 "' access on object " + grant_param);
125         }
126         grant_privs = grant_privs.add(priv_bit);
127       }
128
129       // Do the users exist?
130
for (int i = 0; i < grant_to.size(); ++i) {
131         String JavaDoc name = (String JavaDoc) grant_to.get(i);
132         if (!name.equalsIgnoreCase("public") &&
133             !database.getDatabase().userExists(context, name)) {
134           throw new DatabaseException("User '" + name + "' doesn't exist.");
135         }
136       }
137
138       // Everything checks out so add the grants to the users.
139
for (int i = 0; i < grant_to.size(); ++i) {
140         String JavaDoc name = (String JavaDoc) grant_to.get(i);
141         if (name.equalsIgnoreCase("public")) {
142           // Add a public grant,
143
manager.addGrant(grant_privs, grant_object, grant_param,
144                            GrantManager.PUBLIC_USERNAME_STR,
145                            grant_option, user.getUserName());
146         }
147         else {
148           // Add a user grant.
149
manager.addGrant(grant_privs, grant_object, grant_param,
150                            name, grant_option, user.getUserName());
151         }
152       }
153       
154       // All done.
155

156     }
157     else if (command_type.equals("REVOKE")) {
158       ArrayList JavaDoc revoke_from = (ArrayList JavaDoc) cmd.getObject("revoke_from");
159       boolean revoke_grant_option = cmd.getBoolean("revoke_grant_option");
160       
161       // Get the grant manager.
162
GrantManager manager = context.getGrantManager();
163
164       // Is the user permitted to give out these privs?
165
Privileges revoke_privs = Privileges.EMPTY_PRIVS;
166       for (int i = 0; i < priv_list.size(); ++i) {
167         String JavaDoc priv = ((String JavaDoc) priv_list.get(i)).toUpperCase();
168         int priv_bit;
169         if (priv.equals("ALL")) {
170           if (grant_object == GrantManager.TABLE) {
171             priv_bit = Privileges.TABLE_ALL_PRIVS.toInt();
172           }
173           else if (grant_object == GrantManager.SCHEMA) {
174             priv_bit = Privileges.SCHEMA_ALL_PRIVS.toInt();
175           }
176           else {
177             throw new Error JavaDoc("Unrecognised grant object.");
178           }
179         }
180         else {
181           priv_bit = Privileges.parseString(priv);
182         }
183         revoke_privs = revoke_privs.add(priv_bit);
184       }
185       
186       // Revoke the grants for the given users
187
for (int i = 0; i < revoke_from.size(); ++i) {
188         String JavaDoc name = (String JavaDoc) revoke_from.get(i);
189         if (name.equalsIgnoreCase("public")) {
190           // Revoke a public grant,
191
manager.removeGrant(revoke_privs, grant_object, grant_param,
192                               GrantManager.PUBLIC_USERNAME_STR,
193                               revoke_grant_option, user.getUserName());
194         }
195         else {
196           // Revoke a user grant.
197
manager.removeGrant(revoke_privs, grant_object, grant_param,
198                               name, revoke_grant_option, user.getUserName());
199         }
200       }
201       
202       // All done.
203

204     }
205     else {
206       throw new Error JavaDoc("Unknown priv manager command: " + command_type);
207     }
208
209     return FunctionTable.resultTable(context, 0);
210   }
211
212
213 }
214
215
Popular Tags