build fix
[LibreOffice.git] / connectivity / source / drivers / mysql / YUser.cxx
blobe386d9b385eda9cf23ae3bb2bc77c270c32292ac
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;
39 OMySQLUser::OMySQLUser( const css::uno::Reference< css::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(true)
40 ,m_xConnection(_xConnection)
42 construct();
45 OMySQLUser::OMySQLUser( const css::uno::Reference< css::sdbc::XConnection >& _xConnection,
46 const OUString& Name
47 ) : connectivity::sdbcx::OUser(Name,true)
48 ,m_xConnection(_xConnection)
50 construct();
53 void OMySQLUser::refreshGroups()
57 OUserExtend::OUserExtend( const css::uno::Reference< css::sdbc::XConnection >& _xConnection) : OMySQLUser(_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 OMySQLUser::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 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::EComposeRule::InDataManipulation);
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, "%");
117 break;
120 if ( xRes.is() )
122 static const char sYes [] = "YES";
124 nRightsWithGrant = nRights = 0;
126 Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
127 while( xCurrentRow.is() && xRes->next() )
129 OUString sGrantee = xCurrentRow->getString(5);
130 OUString sPrivilege = xCurrentRow->getString(6);
131 OUString sGrantable = xCurrentRow->getString(7);
133 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
134 continue;
136 if (sPrivilege.equalsIgnoreAsciiCase("SELECT"))
138 nRights |= Privilege::SELECT;
139 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
140 nRightsWithGrant |= Privilege::SELECT;
142 else if (sPrivilege.equalsIgnoreAsciiCase("INSERT"))
144 nRights |= Privilege::INSERT;
145 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
146 nRightsWithGrant |= Privilege::INSERT;
148 else if (sPrivilege.equalsIgnoreAsciiCase("UPDATE"))
150 nRights |= Privilege::UPDATE;
151 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
152 nRightsWithGrant |= Privilege::UPDATE;
154 else if (sPrivilege.equalsIgnoreAsciiCase("DELETE"))
156 nRights |= Privilege::DELETE;
157 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
158 nRightsWithGrant |= Privilege::DELETE;
160 else if (sPrivilege.equalsIgnoreAsciiCase("READ"))
162 nRights |= Privilege::READ;
163 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
164 nRightsWithGrant |= Privilege::READ;
166 else if (sPrivilege.equalsIgnoreAsciiCase("CREATE"))
168 nRights |= Privilege::CREATE;
169 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
170 nRightsWithGrant |= Privilege::CREATE;
172 else if (sPrivilege.equalsIgnoreAsciiCase("ALTER"))
174 nRights |= Privilege::ALTER;
175 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
176 nRightsWithGrant |= Privilege::ALTER;
178 else if (sPrivilege.equalsIgnoreAsciiCase("REFERENCES"))
180 nRights |= Privilege::REFERENCE;
181 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
182 nRightsWithGrant |= Privilege::REFERENCE;
184 else if (sPrivilege.equalsIgnoreAsciiCase("DROP"))
186 nRights |= Privilege::DROP;
187 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
188 nRightsWithGrant |= Privilege::DROP;
191 ::comphelper::disposeComponent(xRes);
195 sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
197 ::osl::MutexGuard aGuard(m_aMutex);
198 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
200 sal_Int32 nRights,nRightsWithGrant;
201 findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
202 return nRightsWithGrant;
205 void SAL_CALL OMySQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
207 if ( objType != PrivilegeObject::TABLE )
209 ::connectivity::SharedResources aResources;
210 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
211 ::dbtools::throwGenericSQLException(sError,*this);
212 } // if ( objType != PrivilegeObject::TABLE )
214 ::osl::MutexGuard aGuard(m_aMutex);
216 OUString sPrivs = getPrivilegeString(objPrivileges);
217 if(!sPrivs.isEmpty())
219 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
220 OUString sGrant = "GRANT " + sPrivs +
221 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
222 " TO " + m_Name;
224 Reference<XStatement> xStmt = m_xConnection->createStatement();
225 if(xStmt.is())
226 xStmt->execute(sGrant);
227 ::comphelper::disposeComponent(xStmt);
231 void SAL_CALL OMySQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
233 if ( objType != PrivilegeObject::TABLE )
235 ::connectivity::SharedResources aResources;
236 const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
237 ::dbtools::throwGenericSQLException(sError,*this);
240 ::osl::MutexGuard aGuard(m_aMutex);
241 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
242 OUString sPrivs = getPrivilegeString(objPrivileges);
243 if(!sPrivs.isEmpty())
245 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
246 OUString sGrant = "REVOKE " + sPrivs +
247 " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::EComposeRule::InDataManipulation) +
248 " FROM " + m_Name;
250 Reference<XStatement> xStmt = m_xConnection->createStatement();
251 if(xStmt.is())
252 xStmt->execute(sGrant);
253 ::comphelper::disposeComponent(xStmt);
257 // XUser
258 void SAL_CALL OMySQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException, std::exception)
260 ::osl::MutexGuard aGuard(m_aMutex);
261 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
262 OUString sAlterPwd = "SET PASSWORD FOR " +
263 m_Name + "@\"%\" = PASSWORD('" +
264 newPassword + "')";
267 Reference<XStatement> xStmt = m_xConnection->createStatement();
268 if ( xStmt.is() )
270 xStmt->execute(sAlterPwd);
271 ::comphelper::disposeComponent(xStmt);
275 OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights)
277 OUString sPrivs;
278 if((nRights & Privilege::INSERT) == Privilege::INSERT)
279 sPrivs += "INSERT";
281 if((nRights & Privilege::DELETE) == Privilege::DELETE)
283 if(!sPrivs.isEmpty())
284 sPrivs += ",";
285 sPrivs += "DELETE";
288 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
290 if(!sPrivs.isEmpty())
291 sPrivs += ",";
292 sPrivs += "UPDATE";
295 if((nRights & Privilege::ALTER) == Privilege::ALTER)
297 if(!sPrivs.isEmpty())
298 sPrivs += ",";
299 sPrivs += "ALTER";
302 if((nRights & Privilege::SELECT) == Privilege::SELECT)
304 if(!sPrivs.isEmpty())
305 sPrivs += ",";
306 sPrivs += "SELECT";
309 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
311 if(!sPrivs.isEmpty())
312 sPrivs += ",";
313 sPrivs += "REFERENCES";
316 return sPrivs;
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */