Use correct object
[LibreOffice.git] / connectivity / source / drivers / mysql_jdbc / YUser.cxx
blob6944aa7e4c3fc5062432c871fb2ff52dd4cca19b
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 <strings.hrc>
29 #include <comphelper/types.hxx>
30 #include <utility>
32 using namespace connectivity;
33 using namespace connectivity::mysql;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::sdbcx;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::container;
39 using namespace ::com::sun::star::lang;
41 OMySQLUser::OMySQLUser(css::uno::Reference<css::sdbc::XConnection> _xConnection)
42 : connectivity::sdbcx::OUser(true)
43 , m_xConnection(std::move(_xConnection))
45 construct();
48 OMySQLUser::OMySQLUser(css::uno::Reference<css::sdbc::XConnection> _xConnection,
49 const OUString& Name)
50 : connectivity::sdbcx::OUser(Name, true)
51 , m_xConnection(std::move(_xConnection))
53 construct();
56 void OMySQLUser::refreshGroups() {}
58 OUserExtend::OUserExtend(const css::uno::Reference<css::sdbc::XConnection>& _xConnection)
59 : OMySQLUser(_xConnection)
61 construct();
64 void OUserExtend::construct()
66 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),
67 PROPERTY_ID_PASSWORD, 0, &m_Password, ::cppu::UnoType<OUString>::get());
70 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
72 Sequence<Property> aProps;
73 describeProperties(aProps);
74 return new cppu::OPropertyArrayHelper(aProps);
77 cppu::IPropertyArrayHelper& OUserExtend::getInfoHelper()
79 return *OUserExtend_PROP::getArrayHelper();
81 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
83 sal_Int32 SAL_CALL OMySQLUser::getPrivileges(const OUString& objName, sal_Int32 objType)
85 ::osl::MutexGuard aGuard(m_aMutex);
86 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
88 sal_Int32 nRights, nRightsWithGrant;
89 findPrivilegesAndGrantPrivileges(objName, objType, nRights, nRightsWithGrant);
90 return nRights;
93 void OMySQLUser::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,
94 sal_Int32& nRights, sal_Int32& nRightsWithGrant)
96 nRightsWithGrant = nRights = 0;
97 // first we need to create the sql stmt to select the privs
98 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
99 OUString sCatalog, sSchema, sTable;
100 ::dbtools::qualifiedNameComponents(xMeta, objName, sCatalog, sSchema, sTable,
101 ::dbtools::EComposeRule::InDataManipulation);
102 Reference<XResultSet> xRes;
103 switch (objType)
105 case PrivilegeObject::TABLE:
106 case PrivilegeObject::VIEW:
108 Any aCatalog;
109 if (!sCatalog.isEmpty())
110 aCatalog <<= sCatalog;
111 xRes = xMeta->getTablePrivileges(aCatalog, sSchema, sTable);
113 break;
115 case PrivilegeObject::COLUMN:
117 Any aCatalog;
118 if (!sCatalog.isEmpty())
119 aCatalog <<= sCatalog;
120 xRes = xMeta->getColumnPrivileges(aCatalog, sSchema, sTable, u"%"_ustr);
122 break;
125 if (!xRes.is())
126 return;
128 static const char sYes[] = "YES";
130 nRightsWithGrant = nRights = 0;
132 Reference<XRow> xCurrentRow(xRes, UNO_QUERY);
133 while (xCurrentRow.is() && xRes->next())
135 OUString sGrantee = xCurrentRow->getString(5);
136 OUString sPrivilege = xCurrentRow->getString(6);
137 OUString sGrantable = xCurrentRow->getString(7);
139 if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
140 continue;
142 if (sPrivilege.equalsIgnoreAsciiCase("SELECT"))
144 nRights |= Privilege::SELECT;
145 if (sGrantable.equalsIgnoreAsciiCase(sYes))
146 nRightsWithGrant |= Privilege::SELECT;
148 else if (sPrivilege.equalsIgnoreAsciiCase("INSERT"))
150 nRights |= Privilege::INSERT;
151 if (sGrantable.equalsIgnoreAsciiCase(sYes))
152 nRightsWithGrant |= Privilege::INSERT;
154 else if (sPrivilege.equalsIgnoreAsciiCase("UPDATE"))
156 nRights |= Privilege::UPDATE;
157 if (sGrantable.equalsIgnoreAsciiCase(sYes))
158 nRightsWithGrant |= Privilege::UPDATE;
160 else if (sPrivilege.equalsIgnoreAsciiCase("DELETE"))
162 nRights |= Privilege::DELETE;
163 if (sGrantable.equalsIgnoreAsciiCase(sYes))
164 nRightsWithGrant |= Privilege::DELETE;
166 else if (sPrivilege.equalsIgnoreAsciiCase("READ"))
168 nRights |= Privilege::READ;
169 if (sGrantable.equalsIgnoreAsciiCase(sYes))
170 nRightsWithGrant |= Privilege::READ;
172 else if (sPrivilege.equalsIgnoreAsciiCase("CREATE"))
174 nRights |= Privilege::CREATE;
175 if (sGrantable.equalsIgnoreAsciiCase(sYes))
176 nRightsWithGrant |= Privilege::CREATE;
178 else if (sPrivilege.equalsIgnoreAsciiCase("ALTER"))
180 nRights |= Privilege::ALTER;
181 if (sGrantable.equalsIgnoreAsciiCase(sYes))
182 nRightsWithGrant |= Privilege::ALTER;
184 else if (sPrivilege.equalsIgnoreAsciiCase("REFERENCES"))
186 nRights |= Privilege::REFERENCE;
187 if (sGrantable.equalsIgnoreAsciiCase(sYes))
188 nRightsWithGrant |= Privilege::REFERENCE;
190 else if (sPrivilege.equalsIgnoreAsciiCase("DROP"))
192 nRights |= Privilege::DROP;
193 if (sGrantable.equalsIgnoreAsciiCase(sYes))
194 nRightsWithGrant |= Privilege::DROP;
197 ::comphelper::disposeComponent(xRes);
200 sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges(const OUString& objName, sal_Int32 objType)
202 ::osl::MutexGuard aGuard(m_aMutex);
203 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
205 sal_Int32 nRights, nRightsWithGrant;
206 findPrivilegesAndGrantPrivileges(objName, objType, nRights, nRightsWithGrant);
207 return nRightsWithGrant;
210 void SAL_CALL OMySQLUser::grantPrivileges(const OUString& objName, sal_Int32 objType,
211 sal_Int32 objPrivileges)
213 if (objType != PrivilegeObject::TABLE)
215 ::connectivity::SharedResources aResources;
216 const OUString sError(aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
217 ::dbtools::throwGenericSQLException(sError, *this);
218 } // if ( objType != PrivilegeObject::TABLE )
220 ::osl::MutexGuard aGuard(m_aMutex);
222 OUString sPrivs = getPrivilegeString(objPrivileges);
223 if (sPrivs.isEmpty())
224 return;
226 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
227 OUString sGrant
228 = "GRANT " + sPrivs + " ON "
229 + ::dbtools::quoteTableName(xMeta, objName, ::dbtools::EComposeRule::InDataManipulation)
230 + " TO " + m_Name;
232 Reference<XStatement> xStmt = m_xConnection->createStatement();
233 if (xStmt.is())
234 xStmt->execute(sGrant);
235 ::comphelper::disposeComponent(xStmt);
238 void SAL_CALL OMySQLUser::revokePrivileges(const OUString& objName, sal_Int32 objType,
239 sal_Int32 objPrivileges)
241 if (objType != PrivilegeObject::TABLE)
243 ::connectivity::SharedResources aResources;
244 const OUString sError(aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
245 ::dbtools::throwGenericSQLException(sError, *this);
248 ::osl::MutexGuard aGuard(m_aMutex);
249 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
250 OUString sPrivs = getPrivilegeString(objPrivileges);
251 if (sPrivs.isEmpty())
252 return;
254 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
255 OUString sGrant
256 = "REVOKE " + sPrivs + " ON "
257 + ::dbtools::quoteTableName(xMeta, objName, ::dbtools::EComposeRule::InDataManipulation)
258 + " FROM " + m_Name;
260 Reference<XStatement> xStmt = m_xConnection->createStatement();
261 if (xStmt.is())
262 xStmt->execute(sGrant);
263 ::comphelper::disposeComponent(xStmt);
266 // XUser
267 void SAL_CALL OMySQLUser::changePassword(const OUString& /*oldPassword*/,
268 const OUString& newPassword)
270 ::osl::MutexGuard aGuard(m_aMutex);
271 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
272 OUString sAlterPwd = "SET PASSWORD FOR " + m_Name + "@\"%\" = PASSWORD('" + newPassword + "')";
274 Reference<XStatement> xStmt = m_xConnection->createStatement();
275 if (xStmt.is())
277 xStmt->execute(sAlterPwd);
278 ::comphelper::disposeComponent(xStmt);
282 OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights)
284 OUString sPrivs;
285 if ((nRights & Privilege::INSERT) == Privilege::INSERT)
286 sPrivs += "INSERT";
288 if ((nRights & Privilege::DELETE) == Privilege::DELETE)
290 if (!sPrivs.isEmpty())
291 sPrivs += ",";
292 sPrivs += "DELETE";
295 if ((nRights & Privilege::UPDATE) == Privilege::UPDATE)
297 if (!sPrivs.isEmpty())
298 sPrivs += ",";
299 sPrivs += "UPDATE";
302 if ((nRights & Privilege::ALTER) == Privilege::ALTER)
304 if (!sPrivs.isEmpty())
305 sPrivs += ",";
306 sPrivs += "ALTER";
309 if ((nRights & Privilege::SELECT) == Privilege::SELECT)
311 if (!sPrivs.isEmpty())
312 sPrivs += ",";
313 sPrivs += "SELECT";
316 if ((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
318 if (!sPrivs.isEmpty())
319 sPrivs += ",";
320 sPrivs += "REFERENCES";
323 return sPrivs;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */