KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > dblook > DB_GrantRevoke


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.DB_Alias
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.dblook;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import java.util.StringTokenizer JavaDoc;
30
31 import org.apache.derby.tools.dblook;
32
33 public class DB_GrantRevoke {
34
35     /** ************************************************
36      * Generate Grant & Revoke statements if sqlAuthorization is on
37      *
38      * @param conn Connection to use
39      */

40     public static void doAuthorizations(Connection JavaDoc conn)
41         throws SQLException JavaDoc {
42
43         // First generate table privilege statements
44
Statement JavaDoc stmt = conn.createStatement();
45         ResultSet JavaDoc rs = stmt.executeQuery("SELECT GRANTEE, SCHEMANAME, TABLENAME, SELECTPRIV, " +
46             "DELETEPRIV, INSERTPRIV, UPDATEPRIV, REFERENCESPRIV, TRIGGERPRIV FROM " +
47             "SYS.SYSTABLEPERMS P, SYS.SYSTABLES T, SYS.SYSSCHEMAS S WHERE T.SCHEMAID = " +
48             "S.SCHEMAID AND T.TABLEID = P.TABLEID");
49         generateTablePrivs(rs);
50
51         // Generate column privilege statements
52
rs = stmt.executeQuery("SELECT GRANTEE, SCHEMANAME, TABLENAME, TYPE, COLUMNS FROM " +
53             "SYS.SYSCOLPERMS P, SYS.SYSTABLES T, SYS.SYSSCHEMAS S WHERE T.SCHEMAID = " +
54             "S.SCHEMAID AND T.TABLEID = P.TABLEID");
55         generateColumnPrivs(rs, conn);
56
57         // Generate routine privilege statements
58
rs = stmt.executeQuery("SELECT GRANTEE, SCHEMANAME, ALIAS, ALIASTYPE FROM " +
59             "SYS.SYSROUTINEPERMS P, SYS.SYSALIASES A, SYS.SYSSCHEMAS S WHERE A.SCHEMAID = " +
60             "S.SCHEMAID AND P.ALIASID = A.ALIASID");
61         generateRoutinePrivs(rs);
62
63         rs.close();
64         stmt.close();
65         return;
66
67     }
68
69     /** ************************************************
70      * Generate table privilege statements
71      *
72      * @param rs Result set holding required information
73      ****/

74     private static void generateTablePrivs(ResultSet JavaDoc rs)
75         throws SQLException JavaDoc
76     {
77         boolean firstTime = true;
78         while (rs.next()) {
79
80             if (firstTime) {
81                 Logs.reportString("----------------------------------------------");
82                 Logs.reportMessage( "DBLOOK_TablePrivHeader");
83                 Logs.reportString("----------------------------------------------\n");
84             }
85
86             String JavaDoc authName = rs.getString(1);
87             String JavaDoc schemaName = dblook.addQuotes(dblook.expandDoubleQuotes(rs.getString(2)));
88             String JavaDoc tableName = dblook.addQuotes(dblook.expandDoubleQuotes(rs.getString(3)));
89             String JavaDoc fullName = schemaName + "." + tableName;
90
91             if (dblook.isIgnorableSchema(schemaName))
92                 continue;
93
94             Logs.writeToNewDDL(tablePrivStatement(rs, fullName, authName));
95             Logs.writeStmtEndToNewDDL();
96             Logs.writeNewlineToNewDDL();
97             firstTime = false;
98         }
99     }
100
101     private static String JavaDoc separatorStr(boolean addSeparator)
102     {
103         return (addSeparator) ? ", " : "";
104     }
105     
106     /** **************************************************
107      * Generate table privilege statement for the current row
108      *
109      * @param rs ResultSet holding tableperm information
110      * @param fullName Table's qualified name
111      * @param authName Authorization id for grant statement
112      */

113     private static String JavaDoc tablePrivStatement(ResultSet JavaDoc rs, String JavaDoc fullName, String JavaDoc authName)
114         throws SQLException JavaDoc
115     {
116         boolean addSeparator = false;
117         StringBuffer JavaDoc grantStmt = new StringBuffer JavaDoc("GRANT ");
118
119         if (rs.getString(4).toUpperCase().equals("Y"))
120         {
121             grantStmt.append("SELECT");
122             addSeparator = true;
123         }
124
125         if (rs.getString(5).toUpperCase().equals("Y"))
126         {
127             grantStmt.append(separatorStr(addSeparator)+ "DELETE");
128             addSeparator = true;
129         }
130
131         if (rs.getString(6).toUpperCase().equals("Y"))
132         {
133             grantStmt.append(separatorStr(addSeparator)+ "INSERT");
134             addSeparator = true;
135         }
136
137         if (rs.getString(7).toUpperCase().equals("Y"))
138         {
139             grantStmt.append(separatorStr(addSeparator)+ "UPDATE");
140             addSeparator = true;
141         }
142
143         if (rs.getString(8).toUpperCase().equals("Y"))
144         {
145             grantStmt.append(separatorStr(addSeparator)+ "REFERENCES");
146             addSeparator = true;
147         }
148
149         if (rs.getString(9).toUpperCase().equals("Y"))
150         {
151             grantStmt.append(separatorStr(addSeparator)+ "TRIGGER");
152             addSeparator = true;
153         }
154
155         grantStmt.append(" ON " + fullName + " TO " + authName);
156
157         return grantStmt.toString();
158     }
159
160     /** ************************************************
161      * Generate column privilege statements
162      *
163      * @param rs ResultSet holding column privilege information
164      * @param conn Connection to use. Used to get another ResultSet
165      ****/

166
167     private static void generateColumnPrivs(ResultSet JavaDoc rs, Connection JavaDoc conn)
168         throws SQLException JavaDoc
169     {
170         boolean firstTime = true;
171         while (rs.next()) {
172             if (firstTime) {
173                 Logs.reportString("----------------------------------------------");
174                 Logs.reportMessage( "DBLOOK_ColumnPrivHeader");
175                 Logs.reportString("----------------------------------------------\n");
176             }
177
178             String JavaDoc authName = rs.getString(1);
179             String JavaDoc schemaName = dblook.expandDoubleQuotes(rs.getString(2));
180             String JavaDoc tableName = dblook.expandDoubleQuotes(rs.getString(3));
181
182             if (dblook.isIgnorableSchema(schemaName))
183                 continue;
184
185             // Create another resultSet to get column names
186
Statement JavaDoc stmtCols = conn.createStatement();
187             String JavaDoc queryCols = "SELECT COLUMNNUMBER, COLUMNNAME " +
188                 "FROM SYS.SYSCOLUMNS C, SYS.SYSTABLES T, SYS.SYSSCHEMAS S " +
189                 "WHERE T.TABLEID = C.REFERENCEID and S.SCHEMAID = T.SCHEMAID "+
190                 "and T.TABLENAME = '"+tableName+"' AND SCHEMANAME = '"+schemaName +
191                 "' ORDER BY COLUMNNUMBER";
192
193             ResultSet JavaDoc rsCols= stmtCols.executeQuery(queryCols);
194             String JavaDoc fullName = dblook.addQuotes(schemaName) + "." + dblook.addQuotes(tableName);
195
196             Logs.writeToNewDDL(columnPrivStatement(rs, fullName, authName, rsCols));
197             Logs.writeStmtEndToNewDDL();
198             Logs.writeNewlineToNewDDL();
199             firstTime = false;
200         }
201     }
202
203     private static String JavaDoc privTypeToString(String JavaDoc privType)
204     {
205         if (privType.equals("S"))
206             return "SELECT";
207         else if (privType.equals("R"))
208             return "REFERENCES";
209         else if (privType.equals("U"))
210             return "UPDATE";
211
212         // Should throw an exception?
213
return "";
214     }
215
216     /** ************************************************
217      * Generate one column grant statement
218      *
219      * @param columns List of columns to grant required privs
220      * @param rsCols ResultSet for mapping column numbers to names
221      ****/

222
223     private static String JavaDoc mapColumnsToNames(String JavaDoc columns, ResultSet JavaDoc rsCols)
224         throws SQLException JavaDoc
225     {
226         StringBuffer JavaDoc colNames = new StringBuffer JavaDoc();
227         rsCols.next();
228         int curColumn = 1;
229         boolean addSeparator = false;
230
231         // Strip out outer {} in addition to spaces and comma
232
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(columns, " ,{}");
233         while (st.hasMoreTokens())
234         {
235             int colNum = Integer.parseInt(st.nextToken());
236             while (colNum+1 > curColumn)
237             {
238                 rsCols.next();
239                 curColumn = rsCols.getInt(1);
240             }
241             colNames.append(separatorStr(addSeparator));
242             colNames.append(rsCols.getString(2));
243             addSeparator = true;
244         }
245
246         return colNames.toString();
247     }
248
249     /** ************************************************
250      *
251      * @param rs ResultSet with info for this GRANT statement
252      * @param fullName Full qualified name of the table
253      * @param authName Authorization name for this GRANT
254      * @param rsCols ResultSet for mapping column numbers to names
255      ****/

256
257     private static String JavaDoc columnPrivStatement(ResultSet JavaDoc rs, String JavaDoc fullName,
258             String JavaDoc authName, ResultSet JavaDoc rsCols) throws SQLException JavaDoc
259     {
260         StringBuffer JavaDoc grantStmt = new StringBuffer JavaDoc("GRANT ");
261
262         String JavaDoc privType = rs.getString(4).toUpperCase();
263         String JavaDoc columns = rs.getString(5);
264         grantStmt.append(privTypeToString(privType));
265         grantStmt.append("(");
266         grantStmt.append(mapColumnsToNames(columns, rsCols));
267         grantStmt.append(")");
268         grantStmt.append(" TO ");
269         grantStmt.append(authName);
270
271         return grantStmt.toString();
272     }
273
274     /** ************************************************
275      * Generate routine privilege statements
276      *
277      * @param rs ResultSet holding required information
278      ****/

279     public static void generateRoutinePrivs(ResultSet JavaDoc rs) throws SQLException JavaDoc
280     {
281         boolean firstTime = true;
282         while (rs.next()) {
283             String JavaDoc authName = rs.getString(1);
284             String JavaDoc schemaName = dblook.addQuotes(dblook.expandDoubleQuotes(rs.getString(2)));
285             String JavaDoc aliasName = dblook.addQuotes(dblook.expandDoubleQuotes(rs.getString(3)));
286             String JavaDoc fullName = schemaName + "." + aliasName;
287             String JavaDoc aliasType = rs.getString(4);
288
289             if (dblook.isIgnorableSchema(schemaName))
290                 continue;
291
292             // Ignore SYSCS_UTIL privileges as all new databases automatically get them
293
if (schemaName.equals("\"SYSCS_UTIL\""))
294                 continue;
295
296             if (firstTime) {
297                 Logs.reportString("----------------------------------------------");
298                 Logs.reportMessage("DBLOOK_RoutinePrivHeader");
299                 Logs.reportString("----------------------------------------------\n");
300             }
301
302             Logs.writeToNewDDL(routinePrivStatement(fullName, authName, aliasType));
303             Logs.writeStmtEndToNewDDL();
304             Logs.writeNewlineToNewDDL();
305             firstTime = false;
306         }
307     }
308
309     private static String JavaDoc routinePrivStatement(String JavaDoc fullName, String JavaDoc authName, String JavaDoc aliasType)
310         throws SQLException JavaDoc
311     {
312         boolean addSeparator = false;
313         StringBuffer JavaDoc grantStmt = new StringBuffer JavaDoc("GRANT EXECUTE ON ");
314
315         grantStmt.append((aliasType.equals("P")) ? "PROCEDURE " : "FUNCTION ");
316         grantStmt.append(fullName);
317         grantStmt.append(" TO ");
318         grantStmt.append(authName);
319
320         return grantStmt.toString();
321     }
322 }
323
Popular Tags