1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "hsqldb/HUser.hxx"
27 #include <com/sun/star/sdbc/XRow.hpp>
28 #include <com/sun/star/sdbc/XResultSet.hpp>
29 #include "connectivity/dbtools.hxx"
30 #include "connectivity/dbexception.hxx"
31 #include <com/sun/star/sdbcx/Privilege.hpp>
32 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
33 #include "TConnection.hxx"
34 #include "resource/hsqldb_res.hrc"
35
36 using namespace connectivity;
37 using namespace connectivity::hsqldb;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::sdbcx;
41 using namespace ::com::sun::star::sdbc;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::lang;
44 // -------------------------------------------------------------------------
OHSQLUser(const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XConnection> & _xConnection)45 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
46 ,m_xConnection(_xConnection)
47 {
48 construct();
49 }
50 // -------------------------------------------------------------------------
OHSQLUser(const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XConnection> & _xConnection,const::rtl::OUString & _Name)51 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
52 const ::rtl::OUString& _Name
53 ) : connectivity::sdbcx::OUser(_Name,sal_True)
54 ,m_xConnection(_xConnection)
55 {
56 construct();
57 }
58 // -------------------------------------------------------------------------
refreshGroups()59 void OHSQLUser::refreshGroups()
60 {
61 }
62 // -------------------------------------------------------------------------
OUserExtend(const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XConnection> & _xConnection)63 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OHSQLUser(_xConnection)
64 {
65 construct();
66 }
67 // -------------------------------------------------------------------------
68 typedef connectivity::sdbcx::OUser OUser_TYPEDEF;
construct()69 void OUserExtend::construct()
70 {
71 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< ::rtl::OUString*>(NULL)));
72 }
73 // -----------------------------------------------------------------------------
createArrayHelper() const74 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
75 {
76 Sequence< Property > aProps;
77 describeProperties(aProps);
78 return new cppu::OPropertyArrayHelper(aProps);
79 }
80 // -------------------------------------------------------------------------
getInfoHelper()81 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
82 {
83 return *OUserExtend_PROP::getArrayHelper();
84 }
85 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
86 // -----------------------------------------------------------------------------
getPrivileges(const::rtl::OUString & objName,sal_Int32 objType)87 sal_Int32 SAL_CALL OHSQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
88 {
89 ::osl::MutexGuard aGuard(m_aMutex);
90 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
91
92 sal_Int32 nRights,nRightsWithGrant;
93 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
94 return nRights;
95 }
96 // -----------------------------------------------------------------------------
findPrivilegesAndGrantPrivileges(const::rtl::OUString & objName,sal_Int32 objType,sal_Int32 & nRights,sal_Int32 & nRightsWithGrant)97 void OHSQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
98 {
99 nRightsWithGrant = nRights = 0;
100 // first we need to create the sql stmt to select the privs
101 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
102 ::rtl::OUString sCatalog,sSchema,sTable;
103 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
104 Reference<XResultSet> xRes;
105 switch(objType)
106 {
107 case PrivilegeObject::TABLE:
108 case PrivilegeObject::VIEW:
109 {
110 Any aCatalog;
111 if ( sCatalog.getLength() )
112 aCatalog <<= sCatalog;
113 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
114 }
115 break;
116
117 case PrivilegeObject::COLUMN:
118 {
119 Any aCatalog;
120 if ( sCatalog.getLength() )
121 aCatalog <<= sCatalog;
122 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")));
123 }
124 break;
125 }
126
127 if ( xRes.is() )
128 {
129 static const ::rtl::OUString sSELECT = ::rtl::OUString::createFromAscii("SELECT");
130 static const ::rtl::OUString sINSERT = ::rtl::OUString::createFromAscii("INSERT");
131 static const ::rtl::OUString sUPDATE = ::rtl::OUString::createFromAscii("UPDATE");
132 static const ::rtl::OUString sDELETE = ::rtl::OUString::createFromAscii("DELETE");
133 static const ::rtl::OUString sREAD = ::rtl::OUString::createFromAscii("READ");
134 static const ::rtl::OUString sCREATE = ::rtl::OUString::createFromAscii("CREATE");
135 static const ::rtl::OUString sALTER = ::rtl::OUString::createFromAscii("ALTER");
136 static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
137 static const ::rtl::OUString sDROP = ::rtl::OUString::createFromAscii("DROP");
138 static const ::rtl::OUString sYes = ::rtl::OUString::createFromAscii("YES");
139
140 nRightsWithGrant = nRights = 0;
141
142 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
143 while( xCurrentRow.is() && xRes->next() )
144 {
145 ::rtl::OUString sGrantee = xCurrentRow->getString(5);
146 ::rtl::OUString sPrivilege = xCurrentRow->getString(6);
147 ::rtl::OUString sGrantable = xCurrentRow->getString(7);
148
149 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
150 continue;
151
152 if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
153 {
154 nRights |= Privilege::SELECT;
155 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
156 nRightsWithGrant |= Privilege::SELECT;
157 }
158 else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
159 {
160 nRights |= Privilege::INSERT;
161 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
162 nRightsWithGrant |= Privilege::INSERT;
163 }
164 else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
165 {
166 nRights |= Privilege::UPDATE;
167 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
168 nRightsWithGrant |= Privilege::UPDATE;
169 }
170 else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
171 {
172 nRights |= Privilege::DELETE;
173 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
174 nRightsWithGrant |= Privilege::DELETE;
175 }
176 else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
177 {
178 nRights |= Privilege::READ;
179 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
180 nRightsWithGrant |= Privilege::READ;
181 }
182 else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
183 {
184 nRights |= Privilege::CREATE;
185 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
186 nRightsWithGrant |= Privilege::CREATE;
187 }
188 else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
189 {
190 nRights |= Privilege::ALTER;
191 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
192 nRightsWithGrant |= Privilege::ALTER;
193 }
194 else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
195 {
196 nRights |= Privilege::REFERENCE;
197 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
198 nRightsWithGrant |= Privilege::REFERENCE;
199 }
200 else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
201 {
202 nRights |= Privilege::DROP;
203 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
204 nRightsWithGrant |= Privilege::DROP;
205 }
206 }
207 ::comphelper::disposeComponent(xRes);
208 }
209 }
210 // -------------------------------------------------------------------------
getGrantablePrivileges(const::rtl::OUString & objName,sal_Int32 objType)211 sal_Int32 SAL_CALL OHSQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
212 {
213 ::osl::MutexGuard aGuard(m_aMutex);
214 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
215
216 sal_Int32 nRights,nRightsWithGrant;
217 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
218 return nRightsWithGrant;
219 }
220 // -------------------------------------------------------------------------
grantPrivileges(const::rtl::OUString & objName,sal_Int32 objType,sal_Int32 objPrivileges)221 void SAL_CALL OHSQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
222 {
223 if ( objType != PrivilegeObject::TABLE )
224 {
225 ::connectivity::SharedResources aResources;
226 const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
227 ::dbtools::throwGenericSQLException(sError,*this);
228 } // if ( objType != PrivilegeObject::TABLE )
229
230
231 ::osl::MutexGuard aGuard(m_aMutex);
232
233 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
234 if(sPrivs.getLength())
235 {
236 ::rtl::OUString sGrant;
237 sGrant += ::rtl::OUString::createFromAscii("GRANT ");
238 sGrant += sPrivs;
239 sGrant += ::rtl::OUString::createFromAscii(" ON ");
240 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
241 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
242 sGrant += ::rtl::OUString::createFromAscii(" TO ");
243 sGrant += m_Name;
244
245 Reference<XStatement> xStmt = m_xConnection->createStatement();
246 if(xStmt.is())
247 xStmt->execute(sGrant);
248 ::comphelper::disposeComponent(xStmt);
249 }
250 }
251 // -------------------------------------------------------------------------
revokePrivileges(const::rtl::OUString & objName,sal_Int32 objType,sal_Int32 objPrivileges)252 void SAL_CALL OHSQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
253 {
254 if ( objType != PrivilegeObject::TABLE )
255 {
256 ::connectivity::SharedResources aResources;
257 const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
258 ::dbtools::throwGenericSQLException(sError,*this);
259 } // if ( objType != PrivilegeObject::TABLE )
260
261 ::osl::MutexGuard aGuard(m_aMutex);
262 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
263 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
264 if(sPrivs.getLength())
265 {
266 ::rtl::OUString sGrant;
267 sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
268 sGrant += sPrivs;
269 sGrant += ::rtl::OUString::createFromAscii(" ON ");
270 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
271 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
272 sGrant += ::rtl::OUString::createFromAscii(" FROM ");
273 sGrant += m_Name;
274
275 Reference<XStatement> xStmt = m_xConnection->createStatement();
276 if(xStmt.is())
277 xStmt->execute(sGrant);
278 ::comphelper::disposeComponent(xStmt);
279 }
280 }
281 // -----------------------------------------------------------------------------
282 // XUser
changePassword(const::rtl::OUString &,const::rtl::OUString & newPassword)283 void SAL_CALL OHSQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
284 {
285 ::osl::MutexGuard aGuard(m_aMutex);
286 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
287 ::rtl::OUString sAlterPwd;
288 sAlterPwd = ::rtl::OUString::createFromAscii("SET PASSWORD FOR ");
289 sAlterPwd += m_Name;
290 sAlterPwd += ::rtl::OUString::createFromAscii("@\"%\" = PASSWORD('") ;
291 sAlterPwd += newPassword;
292 sAlterPwd += ::rtl::OUString::createFromAscii("')") ;
293
294
295 Reference<XStatement> xStmt = m_xConnection->createStatement();
296 if ( xStmt.is() )
297 {
298 xStmt->execute(sAlterPwd);
299 ::comphelper::disposeComponent(xStmt);
300 }
301 }
302 // -----------------------------------------------------------------------------
getPrivilegeString(sal_Int32 nRights) const303 ::rtl::OUString OHSQLUser::getPrivilegeString(sal_Int32 nRights) const
304 {
305 ::rtl::OUString sPrivs;
306 if((nRights & Privilege::INSERT) == Privilege::INSERT)
307 sPrivs += ::rtl::OUString::createFromAscii("INSERT");
308
309 if((nRights & Privilege::DELETE) == Privilege::DELETE)
310 {
311 if(sPrivs.getLength())
312 sPrivs += ::rtl::OUString::createFromAscii(",");
313 sPrivs += ::rtl::OUString::createFromAscii("DELETE");
314 }
315
316 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
317 {
318 if(sPrivs.getLength())
319 sPrivs += ::rtl::OUString::createFromAscii(",");
320 sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
321 }
322
323 if((nRights & Privilege::ALTER) == Privilege::ALTER)
324 {
325 if(sPrivs.getLength())
326 sPrivs += ::rtl::OUString::createFromAscii(",");
327 sPrivs += ::rtl::OUString::createFromAscii("ALTER");
328 }
329
330 if((nRights & Privilege::SELECT) == Privilege::SELECT)
331 {
332 if(sPrivs.getLength())
333 sPrivs += ::rtl::OUString::createFromAscii(",");
334 sPrivs += ::rtl::OUString::createFromAscii("SELECT");
335 }
336
337 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
338 {
339 if(sPrivs.getLength())
340 sPrivs += ::rtl::OUString::createFromAscii(",");
341 sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
342 }
343
344 return sPrivs;
345 }
346 // -----------------------------------------------------------------------------
347
348