bump product version to 4.1.6.2
[LibreOffice.git] / connectivity / source / drivers / mysql / YUser.cxx
blob5f300a3b06722fc65d13cfd132e3597e3ccc57cb
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 "mysql/YUser.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/common_res.hrc"
30 using namespace connectivity;
31 using namespace connectivity::mysql;
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;
38 // -------------------------------------------------------------------------
39 OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
40 ,m_xConnection(_xConnection)
42 construct();
44 // -------------------------------------------------------------------------
45 OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
46 const OUString& _Name
47 ) : connectivity::sdbcx::OUser(_Name,sal_True)
48 ,m_xConnection(_xConnection)
50 construct();
52 // -------------------------------------------------------------------------
53 void OMySQLUser::refreshGroups()
56 // -------------------------------------------------------------------------
57 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OMySQLUser(_xConnection)
59 construct();
61 // -------------------------------------------------------------------------
62 void OUserExtend::construct()
64 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< OUString*>(0)));
66 // -----------------------------------------------------------------------------
67 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
69 Sequence< Property > aProps;
70 describeProperties(aProps);
71 return new cppu::OPropertyArrayHelper(aProps);
73 // -------------------------------------------------------------------------
74 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
76 return *OUserExtend_PROP::getArrayHelper();
78 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
79 // -----------------------------------------------------------------------------
80 sal_Int32 SAL_CALL OMySQLUser::getPrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
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;
89 // -----------------------------------------------------------------------------
90 void OMySQLUser::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);
203 // -------------------------------------------------------------------------
204 sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
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;
213 // -------------------------------------------------------------------------
214 void SAL_CALL OMySQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
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 )
223 ::osl::MutexGuard aGuard(m_aMutex);
225 OUString sPrivs = getPrivilegeString(objPrivileges);
226 if(!sPrivs.isEmpty())
228 OUString sGrant;
229 sGrant += OUString("GRANT ");
230 sGrant += sPrivs;
231 sGrant += OUString(" ON ");
232 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
233 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
234 sGrant += OUString(" TO ");
235 sGrant += m_Name;
237 Reference<XStatement> xStmt = m_xConnection->createStatement();
238 if(xStmt.is())
239 xStmt->execute(sGrant);
240 ::comphelper::disposeComponent(xStmt);
243 // -------------------------------------------------------------------------
244 void SAL_CALL OMySQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
246 if ( objType != PrivilegeObject::TABLE )
248 ::connectivity::SharedResources aResources;
249 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
250 ::dbtools::throwGenericSQLException(sError,*this);
253 ::osl::MutexGuard aGuard(m_aMutex);
254 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
255 OUString sPrivs = getPrivilegeString(objPrivileges);
256 if(!sPrivs.isEmpty())
258 OUString sGrant;
259 sGrant += OUString("REVOKE ");
260 sGrant += sPrivs;
261 sGrant += OUString(" ON ");
262 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
263 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
264 sGrant += OUString(" FROM ");
265 sGrant += m_Name;
267 Reference<XStatement> xStmt = m_xConnection->createStatement();
268 if(xStmt.is())
269 xStmt->execute(sGrant);
270 ::comphelper::disposeComponent(xStmt);
273 // -----------------------------------------------------------------------------
274 // XUser
275 void SAL_CALL OMySQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException)
277 ::osl::MutexGuard aGuard(m_aMutex);
278 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
279 OUString sAlterPwd;
280 sAlterPwd = OUString("SET PASSWORD FOR ");
281 sAlterPwd += m_Name;
282 sAlterPwd += OUString("@\"%\" = PASSWORD('") ;
283 sAlterPwd += newPassword;
284 sAlterPwd += OUString("')") ;
287 Reference<XStatement> xStmt = m_xConnection->createStatement();
288 if ( xStmt.is() )
290 xStmt->execute(sAlterPwd);
291 ::comphelper::disposeComponent(xStmt);
294 // -----------------------------------------------------------------------------
295 OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights) const
297 OUString sPrivs;
298 if((nRights & Privilege::INSERT) == Privilege::INSERT)
299 sPrivs += OUString("INSERT");
301 if((nRights & Privilege::DELETE) == Privilege::DELETE)
303 if(!sPrivs.isEmpty())
304 sPrivs += OUString(",");
305 sPrivs += OUString("DELETE");
308 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
310 if(!sPrivs.isEmpty())
311 sPrivs += OUString(",");
312 sPrivs += OUString("UPDATE");
315 if((nRights & Privilege::ALTER) == Privilege::ALTER)
317 if(!sPrivs.isEmpty())
318 sPrivs += OUString(",");
319 sPrivs += OUString("ALTER");
322 if((nRights & Privilege::SELECT) == Privilege::SELECT)
324 if(!sPrivs.isEmpty())
325 sPrivs += OUString(",");
326 sPrivs += OUString("SELECT");
329 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
331 if(!sPrivs.isEmpty())
332 sPrivs += OUString(",");
333 sPrivs += OUString("REFERENCES");
336 return sPrivs;
338 // -----------------------------------------------------------------------------
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */