LibreOffice Module connectivity (master) 1
HUser.cxx
Go to the documentation of this file.
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>
25#include <comphelper/types.hxx>
26#include <com/sun/star/sdbcx/Privilege.hpp>
27#include <com/sun/star/sdbcx/PrivilegeObject.hpp>
28#include <TConnection.hxx>
29#include <strings.hrc>
30#include <utility>
31
32using namespace connectivity;
33using namespace connectivity::hsqldb;
34using namespace ::com::sun::star::uno;
35using namespace ::com::sun::star::beans;
36using namespace ::com::sun::star::sdbcx;
37using namespace ::com::sun::star::sdbc;
38using namespace ::com::sun::star::container;
39using namespace ::com::sun::star::lang;
40
41OHSQLUser::OHSQLUser( css::uno::Reference< css::sdbc::XConnection > _xConnection) : connectivity::sdbcx::OUser(true)
42 ,m_xConnection(std::move(_xConnection))
43{
44 construct();
45}
46
47OHSQLUser::OHSQLUser( css::uno::Reference< css::sdbc::XConnection > _xConnection,
48 const OUString& Name
49 ) : connectivity::sdbcx::OUser(Name,true)
50 ,m_xConnection(std::move(_xConnection))
51{
52 construct();
53}
54
56{
57}
58
59OUserExtend::OUserExtend( const css::uno::Reference< css::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
60{
61 construct();
62}
63
65{
67}
68
70{
71 Sequence< Property > aProps;
72 describeProperties(aProps);
73 return new cppu::OPropertyArrayHelper(aProps);
74}
75
77{
79}
81
82sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const OUString& objName, sal_Int32 objType )
83{
84 ::osl::MutexGuard aGuard(m_aMutex);
85 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
86
87 sal_Int32 nRights,nRightsWithGrant;
88 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
89 return nRights;
90}
91
92void OHSQLUser::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant)
93{
94 nRightsWithGrant = nRights = 0;
95 // first we need to create the sql stmt to select the privs
96 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
97 OUString sCatalog,sSchema,sTable;
98 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
99 Reference<XResultSet> xRes;
100 switch(objType)
101 {
102 case PrivilegeObject::TABLE:
103 case PrivilegeObject::VIEW:
104 {
105 Any aCatalog;
106 if ( !sCatalog.isEmpty() )
107 aCatalog <<= sCatalog;
108 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
109 }
110 break;
111
112 case PrivilegeObject::COLUMN:
113 {
114 Any aCatalog;
115 if ( !sCatalog.isEmpty() )
116 aCatalog <<= sCatalog;
117 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,"%");
118 }
119 break;
120 }
121
122 if ( !xRes.is() )
123 return;
124
125 static const char sYes [] = "YES";
126
127 nRightsWithGrant = nRights = 0;
128
129 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
130 while( xCurrentRow.is() && xRes->next() )
131 {
132 OUString sGrantee = xCurrentRow->getString(5);
133 OUString sPrivilege = xCurrentRow->getString(6);
134 OUString sGrantable = xCurrentRow->getString(7);
135
136 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
137 continue;
138
139 if (sPrivilege.equalsIgnoreAsciiCase("SELECT"))
140 {
141 nRights |= Privilege::SELECT;
142 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
143 nRightsWithGrant |= Privilege::SELECT;
144 }
145 else if (sPrivilege.equalsIgnoreAsciiCase("INSERT"))
146 {
147 nRights |= Privilege::INSERT;
148 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
149 nRightsWithGrant |= Privilege::INSERT;
150 }
151 else if (sPrivilege.equalsIgnoreAsciiCase("UPDATE"))
152 {
153 nRights |= Privilege::UPDATE;
154 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
155 nRightsWithGrant |= Privilege::UPDATE;
156 }
157 else if (sPrivilege.equalsIgnoreAsciiCase("DELETE"))
158 {
159 nRights |= Privilege::DELETE;
160 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
161 nRightsWithGrant |= Privilege::DELETE;
162 }
163 else if (sPrivilege.equalsIgnoreAsciiCase("READ"))
164 {
165 nRights |= Privilege::READ;
166 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
167 nRightsWithGrant |= Privilege::READ;
168 }
169 else if (sPrivilege.equalsIgnoreAsciiCase("CREATE"))
170 {
171 nRights |= Privilege::CREATE;
172 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
173 nRightsWithGrant |= Privilege::CREATE;
174 }
175 else if (sPrivilege.equalsIgnoreAsciiCase("ALTER"))
176 {
177 nRights |= Privilege::ALTER;
178 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
179 nRightsWithGrant |= Privilege::ALTER;
180 }
181 else if (sPrivilege.equalsIgnoreAsciiCase("REFERENCE"))
182 {
183 nRights |= Privilege::REFERENCE;
184 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
185 nRightsWithGrant |= Privilege::REFERENCE;
186 }
187 else if (sPrivilege.equalsIgnoreAsciiCase("DROP"))
188 {
189 nRights |= Privilege::DROP;
190 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
191 nRightsWithGrant |= Privilege::DROP;
192 }
193 }
194 ::comphelper::disposeComponent(xRes);
195}
196
197sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType )
198{
199 ::osl::MutexGuard aGuard(m_aMutex);
200 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
201
202 sal_Int32 nRights,nRightsWithGrant;
203 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
204 return nRightsWithGrant;
205}
206
207void SAL_CALL OHSQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges )
208{
209 if ( objType != PrivilegeObject::TABLE )
210 {
212 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
214 } // if ( objType != PrivilegeObject::TABLE )
215
216
217 ::osl::MutexGuard aGuard(m_aMutex);
218
219 OUString sPrivs = getPrivilegeString(objPrivileges);
220 if(!sPrivs.isEmpty())
221 {
222 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
223 OUString sGrant = "GRANT " + sPrivs +
224 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
225 " TO " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
226
227 Reference<XStatement> xStmt = m_xConnection->createStatement();
228 if(xStmt.is())
229 xStmt->execute(sGrant);
230 ::comphelper::disposeComponent(xStmt);
231 }
232}
233
234void SAL_CALL OHSQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges )
235{
236 if ( objType != PrivilegeObject::TABLE )
237 {
239 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
241 } // if ( objType != PrivilegeObject::TABLE )
242
243 ::osl::MutexGuard aGuard(m_aMutex);
244 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
245 OUString sPrivs = getPrivilegeString(objPrivileges);
246 if(!sPrivs.isEmpty())
247 {
248 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
249 OUString sGrant = "REVOKE " + sPrivs +
250 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
251 " FROM " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
252
253 Reference<XStatement> xStmt = m_xConnection->createStatement();
254 if(xStmt.is())
255 xStmt->execute(sGrant);
256 ::comphelper::disposeComponent(xStmt);
257 }
258}
259
260// XUser
261void SAL_CALL OHSQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword )
262{
263 ::osl::MutexGuard aGuard(m_aMutex);
264 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
265
266 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
267
268 if( m_Name != xMeta->getUserName() )
269 {
270 ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
271 }
272
273 OUString sAlterPwd = "SET PASSWORD " +
274 ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), newPassword);
275
276 Reference<XStatement> xStmt = m_xConnection->createStatement();
277 if ( xStmt.is() )
278 {
279 xStmt->execute(sAlterPwd);
280 ::comphelper::disposeComponent(xStmt);
281 }
282}
283
284OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights)
285{
286 OUString sPrivs;
287 if((nRights & Privilege::INSERT) == Privilege::INSERT)
288 sPrivs += "INSERT";
289
290 if((nRights & Privilege::DELETE) == Privilege::DELETE)
291 {
292 if(!sPrivs.isEmpty())
293 sPrivs += ",";
294 sPrivs += "DELETE";
295 }
296
297 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
298 {
299 if(!sPrivs.isEmpty())
300 sPrivs += ",";
301 sPrivs += "UPDATE";
302 }
303
304 if((nRights & Privilege::ALTER) == Privilege::ALTER)
305 {
306 if(!sPrivs.isEmpty())
307 sPrivs += ",";
308 sPrivs += "ALTER";
309 }
310
311 if((nRights & Privilege::SELECT) == Privilege::SELECT)
312 {
313 if(!sPrivs.isEmpty())
314 sPrivs += ",";
315 sPrivs += "SELECT";
316 }
317
318 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
319 {
320 if(!sPrivs.isEmpty())
321 sPrivs += ",";
322 sPrivs += "REFERENCES";
323 }
324
325 return sPrivs;
326}
327
328
329/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
Definition: HUser.cxx:80
::cppu::IPropertyArrayHelper * getArrayHelper()
void describeProperties(css::uno::Sequence< css::beans::Property > &_rProps) const
void registerProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void *_pPointerToMember, const css::uno::Type &_rMemberType)
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
helper class for accessing resources shared by different libraries in the connectivity module
OUString getResourceString(TranslateId pResId) const
loads a string from the shared resource file
virtual void refreshGroups() override
Definition: HUser.cxx:55
virtual void SAL_CALL changePassword(const OUString &objPassword, const OUString &newPassword) override
Definition: HUser.cxx:261
virtual sal_Int32 SAL_CALL getPrivileges(const OUString &objName, sal_Int32 objType) override
Definition: HUser.cxx:82
static OUString getPrivilegeString(sal_Int32 nRights)
Definition: HUser.cxx:284
OHSQLUser(css::uno::Reference< css::sdbc::XConnection > _xConnection)
Definition: HUser.cxx:41
virtual void SAL_CALL revokePrivileges(const OUString &objName, sal_Int32 objType, sal_Int32 objPrivileges) override
Definition: HUser.cxx:234
css::uno::Reference< css::sdbc::XConnection > m_xConnection
Definition: HUser.hxx:31
virtual void SAL_CALL grantPrivileges(const OUString &objName, sal_Int32 objType, sal_Int32 objPrivileges) override
Definition: HUser.cxx:207
virtual sal_Int32 SAL_CALL getGrantablePrivileges(const OUString &objName, sal_Int32 objType) override
Definition: HUser.cxx:197
void findPrivilegesAndGrantPrivileges(const OUString &objName, sal_Int32 objType, sal_Int32 &nRights, sal_Int32 &nRightsWithGrant)
Definition: HUser.cxx:92
OUserExtend(const css::uno::Reference< css::sdbc::XConnection > &_xConnection)
Definition: HUser.cxx:59
virtual ::cppu::IPropertyArrayHelper * createArrayHelper() const override
Definition: HUser.cxx:69
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
Definition: HUser.cxx:76
virtual void construct() override
Definition: HUser.cxx:64
mutable::osl::Mutex m_aMutex
::cppu::WeakComponentImplHelper< css::sdbcx::XUser, css::sdbcx::XGroupsSupplier, css::container::XNamed, css::lang::XServiceInfo > OUser_BASE
Definition: VUser.hxx:41
void checkDisposed(bool _bThrow)
Definition: dbtools.cxx:1951
OUString quoteTableName(const Reference< XDatabaseMetaData > &_rxMeta, const OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:853
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
OUString quoteName(std::u16string_view _rQuote, const OUString &_rName)
quote the given name with the given quote string.
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.
Reference< XConnection > m_xConnection
#define PROPERTY_ID_PASSWORD
Definition: propertyids.hxx:74
OUString Name