1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
))
48 OMySQLUser::OMySQLUser(css::uno::Reference
<css::sdbc::XConnection
> _xConnection
,
50 : connectivity::sdbcx::OUser(Name
, true)
51 , m_xConnection(std::move(_xConnection
))
56 void OMySQLUser::refreshGroups() {}
58 OUserExtend::OUserExtend(const css::uno::Reference
<css::sdbc::XConnection
>& _xConnection
)
59 : OMySQLUser(_xConnection
)
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
);
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
;
105 case PrivilegeObject::TABLE
:
106 case PrivilegeObject::VIEW
:
109 if (!sCatalog
.isEmpty())
110 aCatalog
<<= sCatalog
;
111 xRes
= xMeta
->getTablePrivileges(aCatalog
, sSchema
, sTable
);
115 case PrivilegeObject::COLUMN
:
118 if (!sCatalog
.isEmpty())
119 aCatalog
<<= sCatalog
;
120 xRes
= xMeta
->getColumnPrivileges(aCatalog
, sSchema
, sTable
, u
"%"_ustr
);
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
))
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())
226 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
228 = "GRANT " + sPrivs
+ " ON "
229 + ::dbtools::quoteTableName(xMeta
, objName
, ::dbtools::EComposeRule::InDataManipulation
)
232 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
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())
254 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
256 = "REVOKE " + sPrivs
+ " ON "
257 + ::dbtools::quoteTableName(xMeta
, objName
, ::dbtools::EComposeRule::InDataManipulation
)
260 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
262 xStmt
->execute(sGrant
);
263 ::comphelper::disposeComponent(xStmt
);
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();
277 xStmt
->execute(sAlterPwd
);
278 ::comphelper::disposeComponent(xStmt
);
282 OUString
OMySQLUser::getPrivilegeString(sal_Int32 nRights
)
285 if ((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
288 if ((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
290 if (!sPrivs
.isEmpty())
295 if ((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
297 if (!sPrivs
.isEmpty())
302 if ((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
304 if (!sPrivs
.isEmpty())
309 if ((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
311 if (!sPrivs
.isEmpty())
316 if ((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
318 if (!sPrivs
.isEmpty())
320 sPrivs
+= "REFERENCES";
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */