bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HUser.cxx
blob2ed0c06262ff9d05550337361b9f4fc3067e4b6c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <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>
31 using namespace connectivity;
32 using namespace connectivity::hsqldb;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::sdbcx;
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::lang;
40 OHSQLUser::OHSQLUser( const css::uno::Reference< css::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(true)
41 ,m_xConnection(_xConnection)
43 construct();
46 OHSQLUser::OHSQLUser( const css::uno::Reference< css::sdbc::XConnection >& _xConnection,
47 const OUString& Name
48 ) : connectivity::sdbcx::OUser(Name,true)
49 ,m_xConnection(_xConnection)
51 construct();
54 void OHSQLUser::refreshGroups()
58 OUserExtend::OUserExtend( const css::uno::Reference< css::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
60 construct();
63 void OUserExtend::construct()
65 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::cppu::UnoType<OUString>::get());
68 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
70 Sequence< Property > aProps;
71 describeProperties(aProps);
72 return new cppu::OPropertyArrayHelper(aProps);
75 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
77 return *OUserExtend_PROP::getArrayHelper();
79 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
81 sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const OUString& objName, sal_Int32 objType )
83 ::osl::MutexGuard aGuard(m_aMutex);
84 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
86 sal_Int32 nRights,nRightsWithGrant;
87 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
88 return nRights;
91 void OHSQLUser::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant)
93 nRightsWithGrant = nRights = 0;
94 // first we need to create the sql stmt to select the privs
95 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
96 OUString sCatalog,sSchema,sTable;
97 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
98 Reference<XResultSet> xRes;
99 switch(objType)
101 case PrivilegeObject::TABLE:
102 case PrivilegeObject::VIEW:
104 Any aCatalog;
105 if ( !sCatalog.isEmpty() )
106 aCatalog <<= sCatalog;
107 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
109 break;
111 case PrivilegeObject::COLUMN:
113 Any aCatalog;
114 if ( !sCatalog.isEmpty() )
115 aCatalog <<= sCatalog;
116 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,"%");
118 break;
121 if ( !xRes.is() )
122 return;
124 static const char sYes [] = "YES";
126 nRightsWithGrant = nRights = 0;
128 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
129 while( xCurrentRow.is() && xRes->next() )
131 OUString sGrantee = xCurrentRow->getString(5);
132 OUString sPrivilege = xCurrentRow->getString(6);
133 OUString sGrantable = xCurrentRow->getString(7);
135 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
136 continue;
138 if (sPrivilege.equalsIgnoreAsciiCase("SELECT"))
140 nRights |= Privilege::SELECT;
141 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
142 nRightsWithGrant |= Privilege::SELECT;
144 else if (sPrivilege.equalsIgnoreAsciiCase("INSERT"))
146 nRights |= Privilege::INSERT;
147 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
148 nRightsWithGrant |= Privilege::INSERT;
150 else if (sPrivilege.equalsIgnoreAsciiCase("UPDATE"))
152 nRights |= Privilege::UPDATE;
153 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
154 nRightsWithGrant |= Privilege::UPDATE;
156 else if (sPrivilege.equalsIgnoreAsciiCase("DELETE"))
158 nRights |= Privilege::DELETE;
159 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
160 nRightsWithGrant |= Privilege::DELETE;
162 else if (sPrivilege.equalsIgnoreAsciiCase("READ"))
164 nRights |= Privilege::READ;
165 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
166 nRightsWithGrant |= Privilege::READ;
168 else if (sPrivilege.equalsIgnoreAsciiCase("CREATE"))
170 nRights |= Privilege::CREATE;
171 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
172 nRightsWithGrant |= Privilege::CREATE;
174 else if (sPrivilege.equalsIgnoreAsciiCase("ALTER"))
176 nRights |= Privilege::ALTER;
177 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
178 nRightsWithGrant |= Privilege::ALTER;
180 else if (sPrivilege.equalsIgnoreAsciiCase("REFERENCE"))
182 nRights |= Privilege::REFERENCE;
183 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
184 nRightsWithGrant |= Privilege::REFERENCE;
186 else if (sPrivilege.equalsIgnoreAsciiCase("DROP"))
188 nRights |= Privilege::DROP;
189 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
190 nRightsWithGrant |= Privilege::DROP;
193 ::comphelper::disposeComponent(xRes);
196 sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType )
198 ::osl::MutexGuard aGuard(m_aMutex);
199 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
201 sal_Int32 nRights,nRightsWithGrant;
202 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
203 return nRightsWithGrant;
206 void SAL_CALL OHSQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges )
208 if ( objType != PrivilegeObject::TABLE )
210 ::connectivity::SharedResources aResources;
211 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
212 ::dbtools::throwGenericSQLException(sError,*this);
213 } // if ( objType != PrivilegeObject::TABLE )
216 ::osl::MutexGuard aGuard(m_aMutex);
218 OUString sPrivs = getPrivilegeString(objPrivileges);
219 if(!sPrivs.isEmpty())
221 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
222 OUString sGrant = "GRANT " + sPrivs +
223 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
224 " TO " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
226 Reference<XStatement> xStmt = m_xConnection->createStatement();
227 if(xStmt.is())
228 xStmt->execute(sGrant);
229 ::comphelper::disposeComponent(xStmt);
233 void SAL_CALL OHSQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges )
235 if ( objType != PrivilegeObject::TABLE )
237 ::connectivity::SharedResources aResources;
238 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
239 ::dbtools::throwGenericSQLException(sError,*this);
240 } // if ( objType != PrivilegeObject::TABLE )
242 ::osl::MutexGuard aGuard(m_aMutex);
243 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
244 OUString sPrivs = getPrivilegeString(objPrivileges);
245 if(!sPrivs.isEmpty())
247 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
248 OUString sGrant = "REVOKE " + sPrivs +
249 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
250 " FROM " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
252 Reference<XStatement> xStmt = m_xConnection->createStatement();
253 if(xStmt.is())
254 xStmt->execute(sGrant);
255 ::comphelper::disposeComponent(xStmt);
259 // XUser
260 void SAL_CALL OHSQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword )
262 ::osl::MutexGuard aGuard(m_aMutex);
263 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
265 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
267 if( m_Name != xMeta->getUserName() )
269 ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
272 OUString sAlterPwd = "SET PASSWORD " +
273 ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), newPassword);
275 Reference<XStatement> xStmt = m_xConnection->createStatement();
276 if ( xStmt.is() )
278 xStmt->execute(sAlterPwd);
279 ::comphelper::disposeComponent(xStmt);
283 OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights)
285 OUString sPrivs;
286 if((nRights & Privilege::INSERT) == Privilege::INSERT)
287 sPrivs += "INSERT";
289 if((nRights & Privilege::DELETE) == Privilege::DELETE)
291 if(!sPrivs.isEmpty())
292 sPrivs += ",";
293 sPrivs += "DELETE";
296 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
298 if(!sPrivs.isEmpty())
299 sPrivs += ",";
300 sPrivs += "UPDATE";
303 if((nRights & Privilege::ALTER) == Privilege::ALTER)
305 if(!sPrivs.isEmpty())
306 sPrivs += ",";
307 sPrivs += "ALTER";
310 if((nRights & Privilege::SELECT) == Privilege::SELECT)
312 if(!sPrivs.isEmpty())
313 sPrivs += ",";
314 sPrivs += "SELECT";
317 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
319 if(!sPrivs.isEmpty())
320 sPrivs += ",";
321 sPrivs += "REFERENCES";
324 return sPrivs;
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */