]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ea2efeb80920d301e3d95b9bc72ec07b30dbf984
[tmp/jakarta-migration.git] /
1 /**\r
2  *  This document is a part of the source code and related artifacts\r
3  *  for CollectionSpace, an open source collections management system\r
4  *  for museums and related institutions:\r
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \r
11  *  Licensed under the Educational Community License (ECL), Version 2.0.\r
12  *  You may not use this file except in compliance with this License.\r
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17  */\r
18 package org.collectionspace.services.report.nuxeo;\r
19 \r
20 import java.sql.Connection;\r
21 import java.sql.ResultSet;\r
22 import java.sql.SQLException;\r
23 import java.sql.Statement;\r
24 import java.util.List;\r
25 \r
26 import javax.sql.DataSource;\r
27 \r
28 import org.collectionspace.services.common.api.Tools;\r
29 import org.collectionspace.services.common.service.ServiceBindingType;\r
30 import org.collectionspace.services.common.init.IInitHandler;\r
31 import org.collectionspace.services.common.init.InitHandler;\r
32 import org.collectionspace.services.common.service.InitHandler.Params.Field;\r
33 import org.collectionspace.services.common.service.InitHandler.Params.Property;\r
34 import org.collectionspace.services.common.storage.DatabaseProductType;\r
35 import org.collectionspace.services.common.storage.JDBCTools;\r
36 \r
37 import org.slf4j.Logger;\r
38 import org.slf4j.LoggerFactory;\r
39 \r
40 /**\r
41  * ReportPostInitHandler, post-init action to add grant reader access to DB\r
42  * \r
43  * In the configuration file, looks for a single Field declaration \r
44  * with a param value that has the name of the reader account/role.\r
45  * If not specified, it will assume 'reader'; \r
46  * \r
47  * $LastChangedRevision: 5103 $\r
48  * $LastChangedDate: 2011-06-23 16:50:06 -0700 (Thu, 23 Jun 2011) $\r
49  */\r
50 public class ReportPostInitHandler extends InitHandler implements IInitHandler {\r
51 \r
52     final Logger logger = LoggerFactory.getLogger(ReportPostInitHandler.class);\r
53     public static final String READ_ROLE_NAME_KEY = "readerRoleName";\r
54     private String readerRoleName = "reader";\r
55 \r
56     /** See the class javadoc for this class: it shows the syntax supported in the configuration params.\r
57      */\r
58     @Override\r
59     public void onRepositoryInitialized(DataSource dataSource,\r
60                 ServiceBindingType sbt, \r
61                 List<Field> fields, \r
62                 List<Property> properties) throws Exception {\r
63         //Check for existing privileges, and if not there, grant them\r
64         for(Property prop:properties) {\r
65                 if(READ_ROLE_NAME_KEY.equals(prop.getKey())) {\r
66                     String value = prop.getValue();\r
67                     if(Tools.notEmpty(value) && !readerRoleName.equals(value)){\r
68                         readerRoleName = value;\r
69                         logger.debug("ReportPostInitHandler: overriding readerRoleName to use: "\r
70                                 + value);\r
71                     }\r
72                 }\r
73         }\r
74         Connection conn = null;\r
75         Statement stmt = null;\r
76         String sql = "";\r
77         try {\r
78             DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType();\r
79             if (databaseProductType == DatabaseProductType.MYSQL) {\r
80                 // Nothing to do: MYSQL already does wildcard grants in init_db.sql\r
81             } else if(databaseProductType != DatabaseProductType.POSTGRESQL) {\r
82                 throw new Exception("Unrecognized database system " + databaseProductType);\r
83             } else {\r
84                 boolean hasRights = false;\r
85                 // Check for rights on report_common, and infer rights from that\r
86                 sql = "SELECT has_table_privilege('"+readerRoleName\r
87                         +"', '"+ReportConstants.DB_COMMON_PART_TABLE_NAME+"', 'SELECT')";\r
88                 conn = JDBCTools.getConnection(dataSource);\r
89                 stmt = conn.createStatement();\r
90                 ResultSet rs = stmt.executeQuery(sql);\r
91                 if(rs.next()) {\r
92                         hasRights = rs.getBoolean(1);\r
93                 }\r
94                 rs.close();\r
95                 if(!hasRights) {\r
96                         sql = "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM "+readerRoleName;\r
97                     stmt.execute(sql);\r
98                         sql = "GRANT SELECT ON ALL TABLES IN SCHEMA public TO "+readerRoleName;\r
99                     stmt.execute(sql);\r
100                 }\r
101             }\r
102             \r
103         } catch (SQLException sqle) {\r
104             SQLException tempException = sqle;\r
105             while (null != tempException) {       // SQLExceptions can be chained. Loop to log all.\r
106                 logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
107                 tempException = tempException.getNextException();\r
108             }\r
109             logger.debug("ReportPostInitHandler: SQL problem in executeQuery: ", sqle);\r
110         } catch (Throwable e) {\r
111             logger.debug("ReportPostInitHandler: problem checking/adding grant for reader: "+readerRoleName+") SQL: "+sql+" ERROR: "+e);\r
112         } finally {\r
113             try {\r
114                 if (conn != null) {\r
115                     conn.close();\r
116                 }\r
117                 if (stmt != null) {\r
118                     stmt.close();\r
119                 }\r
120             } catch (SQLException sqle) {\r
121                 logger.debug("SQL Exception closing statement/connection in executeQuery: " + sqle.getLocalizedMessage());\r
122             }\r
123         }\r
124     }\r
125     \r
126 \r
127 }\r