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