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
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright 2009 University of California at Berkeley
\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
14 * You may obtain a copy of the ECL 2.0 License at
\r
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
18 package org.collectionspace.services.report.nuxeo;
\r
20 import java.sql.Connection;
\r
21 import java.sql.SQLException;
\r
22 import java.sql.Statement;
\r
23 import java.util.List;
\r
25 import org.collectionspace.services.common.api.Tools;
\r
26 import org.collectionspace.services.common.init.IInitHandler;
\r
27 import org.collectionspace.services.common.init.InitHandler;
\r
28 import org.collectionspace.services.common.storage.DatabaseProductType;
\r
29 import org.collectionspace.services.common.storage.JDBCTools;
\r
31 import org.collectionspace.services.config.service.InitHandler.Params.Field;
\r
32 import org.collectionspace.services.config.service.InitHandler.Params.Property;
\r
33 import org.collectionspace.services.config.service.ServiceBindingType;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
39 * ReportPostInitHandler, post-init action to add grant reader access to DB
\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
45 * $LastChangedRevision: 5103 $
\r
46 * $LastChangedDate: 2011-06-23 16:50:06 -0700 (Thu, 23 Jun 2011) $
\r
48 public class ReportPostInitHandler extends InitHandler implements IInitHandler {
\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
54 /** See the class javadoc for this class: it shows the syntax supported in the configuration params.
\r
57 public void onRepositoryInitialized(String dataSourceName,
\r
58 String repositoryName,
\r
59 String cspaceInstanceId,
\r
60 ServiceBindingType sbt,
\r
61 List<Field> fields,
\r
62 List<Property> propertyList) throws Exception {
\r
63 //Check for existing privileges, and if not there, grant them
\r
64 for(Property prop : propertyList) {
\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
75 Connection conn = null;
\r
76 Statement stmt = null;
\r
79 DatabaseProductType databaseProductType = JDBCTools.getDatabaseProductType(dataSourceName, repositoryName,
\r
81 if (databaseProductType == DatabaseProductType.MYSQL) {
\r
82 // Nothing to do: MYSQL already does wildcard grants in init_db.sql
\r
83 } else if(databaseProductType != DatabaseProductType.POSTGRESQL) {
\r
84 throw new Exception("Unrecognized database system " + databaseProductType);
\r
86 String databaseName = JDBCTools.getDatabaseName(repositoryName, cspaceInstanceId);
\r
87 conn = JDBCTools.getConnection(dataSourceName, databaseName);
\r
88 stmt = conn.createStatement();
\r
89 //sql = "REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM "+readerRoleName;
\r
90 //stmt.execute(sql);
\r
91 sql = "GRANT SELECT ON ALL TABLES IN SCHEMA public TO "+readerRoleName;
\r
95 } catch (SQLException sqle) {
\r
96 SQLException tempException = sqle;
\r
97 while (null != tempException) { // SQLExceptions can be chained. Loop to log all.
\r
98 logger.debug("SQL Exception: " + sqle.getLocalizedMessage());
\r
99 tempException = tempException.getNextException();
\r
101 logger.debug("ReportPostInitHandler: SQL problem in executeQuery: ", sqle);
\r
102 } catch (Throwable e) {
\r
103 logger.debug("ReportPostInitHandler: problem checking/adding grant for reader: "+readerRoleName+") SQL: "+sql+" ERROR: "+e);
\r
106 if (conn != null) {
\r
109 if (stmt != null) {
\r
112 } catch (SQLException sqle) {
\r
113 logger.debug("SQL Exception closing statement/connection in executeQuery: " + sqle.getLocalizedMessage());
\r