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(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 OUString& _Name
47 : ) : connectivity::sdbcx::OUser(_Name, 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,::cppu::UnoType<OUString>::get());
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 OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
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 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 : 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,OUString("%"));
116 : }
117 0 : break;
118 : }
119 :
120 0 : if ( xRes.is() )
121 : {
122 0 : static const OUString sSELECT( "SELECT" );
123 0 : static const OUString sINSERT( "INSERT" );
124 0 : static const OUString sUPDATE( "UPDATE" );
125 0 : static const OUString sDELETE( "DELETE" );
126 0 : static const OUString sREAD( "READ" );
127 0 : static const OUString sCREATE( "CREATE" );
128 0 : static const OUString sALTER( "ALTER" );
129 0 : static const OUString sREFERENCE( "REFERENCE" );
130 0 : static const OUString sDROP( "DROP" );
131 0 : static const 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 : OUString sGrantee = xCurrentRow->getString(5);
139 0 : OUString sPrivilege = xCurrentRow->getString(6);
140 0 : 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 OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
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 OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
215 : {
216 0 : if ( objType != PrivilegeObject::TABLE )
217 : {
218 0 : ::connectivity::SharedResources aResources;
219 0 : const 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 : OUString sPrivs = getPrivilegeString(objPrivileges);
227 0 : if(!sPrivs.isEmpty())
228 : {
229 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
230 0 : OUString sGrant = "GRANT " + sPrivs +
231 0 : " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
232 0 : " TO " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
233 :
234 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
235 0 : if(xStmt.is())
236 0 : xStmt->execute(sGrant);
237 0 : ::comphelper::disposeComponent(xStmt);
238 0 : }
239 0 : }
240 :
241 0 : void SAL_CALL OHSQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
242 : {
243 0 : if ( objType != PrivilegeObject::TABLE )
244 : {
245 0 : ::connectivity::SharedResources aResources;
246 0 : const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
247 0 : ::dbtools::throwGenericSQLException(sError,*this);
248 : } // if ( objType != PrivilegeObject::TABLE )
249 :
250 0 : ::osl::MutexGuard aGuard(m_aMutex);
251 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
252 0 : OUString sPrivs = getPrivilegeString(objPrivileges);
253 0 : if(!sPrivs.isEmpty())
254 : {
255 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
256 0 : OUString sGrant = "REVOKE " + sPrivs +
257 0 : " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
258 0 : " FROM " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
259 :
260 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
261 0 : if(xStmt.is())
262 0 : xStmt->execute(sGrant);
263 0 : ::comphelper::disposeComponent(xStmt);
264 0 : }
265 0 : }
266 :
267 : // XUser
268 0 : void SAL_CALL OHSQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException, std::exception)
269 : {
270 0 : ::osl::MutexGuard aGuard(m_aMutex);
271 0 : checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
272 :
273 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
274 :
275 0 : if( m_Name != xMeta->getUserName() )
276 : {
277 0 : ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
278 : }
279 :
280 0 : OUString sAlterPwd = "SET PASSWORD " +
281 0 : ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), newPassword);
282 :
283 0 : Reference<XStatement> xStmt = m_xConnection->createStatement();
284 0 : if ( xStmt.is() )
285 : {
286 0 : xStmt->execute(sAlterPwd);
287 0 : ::comphelper::disposeComponent(xStmt);
288 0 : }
289 0 : }
290 :
291 0 : OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
292 : {
293 0 : OUString sPrivs;
294 0 : if((nRights & Privilege::INSERT) == Privilege::INSERT)
295 0 : sPrivs += "INSERT";
296 :
297 0 : if((nRights & Privilege::DELETE) == Privilege::DELETE)
298 : {
299 0 : if(!sPrivs.isEmpty())
300 0 : sPrivs += ",";
301 0 : sPrivs += "DELETE";
302 : }
303 :
304 0 : if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
305 : {
306 0 : if(!sPrivs.isEmpty())
307 0 : sPrivs += ",";
308 0 : sPrivs += "UPDATE";
309 : }
310 :
311 0 : if((nRights & Privilege::ALTER) == Privilege::ALTER)
312 : {
313 0 : if(!sPrivs.isEmpty())
314 0 : sPrivs += ",";
315 0 : sPrivs += "ALTER";
316 : }
317 :
318 0 : if((nRights & Privilege::SELECT) == Privilege::SELECT)
319 : {
320 0 : if(!sPrivs.isEmpty())
321 0 : sPrivs += ",";
322 0 : sPrivs += "SELECT";
323 : }
324 :
325 0 : if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
326 : {
327 0 : if(!sPrivs.isEmpty())
328 0 : sPrivs += ",";
329 0 : sPrivs += "REFERENCES";
330 : }
331 :
332 0 : return sPrivs;
333 : }
334 :
335 :
336 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|