Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / hsqldb / HUser.cxx
blob41a97c09422b11b3af8ec2aa47314de05cd8e368
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HUser.cxx,v $
10 * $Revision: 1.6.56.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "hsqldb/HUser.hxx"
34 #include <com/sun/star/sdbc/XRow.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include "connectivity/dbtools.hxx"
37 #include "connectivity/dbexception.hxx"
38 #include <com/sun/star/sdbcx/Privilege.hpp>
39 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
40 #include "TConnection.hxx"
41 #include "resource/hsqldb_res.hrc"
43 using namespace connectivity;
44 using namespace connectivity::hsqldb;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::sdbcx;
48 using namespace ::com::sun::star::sdbc;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::lang;
51 // -------------------------------------------------------------------------
52 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
53 ,m_xConnection(_xConnection)
55 construct();
57 // -------------------------------------------------------------------------
58 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
59 const ::rtl::OUString& _Name
60 ) : connectivity::sdbcx::OUser(_Name,sal_True)
61 ,m_xConnection(_xConnection)
63 construct();
65 // -------------------------------------------------------------------------
66 void OHSQLUser::refreshGroups()
69 // -------------------------------------------------------------------------
70 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
72 construct();
74 // -------------------------------------------------------------------------
75 typedef connectivity::sdbcx::OUser OUser_TYPEDEF;
76 void OUserExtend::construct()
78 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
80 // -----------------------------------------------------------------------------
81 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
83 Sequence< Property > aProps;
84 describeProperties(aProps);
85 return new cppu::OPropertyArrayHelper(aProps);
87 // -------------------------------------------------------------------------
88 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
90 return *OUserExtend_PROP::getArrayHelper();
92 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
93 // -----------------------------------------------------------------------------
94 sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
96 ::osl::MutexGuard aGuard(m_aMutex);
97 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
99 sal_Int32 nRights,nRightsWithGrant;
100 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
101 return nRights;
103 // -----------------------------------------------------------------------------
104 void OHSQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
106 nRightsWithGrant = nRights = 0;
107 // first we need to create the sql stmt to select the privs
108 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
109 ::rtl::OUString sCatalog,sSchema,sTable;
110 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
111 Reference<XResultSet> xRes;
112 switch(objType)
114 case PrivilegeObject::TABLE:
115 case PrivilegeObject::VIEW:
117 Any aCatalog;
118 if ( sCatalog.getLength() )
119 aCatalog <<= sCatalog;
120 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
122 break;
124 case PrivilegeObject::COLUMN:
126 Any aCatalog;
127 if ( sCatalog.getLength() )
128 aCatalog <<= sCatalog;
129 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")));
131 break;
134 if ( xRes.is() )
136 static const ::rtl::OUString sSELECT = ::rtl::OUString::createFromAscii("SELECT");
137 static const ::rtl::OUString sINSERT = ::rtl::OUString::createFromAscii("INSERT");
138 static const ::rtl::OUString sUPDATE = ::rtl::OUString::createFromAscii("UPDATE");
139 static const ::rtl::OUString sDELETE = ::rtl::OUString::createFromAscii("DELETE");
140 static const ::rtl::OUString sREAD = ::rtl::OUString::createFromAscii("READ");
141 static const ::rtl::OUString sCREATE = ::rtl::OUString::createFromAscii("CREATE");
142 static const ::rtl::OUString sALTER = ::rtl::OUString::createFromAscii("ALTER");
143 static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
144 static const ::rtl::OUString sDROP = ::rtl::OUString::createFromAscii("DROP");
145 static const ::rtl::OUString sYes = ::rtl::OUString::createFromAscii("YES");
147 nRightsWithGrant = nRights = 0;
149 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
150 while( xCurrentRow.is() && xRes->next() )
152 ::rtl::OUString sGrantee = xCurrentRow->getString(5);
153 ::rtl::OUString sPrivilege = xCurrentRow->getString(6);
154 ::rtl::OUString sGrantable = xCurrentRow->getString(7);
156 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
157 continue;
159 if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
161 nRights |= Privilege::SELECT;
162 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
163 nRightsWithGrant |= Privilege::SELECT;
165 else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
167 nRights |= Privilege::INSERT;
168 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
169 nRightsWithGrant |= Privilege::INSERT;
171 else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
173 nRights |= Privilege::UPDATE;
174 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
175 nRightsWithGrant |= Privilege::UPDATE;
177 else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
179 nRights |= Privilege::DELETE;
180 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
181 nRightsWithGrant |= Privilege::DELETE;
183 else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
185 nRights |= Privilege::READ;
186 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
187 nRightsWithGrant |= Privilege::READ;
189 else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
191 nRights |= Privilege::CREATE;
192 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
193 nRightsWithGrant |= Privilege::CREATE;
195 else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
197 nRights |= Privilege::ALTER;
198 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
199 nRightsWithGrant |= Privilege::ALTER;
201 else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
203 nRights |= Privilege::REFERENCE;
204 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
205 nRightsWithGrant |= Privilege::REFERENCE;
207 else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
209 nRights |= Privilege::DROP;
210 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
211 nRightsWithGrant |= Privilege::DROP;
214 ::comphelper::disposeComponent(xRes);
217 // -------------------------------------------------------------------------
218 sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
220 ::osl::MutexGuard aGuard(m_aMutex);
221 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
223 sal_Int32 nRights,nRightsWithGrant;
224 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
225 return nRightsWithGrant;
227 // -------------------------------------------------------------------------
228 void SAL_CALL OHSQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
230 if ( objType != PrivilegeObject::TABLE )
232 ::connectivity::SharedResources aResources;
233 const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
234 ::dbtools::throwGenericSQLException(sError,*this);
235 } // if ( objType != PrivilegeObject::TABLE )
238 ::osl::MutexGuard aGuard(m_aMutex);
240 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
241 if(sPrivs.getLength())
243 ::rtl::OUString sGrant;
244 sGrant += ::rtl::OUString::createFromAscii("GRANT ");
245 sGrant += sPrivs;
246 sGrant += ::rtl::OUString::createFromAscii(" ON ");
247 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
248 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
249 sGrant += ::rtl::OUString::createFromAscii(" TO ");
250 sGrant += m_Name;
252 Reference<XStatement> xStmt = m_xConnection->createStatement();
253 if(xStmt.is())
254 xStmt->execute(sGrant);
255 ::comphelper::disposeComponent(xStmt);
258 // -------------------------------------------------------------------------
259 void SAL_CALL OHSQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
261 if ( objType != PrivilegeObject::TABLE )
263 ::connectivity::SharedResources aResources;
264 const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
265 ::dbtools::throwGenericSQLException(sError,*this);
266 } // if ( objType != PrivilegeObject::TABLE )
268 ::osl::MutexGuard aGuard(m_aMutex);
269 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
270 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
271 if(sPrivs.getLength())
273 ::rtl::OUString sGrant;
274 sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
275 sGrant += sPrivs;
276 sGrant += ::rtl::OUString::createFromAscii(" ON ");
277 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
278 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
279 sGrant += ::rtl::OUString::createFromAscii(" FROM ");
280 sGrant += m_Name;
282 Reference<XStatement> xStmt = m_xConnection->createStatement();
283 if(xStmt.is())
284 xStmt->execute(sGrant);
285 ::comphelper::disposeComponent(xStmt);
288 // -----------------------------------------------------------------------------
289 // XUser
290 void SAL_CALL OHSQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
292 ::osl::MutexGuard aGuard(m_aMutex);
293 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
294 ::rtl::OUString sAlterPwd;
295 sAlterPwd = ::rtl::OUString::createFromAscii("SET PASSWORD FOR ");
296 sAlterPwd += m_Name;
297 sAlterPwd += ::rtl::OUString::createFromAscii("@\"%\" = PASSWORD('") ;
298 sAlterPwd += newPassword;
299 sAlterPwd += ::rtl::OUString::createFromAscii("')") ;
302 Reference<XStatement> xStmt = m_xConnection->createStatement();
303 if ( xStmt.is() )
305 xStmt->execute(sAlterPwd);
306 ::comphelper::disposeComponent(xStmt);
309 // -----------------------------------------------------------------------------
310 ::rtl::OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
312 ::rtl::OUString sPrivs;
313 if((nRights & Privilege::INSERT) == Privilege::INSERT)
314 sPrivs += ::rtl::OUString::createFromAscii("INSERT");
316 if((nRights & Privilege::DELETE) == Privilege::DELETE)
318 if(sPrivs.getLength())
319 sPrivs += ::rtl::OUString::createFromAscii(",");
320 sPrivs += ::rtl::OUString::createFromAscii("DELETE");
323 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
325 if(sPrivs.getLength())
326 sPrivs += ::rtl::OUString::createFromAscii(",");
327 sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
330 if((nRights & Privilege::ALTER) == Privilege::ALTER)
332 if(sPrivs.getLength())
333 sPrivs += ::rtl::OUString::createFromAscii(",");
334 sPrivs += ::rtl::OUString::createFromAscii("ALTER");
337 if((nRights & Privilege::SELECT) == Privilege::SELECT)
339 if(sPrivs.getLength())
340 sPrivs += ::rtl::OUString::createFromAscii(",");
341 sPrivs += ::rtl::OUString::createFromAscii("SELECT");
344 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
346 if(sPrivs.getLength())
347 sPrivs += ::rtl::OUString::createFromAscii(",");
348 sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
351 return sPrivs;
353 // -----------------------------------------------------------------------------