Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "hsqldb/HUser.hxx"
21 : : #include <com/sun/star/sdbc/XRow.hpp>
22 : : #include <com/sun/star/sdbc/XResultSet.hpp>
23 : : #include "connectivity/dbtools.hxx"
24 : : #include "connectivity/dbexception.hxx"
25 : : #include <com/sun/star/sdbcx/Privilege.hpp>
26 : : #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
27 : : #include "TConnection.hxx"
28 : : #include "resource/hsqldb_res.hrc"
29 : :
30 : : using namespace connectivity;
31 : : using namespace connectivity::hsqldb;
32 : : using namespace ::com::sun::star::uno;
33 : : using namespace ::com::sun::star::beans;
34 : : using namespace ::com::sun::star::sdbcx;
35 : : using namespace ::com::sun::star::sdbc;
36 : : using namespace ::com::sun::star::container;
37 : : using namespace ::com::sun::star::lang;
38 : : // -------------------------------------------------------------------------
39 : 0 : OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
40 : 0 : ,m_xConnection(_xConnection)
41 : : {
42 : 0 : construct();
43 : 0 : }
44 : : // -------------------------------------------------------------------------
45 : 0 : OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
46 : : const ::rtl::OUString& _Name
47 : : ) : connectivity::sdbcx::OUser(_Name,sal_True)
48 : 0 : ,m_xConnection(_xConnection)
49 : : {
50 : 0 : construct();
51 : 0 : }
52 : : // -------------------------------------------------------------------------
53 : 0 : void OHSQLUser::refreshGroups()
54 : : {
55 : 0 : }
56 : : // -------------------------------------------------------------------------
57 : 0 : OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
58 : : {
59 : 0 : construct();
60 : 0 : }
61 : : // -------------------------------------------------------------------------
62 : 0 : void OUserExtend::construct()
63 : : {
64 : 0 : registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< ::rtl::OUString*>(0)));
65 : 0 : }
66 : : // -----------------------------------------------------------------------------
67 : 0 : cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
68 : : {
69 : 0 : Sequence< Property > aProps;
70 : 0 : describeProperties(aProps);
71 : 0 : return new cppu::OPropertyArrayHelper(aProps);
72 : : }
73 : : // -------------------------------------------------------------------------
74 : 0 : cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
75 : : {
76 : 0 : return *OUserExtend_PROP::getArrayHelper();
77 : : }
78 : : typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
79 : : // -----------------------------------------------------------------------------
80 : 0 : sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
81 : : {
82 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
83 : 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
84 : :
85 : : sal_Int32 nRights,nRightsWithGrant;
86 : 0 : findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
87 : 0 : return nRights;
88 : : }
89 : : // -----------------------------------------------------------------------------
90 : 0 : void OHSQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
91 : : {
92 : 0 : nRightsWithGrant = nRights = 0;
93 : : // first we need to create the sql stmt to select the privs
94 : 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
95 : 0 : ::rtl::OUString sCatalog,sSchema,sTable;
96 : 0 : ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
97 : 0 : Reference<XResultSet> xRes;
98 : 0 : switch(objType)
99 : : {
100 : : case PrivilegeObject::TABLE:
101 : : case PrivilegeObject::VIEW:
102 : : {
103 : 0 : Any aCatalog;
104 : 0 : if ( !sCatalog.isEmpty() )
105 : 0 : aCatalog <<= sCatalog;
106 : 0 : xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
107 : : }
108 : 0 : break;
109 : :
110 : : case PrivilegeObject::COLUMN:
111 : : {
112 : 0 : Any aCatalog;
113 : 0 : if ( !sCatalog.isEmpty() )
114 : 0 : aCatalog <<= sCatalog;
115 : 0 : xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString("%"));
116 : : }
117 : 0 : break;
118 : : }
119 : :
120 : 0 : if ( xRes.is() )
121 : : {
122 : 0 : static const ::rtl::OUString sSELECT( "SELECT" );
123 : 0 : static const ::rtl::OUString sINSERT( "INSERT" );
124 : 0 : static const ::rtl::OUString sUPDATE( "UPDATE" );
125 : 0 : static const ::rtl::OUString sDELETE( "DELETE" );
126 : 0 : static const ::rtl::OUString sREAD( "READ" );
127 : 0 : static const ::rtl::OUString sCREATE( "CREATE" );
128 : 0 : static const ::rtl::OUString sALTER( "ALTER" );
129 : 0 : static const ::rtl::OUString sREFERENCE( "REFERENCE" );
130 : 0 : static const ::rtl::OUString sDROP( "DROP" );
131 : 0 : static const ::rtl::OUString sYes( "YES" );
132 : :
133 : 0 : nRightsWithGrant = nRights = 0;
134 : :
135 : 0 : Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
136 : 0 : while( xCurrentRow.is() && xRes->next() )
137 : : {
138 : 0 : ::rtl::OUString sGrantee = xCurrentRow->getString(5);
139 : 0 : ::rtl::OUString sPrivilege = xCurrentRow->getString(6);
140 : 0 : ::rtl::OUString sGrantable = xCurrentRow->getString(7);
141 : :
142 : 0 : if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
143 : 0 : continue;
144 : :
145 : 0 : if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
146 : : {
147 : 0 : nRights |= Privilege::SELECT;
148 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
149 : 0 : nRightsWithGrant |= Privilege::SELECT;
150 : : }
151 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
152 : : {
153 : 0 : nRights |= Privilege::INSERT;
154 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
155 : 0 : nRightsWithGrant |= Privilege::INSERT;
156 : : }
157 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
158 : : {
159 : 0 : nRights |= Privilege::UPDATE;
160 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
161 : 0 : nRightsWithGrant |= Privilege::UPDATE;
162 : : }
163 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
164 : : {
165 : 0 : nRights |= Privilege::DELETE;
166 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
167 : 0 : nRightsWithGrant |= Privilege::DELETE;
168 : : }
169 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
170 : : {
171 : 0 : nRights |= Privilege::READ;
172 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
173 : 0 : nRightsWithGrant |= Privilege::READ;
174 : : }
175 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
176 : : {
177 : 0 : nRights |= Privilege::CREATE;
178 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
179 : 0 : nRightsWithGrant |= Privilege::CREATE;
180 : : }
181 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
182 : : {
183 : 0 : nRights |= Privilege::ALTER;
184 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
185 : 0 : nRightsWithGrant |= Privilege::ALTER;
186 : : }
187 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
188 : : {
189 : 0 : nRights |= Privilege::REFERENCE;
190 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
191 : 0 : nRightsWithGrant |= Privilege::REFERENCE;
192 : : }
193 : 0 : else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
194 : : {
195 : 0 : nRights |= Privilege::DROP;
196 : 0 : if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
197 : 0 : nRightsWithGrant |= Privilege::DROP;
198 : : }
199 : 0 : }
200 : 0 : ::comphelper::disposeComponent(xRes);
201 : 0 : }
202 : 0 : }
203 : : // -------------------------------------------------------------------------
204 : 0 : sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
205 : : {
206 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
207 : 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
208 : :
209 : : sal_Int32 nRights,nRightsWithGrant;
210 : 0 : findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
211 : 0 : return nRightsWithGrant;
212 : : }
213 : : // -------------------------------------------------------------------------
214 : 0 : void SAL_CALL OHSQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
215 : : {
216 : 0 : if ( objType != PrivilegeObject::TABLE )
217 : : {
218 : 0 : ::connectivity::SharedResources aResources;
219 : 0 : const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
220 : 0 : ::dbtools::throwGenericSQLException(sError,*this);
221 : : } // if ( objType != PrivilegeObject::TABLE )
222 : :
223 : :
224 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
225 : :
226 : 0 : ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
227 : 0 : if(!sPrivs.isEmpty())
228 : : {
229 : 0 : ::rtl::OUString sGrant;
230 : 0 : sGrant += ::rtl::OUString("GRANT ");
231 : 0 : sGrant += sPrivs;
232 : 0 : sGrant += ::rtl::OUString(" ON ");
233 : 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
234 : 0 : sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
235 : 0 : sGrant += ::rtl::OUString(" TO ");
236 : 0 : sGrant += m_Name;
237 : :
238 : 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
239 : 0 : if(xStmt.is())
240 : 0 : xStmt->execute(sGrant);
241 : 0 : ::comphelper::disposeComponent(xStmt);
242 : 0 : }
243 : 0 : }
244 : : // -------------------------------------------------------------------------
245 : 0 : void SAL_CALL OHSQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
246 : : {
247 : 0 : if ( objType != PrivilegeObject::TABLE )
248 : : {
249 : 0 : ::connectivity::SharedResources aResources;
250 : 0 : const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
251 : 0 : ::dbtools::throwGenericSQLException(sError,*this);
252 : : } // if ( objType != PrivilegeObject::TABLE )
253 : :
254 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
255 : 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
256 : 0 : ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
257 : 0 : if(!sPrivs.isEmpty())
258 : : {
259 : 0 : ::rtl::OUString sGrant;
260 : 0 : sGrant += ::rtl::OUString("REVOKE ");
261 : 0 : sGrant += sPrivs;
262 : 0 : sGrant += ::rtl::OUString(" ON ");
263 : 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
264 : 0 : sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
265 : 0 : sGrant += ::rtl::OUString(" FROM ");
266 : 0 : sGrant += m_Name;
267 : :
268 : 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
269 : 0 : if(xStmt.is())
270 : 0 : xStmt->execute(sGrant);
271 : 0 : ::comphelper::disposeComponent(xStmt);
272 : 0 : }
273 : 0 : }
274 : : // -----------------------------------------------------------------------------
275 : : // XUser
276 : 0 : void SAL_CALL OHSQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
277 : : {
278 : 0 : ::osl::MutexGuard aGuard(m_aMutex);
279 : 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
280 : 0 : ::rtl::OUString sAlterPwd;
281 : 0 : sAlterPwd = ::rtl::OUString("SET PASSWORD FOR ");
282 : 0 : sAlterPwd += m_Name;
283 : 0 : sAlterPwd += ::rtl::OUString("@\"%\" = PASSWORD('") ;
284 : 0 : sAlterPwd += newPassword;
285 : 0 : sAlterPwd += ::rtl::OUString("')") ;
286 : :
287 : :
288 : 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
289 : 0 : if ( xStmt.is() )
290 : : {
291 : 0 : xStmt->execute(sAlterPwd);
292 : 0 : ::comphelper::disposeComponent(xStmt);
293 : 0 : }
294 : 0 : }
295 : : // -----------------------------------------------------------------------------
296 : 0 : ::rtl::OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
297 : : {
298 : 0 : ::rtl::OUString sPrivs;
299 : 0 : if((nRights & Privilege::INSERT) == Privilege::INSERT)
300 : 0 : sPrivs += ::rtl::OUString("INSERT");
301 : :
302 : 0 : if((nRights & Privilege::DELETE) == Privilege::DELETE)
303 : : {
304 : 0 : if(!sPrivs.isEmpty())
305 : 0 : sPrivs += ::rtl::OUString(",");
306 : 0 : sPrivs += ::rtl::OUString("DELETE");
307 : : }
308 : :
309 : 0 : if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
310 : : {
311 : 0 : if(!sPrivs.isEmpty())
312 : 0 : sPrivs += ::rtl::OUString(",");
313 : 0 : sPrivs += ::rtl::OUString("UPDATE");
314 : : }
315 : :
316 : 0 : if((nRights & Privilege::ALTER) == Privilege::ALTER)
317 : : {
318 : 0 : if(!sPrivs.isEmpty())
319 : 0 : sPrivs += ::rtl::OUString(",");
320 : 0 : sPrivs += ::rtl::OUString("ALTER");
321 : : }
322 : :
323 : 0 : if((nRights & Privilege::SELECT) == Privilege::SELECT)
324 : : {
325 : 0 : if(!sPrivs.isEmpty())
326 : 0 : sPrivs += ::rtl::OUString(",");
327 : 0 : sPrivs += ::rtl::OUString("SELECT");
328 : : }
329 : :
330 : 0 : if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
331 : : {
332 : 0 : if(!sPrivs.isEmpty())
333 : 0 : sPrivs += ::rtl::OUString(",");
334 : 0 : sPrivs += ::rtl::OUString("REFERENCES");
335 : : }
336 : :
337 : 0 : return sPrivs;
338 : : }
339 : : // -----------------------------------------------------------------------------
340 : :
341 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|