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>
31 using namespace connectivity
;
32 using namespace connectivity::mysql
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::sdbcx
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::lang
;
40 OMySQLUser::OMySQLUser(const css::uno::Reference
<css::sdbc::XConnection
>& _xConnection
)
41 : connectivity::sdbcx::OUser(true)
42 , m_xConnection(_xConnection
)
47 OMySQLUser::OMySQLUser(const css::uno::Reference
<css::sdbc::XConnection
>& _xConnection
,
49 : connectivity::sdbcx::OUser(Name
, true)
50 , m_xConnection(_xConnection
)
55 void OMySQLUser::refreshGroups() {}
57 OUserExtend::OUserExtend(const css::uno::Reference
<css::sdbc::XConnection
>& _xConnection
)
58 : OMySQLUser(_xConnection
)
63 void OUserExtend::construct()
65 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD
),
66 PROPERTY_ID_PASSWORD
, 0, &m_Password
, ::cppu::UnoType
<OUString
>::get());
69 cppu::IPropertyArrayHelper
* OUserExtend::createArrayHelper() const
71 Sequence
<Property
> aProps
;
72 describeProperties(aProps
);
73 return new cppu::OPropertyArrayHelper(aProps
);
76 cppu::IPropertyArrayHelper
& OUserExtend::getInfoHelper()
78 return *OUserExtend_PROP::getArrayHelper();
80 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
;
82 sal_Int32 SAL_CALL
OMySQLUser::getPrivileges(const OUString
& objName
, sal_Int32 objType
)
84 ::osl::MutexGuard
aGuard(m_aMutex
);
85 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
87 sal_Int32 nRights
, nRightsWithGrant
;
88 findPrivilegesAndGrantPrivileges(objName
, objType
, nRights
, nRightsWithGrant
);
92 void OMySQLUser::findPrivilegesAndGrantPrivileges(const OUString
& objName
, sal_Int32 objType
,
93 sal_Int32
& nRights
, sal_Int32
& nRightsWithGrant
)
95 nRightsWithGrant
= nRights
= 0;
96 // first we need to create the sql stmt to select the privs
97 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
98 OUString sCatalog
, sSchema
, sTable
;
99 ::dbtools::qualifiedNameComponents(xMeta
, objName
, sCatalog
, sSchema
, sTable
,
100 ::dbtools::EComposeRule::InDataManipulation
);
101 Reference
<XResultSet
> xRes
;
104 case PrivilegeObject::TABLE
:
105 case PrivilegeObject::VIEW
:
108 if (!sCatalog
.isEmpty())
109 aCatalog
<<= sCatalog
;
110 xRes
= xMeta
->getTablePrivileges(aCatalog
, sSchema
, sTable
);
114 case PrivilegeObject::COLUMN
:
117 if (!sCatalog
.isEmpty())
118 aCatalog
<<= sCatalog
;
119 xRes
= xMeta
->getColumnPrivileges(aCatalog
, sSchema
, sTable
, "%");
127 static const char sYes
[] = "YES";
129 nRightsWithGrant
= nRights
= 0;
131 Reference
<XRow
> xCurrentRow(xRes
, UNO_QUERY
);
132 while (xCurrentRow
.is() && xRes
->next())
134 OUString sGrantee
= xCurrentRow
->getString(5);
135 OUString sPrivilege
= xCurrentRow
->getString(6);
136 OUString sGrantable
= xCurrentRow
->getString(7);
138 if (!m_Name
.equalsIgnoreAsciiCase(sGrantee
))
141 if (sPrivilege
.equalsIgnoreAsciiCase("SELECT"))
143 nRights
|= Privilege::SELECT
;
144 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
145 nRightsWithGrant
|= Privilege::SELECT
;
147 else if (sPrivilege
.equalsIgnoreAsciiCase("INSERT"))
149 nRights
|= Privilege::INSERT
;
150 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
151 nRightsWithGrant
|= Privilege::INSERT
;
153 else if (sPrivilege
.equalsIgnoreAsciiCase("UPDATE"))
155 nRights
|= Privilege::UPDATE
;
156 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
157 nRightsWithGrant
|= Privilege::UPDATE
;
159 else if (sPrivilege
.equalsIgnoreAsciiCase("DELETE"))
161 nRights
|= Privilege::DELETE
;
162 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
163 nRightsWithGrant
|= Privilege::DELETE
;
165 else if (sPrivilege
.equalsIgnoreAsciiCase("READ"))
167 nRights
|= Privilege::READ
;
168 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
169 nRightsWithGrant
|= Privilege::READ
;
171 else if (sPrivilege
.equalsIgnoreAsciiCase("CREATE"))
173 nRights
|= Privilege::CREATE
;
174 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
175 nRightsWithGrant
|= Privilege::CREATE
;
177 else if (sPrivilege
.equalsIgnoreAsciiCase("ALTER"))
179 nRights
|= Privilege::ALTER
;
180 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
181 nRightsWithGrant
|= Privilege::ALTER
;
183 else if (sPrivilege
.equalsIgnoreAsciiCase("REFERENCES"))
185 nRights
|= Privilege::REFERENCE
;
186 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
187 nRightsWithGrant
|= Privilege::REFERENCE
;
189 else if (sPrivilege
.equalsIgnoreAsciiCase("DROP"))
191 nRights
|= Privilege::DROP
;
192 if (sGrantable
.equalsIgnoreAsciiCase(sYes
))
193 nRightsWithGrant
|= Privilege::DROP
;
196 ::comphelper::disposeComponent(xRes
);
199 sal_Int32 SAL_CALL
OMySQLUser::getGrantablePrivileges(const OUString
& objName
, sal_Int32 objType
)
201 ::osl::MutexGuard
aGuard(m_aMutex
);
202 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
204 sal_Int32 nRights
, nRightsWithGrant
;
205 findPrivilegesAndGrantPrivileges(objName
, objType
, nRights
, nRightsWithGrant
);
206 return nRightsWithGrant
;
209 void SAL_CALL
OMySQLUser::grantPrivileges(const OUString
& objName
, sal_Int32 objType
,
210 sal_Int32 objPrivileges
)
212 if (objType
!= PrivilegeObject::TABLE
)
214 ::connectivity::SharedResources aResources
;
215 const OUString
sError(aResources
.getResourceString(STR_PRIVILEGE_NOT_GRANTED
));
216 ::dbtools::throwGenericSQLException(sError
, *this);
217 } // if ( objType != PrivilegeObject::TABLE )
219 ::osl::MutexGuard
aGuard(m_aMutex
);
221 OUString sPrivs
= getPrivilegeString(objPrivileges
);
222 if (sPrivs
.isEmpty())
225 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
227 = "GRANT " + sPrivs
+ " ON "
228 + ::dbtools::quoteTableName(xMeta
, objName
, ::dbtools::EComposeRule::InDataManipulation
)
231 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
233 xStmt
->execute(sGrant
);
234 ::comphelper::disposeComponent(xStmt
);
237 void SAL_CALL
OMySQLUser::revokePrivileges(const OUString
& objName
, sal_Int32 objType
,
238 sal_Int32 objPrivileges
)
240 if (objType
!= PrivilegeObject::TABLE
)
242 ::connectivity::SharedResources aResources
;
243 const OUString
sError(aResources
.getResourceString(STR_PRIVILEGE_NOT_REVOKED
));
244 ::dbtools::throwGenericSQLException(sError
, *this);
247 ::osl::MutexGuard
aGuard(m_aMutex
);
248 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
249 OUString sPrivs
= getPrivilegeString(objPrivileges
);
250 if (sPrivs
.isEmpty())
253 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
255 = "REVOKE " + sPrivs
+ " ON "
256 + ::dbtools::quoteTableName(xMeta
, objName
, ::dbtools::EComposeRule::InDataManipulation
)
259 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
261 xStmt
->execute(sGrant
);
262 ::comphelper::disposeComponent(xStmt
);
266 void SAL_CALL
OMySQLUser::changePassword(const OUString
& /*oldPassword*/,
267 const OUString
& newPassword
)
269 ::osl::MutexGuard
aGuard(m_aMutex
);
270 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
271 OUString sAlterPwd
= "SET PASSWORD FOR " + m_Name
+ "@\"%\" = PASSWORD('" + newPassword
+ "')";
273 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
276 xStmt
->execute(sAlterPwd
);
277 ::comphelper::disposeComponent(xStmt
);
281 OUString
OMySQLUser::getPrivilegeString(sal_Int32 nRights
)
284 if ((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
287 if ((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
289 if (!sPrivs
.isEmpty())
294 if ((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
296 if (!sPrivs
.isEmpty())
301 if ((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
303 if (!sPrivs
.isEmpty())
308 if ((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
310 if (!sPrivs
.isEmpty())
315 if ((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
317 if (!sPrivs
.isEmpty())
319 sPrivs
+= "REFERENCES";
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */