Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HUser.cxx
blobdf04d379cec9e3a4d27bfe009975dd320cc6d65f
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 <com/sun/star/sdbcx/Privilege.hpp>
26 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
27 #include "TConnection.hxx"
28 #include "resource/hsqldb_res.hrc"
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;
39 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(true)
40 ,m_xConnection(_xConnection)
42 construct();
45 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 ,m_xConnection(_xConnection)
50 construct();
53 void OHSQLUser::refreshGroups()
57 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
59 construct();
62 void OUserExtend::construct()
64 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::cppu::UnoType<OUString>::get());
67 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
69 Sequence< Property > aProps;
70 describeProperties(aProps);
71 return new cppu::OPropertyArrayHelper(aProps);
74 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
76 return *OUserExtend_PROP::getArrayHelper();
78 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
80 sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
82 ::osl::MutexGuard aGuard(m_aMutex);
83 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
85 sal_Int32 nRights,nRightsWithGrant;
86 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
87 return nRights;
90 void OHSQLUser::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
92 nRightsWithGrant = nRights = 0;
93 // first we need to create the sql stmt to select the privs
94 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
95 OUString sCatalog,sSchema,sTable;
96 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
97 Reference<XResultSet> xRes;
98 switch(objType)
100 case PrivilegeObject::TABLE:
101 case PrivilegeObject::VIEW:
103 Any aCatalog;
104 if ( !sCatalog.isEmpty() )
105 aCatalog <<= sCatalog;
106 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
108 break;
110 case PrivilegeObject::COLUMN:
112 Any aCatalog;
113 if ( !sCatalog.isEmpty() )
114 aCatalog <<= sCatalog;
115 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,OUString("%"));
117 break;
120 if ( xRes.is() )
122 static const OUString sSELECT( "SELECT" );
123 static const OUString sINSERT( "INSERT" );
124 static const OUString sUPDATE( "UPDATE" );
125 static const OUString sDELETE( "DELETE" );
126 static const OUString sREAD( "READ" );
127 static const OUString sCREATE( "CREATE" );
128 static const OUString sALTER( "ALTER" );
129 static const OUString sREFERENCE( "REFERENCE" );
130 static const OUString sDROP( "DROP" );
131 static const OUString sYes( "YES" );
133 nRightsWithGrant = nRights = 0;
135 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
136 while( xCurrentRow.is() && xRes->next() )
138 OUString sGrantee = xCurrentRow->getString(5);
139 OUString sPrivilege = xCurrentRow->getString(6);
140 OUString sGrantable = xCurrentRow->getString(7);
142 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
143 continue;
145 if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
147 nRights |= Privilege::SELECT;
148 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
149 nRightsWithGrant |= Privilege::SELECT;
151 else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
153 nRights |= Privilege::INSERT;
154 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
155 nRightsWithGrant |= Privilege::INSERT;
157 else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
159 nRights |= Privilege::UPDATE;
160 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
161 nRightsWithGrant |= Privilege::UPDATE;
163 else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
165 nRights |= Privilege::DELETE;
166 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
167 nRightsWithGrant |= Privilege::DELETE;
169 else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
171 nRights |= Privilege::READ;
172 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
173 nRightsWithGrant |= Privilege::READ;
175 else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
177 nRights |= Privilege::CREATE;
178 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
179 nRightsWithGrant |= Privilege::CREATE;
181 else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
183 nRights |= Privilege::ALTER;
184 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
185 nRightsWithGrant |= Privilege::ALTER;
187 else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
189 nRights |= Privilege::REFERENCE;
190 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
191 nRightsWithGrant |= Privilege::REFERENCE;
193 else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
195 nRights |= Privilege::DROP;
196 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
197 nRightsWithGrant |= Privilege::DROP;
200 ::comphelper::disposeComponent(xRes);
204 sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
206 ::osl::MutexGuard aGuard(m_aMutex);
207 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
209 sal_Int32 nRights,nRightsWithGrant;
210 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
211 return nRightsWithGrant;
214 void SAL_CALL OHSQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
216 if ( objType != PrivilegeObject::TABLE )
218 ::connectivity::SharedResources aResources;
219 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
220 ::dbtools::throwGenericSQLException(sError,*this);
221 } // if ( objType != PrivilegeObject::TABLE )
224 ::osl::MutexGuard aGuard(m_aMutex);
226 OUString sPrivs = getPrivilegeString(objPrivileges);
227 if(!sPrivs.isEmpty())
229 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
230 OUString sGrant = "GRANT " + sPrivs +
231 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
232 " TO " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
234 Reference<XStatement> xStmt = m_xConnection->createStatement();
235 if(xStmt.is())
236 xStmt->execute(sGrant);
237 ::comphelper::disposeComponent(xStmt);
241 void SAL_CALL OHSQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
243 if ( objType != PrivilegeObject::TABLE )
245 ::connectivity::SharedResources aResources;
246 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
247 ::dbtools::throwGenericSQLException(sError,*this);
248 } // if ( objType != PrivilegeObject::TABLE )
250 ::osl::MutexGuard aGuard(m_aMutex);
251 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
252 OUString sPrivs = getPrivilegeString(objPrivileges);
253 if(!sPrivs.isEmpty())
255 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
256 OUString sGrant = "REVOKE " + sPrivs +
257 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
258 " FROM " + ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), m_Name);
260 Reference<XStatement> xStmt = m_xConnection->createStatement();
261 if(xStmt.is())
262 xStmt->execute(sGrant);
263 ::comphelper::disposeComponent(xStmt);
267 // XUser
268 void SAL_CALL OHSQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException, std::exception)
270 ::osl::MutexGuard aGuard(m_aMutex);
271 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
273 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
275 if( m_Name != xMeta->getUserName() )
277 ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
280 OUString sAlterPwd = "SET PASSWORD " +
281 ::dbtools::quoteName(xMeta->getIdentifierQuoteString(), newPassword);
283 Reference<XStatement> xStmt = m_xConnection->createStatement();
284 if ( xStmt.is() )
286 xStmt->execute(sAlterPwd);
287 ::comphelper::disposeComponent(xStmt);
291 OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
293 OUString sPrivs;
294 if((nRights & Privilege::INSERT) == Privilege::INSERT)
295 sPrivs += "INSERT";
297 if((nRights & Privilege::DELETE) == Privilege::DELETE)
299 if(!sPrivs.isEmpty())
300 sPrivs += ",";
301 sPrivs += "DELETE";
304 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
306 if(!sPrivs.isEmpty())
307 sPrivs += ",";
308 sPrivs += "UPDATE";
311 if((nRights & Privilege::ALTER) == Privilege::ALTER)
313 if(!sPrivs.isEmpty())
314 sPrivs += ",";
315 sPrivs += "ALTER";
318 if((nRights & Privilege::SELECT) == Privilege::SELECT)
320 if(!sPrivs.isEmpty())
321 sPrivs += ",";
322 sPrivs += "SELECT";
325 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
327 if(!sPrivs.isEmpty())
328 sPrivs += ",";
329 sPrivs += "REFERENCES";
332 return sPrivs;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */