]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
34121f4c5074a4c4b20b86fc14164ce0f913e14d
[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.init.IInitHandler;\r
30 import org.collectionspace.services.common.init.InitHandler;\r
31 import org.collectionspace.services.common.storage.DatabaseProductType;\r
32 import org.collectionspace.services.common.storage.JDBCTools;\r
33 \r
34 import org.collectionspace.services.config.service.InitHandler.Params.Field;\r
35 import org.collectionspace.services.config.service.InitHandler.Params.Property;\r
36 import org.collectionspace.services.config.service.ServiceBindingType;\r
37 \r
38 import org.slf4j.Logger;\r
39 import org.slf4j.LoggerFactory;\r
40 \r
41 /**\r
42  * ReportPostInitHandler, post-init action to add grant reader access to DB\r
43  * \r
44  * In the configuration file, looks for a single Field declaration \r
45  * with a param value that has the name of the reader account/role.\r
46  * If not specified, it will assume 'reader'; \r
47  * \r
48  * $LastChangedRevision: 5103 $\r
49  * $LastChangedDate: 2011-06-23 16:50:06 -0700 (Thu, 23 Jun 2011) $\r
50  */\r
51 public class ReportPostInitHandler extends InitHandler implements IInitHandler {\r
52 \r
53     final Logger logger = LoggerFactory.getLogger(ReportPostInitHandler.class);\r
54     public static final String READ_ROLE_NAME_KEY = "readerRoleName";\r
55     private String readerRoleName = "reader";\r
56 \r
57     /** See the class javadoc for this class: it shows the syntax supported in the configuration params.\r
58      */\r
59     @Override\r
60     public void onRepositoryInitialized(DataSource dataSource,\r
61                 ServiceBindingType sbt, \r
62                 List<Field> fields, \r
63                 List<Property> properties) throws Exception {\r
64         //Check for existing privileges, and if not there, grant them\r
65         for(Property prop:properties) {\r
66                 if(READ_ROLE_NAME_KEY.equals(prop.getKey())) {\r
67                     String value = prop.getValue();\r
68                     if(Tools.notEmpty(value) && !readerRoleName.equals(value)){\r
69                         readerRoleName = value;\r
70                         logger.debug("ReportPostInitHandler: overriding readerRoleName to use: "\r
71                                 + value);\r
72                     }\r
73                 }\r
74         }\r
75         Connection conn = null;\r
76         Statement stmt = null;\r
77         String sql = "";\r
78         try {\r
79             DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType();\r
80             if (databaseProductType == DatabaseProductType.MYSQL) {\r
81                 // Nothing to do: MYSQL already does wildcard grants in init_db.sql\r
82             } else if(databaseProductType != DatabaseProductType.POSTGRESQL) {\r
83                 throw new Exception("Unrecognized database system " + databaseProductType);\r
84             } else {\r
85                 boolean hasRights = false;\r
86                 // Check for rights on report_common, and infer rights from that\r
87                 sql = "SELECT has_table_privilege('"+readerRoleName\r
88                         +"', '"+ReportConstants.DB_COMMON_PART_TABLE_NAME+"', 'SELECT')";\r
89                 conn = JDBCTools.getConnection(dataSource);\r
90                 stmt = conn.createStatement();\r
91                 ResultSet rs = stmt.executeQuery(sql);\r
92                 if(rs.next()) {\r
93                         hasRights = rs.getBoolean(1);\r
94                 }\r
95                 rs.close();\r
96                 if(!hasRights) {\r
97                         sql = "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM "+readerRoleName;\r
98                     stmt.execute(sql);\r
99                         sql = "GRANT SELECT ON ALL TABLES IN SCHEMA public TO "+readerRoleName;\r
100                     stmt.execute(sql);\r
101                 }\r
102             }\r
103             \r
104         } catch (SQLException sqle) {\r
105             SQLException tempException = sqle;\r
106             while (null != tempException) {       // SQLExceptions can be chained. Loop to log all.\r
107                 logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
108                 tempException = tempException.getNextException();\r
109             }\r
110             logger.debug("ReportPostInitHandler: SQL problem in executeQuery: ", sqle);\r
111         } catch (Throwable e) {\r
112             logger.debug("ReportPostInitHandler: problem checking/adding grant for reader: "+readerRoleName+") SQL: "+sql+" ERROR: "+e);\r
113         } finally {\r
114             try {\r
115                 if (conn != null) {\r
116                     conn.close();\r
117                 }\r
118                 if (stmt != null) {\r
119                     stmt.close();\r
120                 }\r
121             } catch (SQLException sqle) {\r
122                 logger.debug("SQL Exception closing statement/connection in executeQuery: " + sqle.getLocalizedMessage());\r
123             }\r
124         }\r
125     }\r
126     \r
127 \r
128 }\r