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 "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 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
) : connectivity::sdbcx::OUser(true)
40 ,m_xConnection(_xConnection
)
45 OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
47 ) : connectivity::sdbcx::OUser(_Name
, true)
48 ,m_xConnection(_xConnection
)
53 void OMySQLUser::refreshGroups()
57 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
) : OMySQLUser(_xConnection
)
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
);
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
;
100 case PrivilegeObject::TABLE
:
101 case PrivilegeObject::VIEW
:
104 if ( !sCatalog
.isEmpty() )
105 aCatalog
<<= sCatalog
;
106 xRes
= xMeta
->getTablePrivileges(aCatalog
,sSchema
,sTable
);
110 case PrivilegeObject::COLUMN
:
113 if ( !sCatalog
.isEmpty() )
114 aCatalog
<<= sCatalog
;
115 xRes
= xMeta
->getColumnPrivileges(aCatalog
,sSchema
,sTable
,OUString("%"));
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( "REFERENCES" );
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
))
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
);
204 sal_Int32 SAL_CALL
OMySQLUser::getGrantablePrivileges( const OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
, std::exception
)
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
;
214 void SAL_CALL
OMySQLUser::grantPrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
, std::exception
)
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 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
229 OUString sGrant
= "GRANT " + sPrivs
+
230 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
) +
233 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
235 xStmt
->execute(sGrant
);
236 ::comphelper::disposeComponent(xStmt
);
240 void SAL_CALL
OMySQLUser::revokePrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
, std::exception
)
242 if ( objType
!= PrivilegeObject::TABLE
)
244 ::connectivity::SharedResources aResources
;
245 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_REVOKED
));
246 ::dbtools::throwGenericSQLException(sError
,*this);
249 ::osl::MutexGuard
aGuard(m_aMutex
);
250 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
251 OUString sPrivs
= getPrivilegeString(objPrivileges
);
252 if(!sPrivs
.isEmpty())
254 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
255 OUString sGrant
= "REVOKE " + sPrivs
+
256 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
) +
259 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
261 xStmt
->execute(sGrant
);
262 ::comphelper::disposeComponent(xStmt
);
267 void SAL_CALL
OMySQLUser::changePassword( const OUString
& /*oldPassword*/, const OUString
& newPassword
) throw(SQLException
, RuntimeException
, std::exception
)
269 ::osl::MutexGuard
aGuard(m_aMutex
);
270 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
271 OUString sAlterPwd
= "SET PASSWORD FOR " +
272 m_Name
+ "@\"%\" = PASSWORD('" +
276 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
279 xStmt
->execute(sAlterPwd
);
280 ::comphelper::disposeComponent(xStmt
);
284 OUString
OMySQLUser::getPrivilegeString(sal_Int32 nRights
) const
287 if((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
290 if((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
292 if(!sPrivs
.isEmpty())
297 if((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
299 if(!sPrivs
.isEmpty())
304 if((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
306 if(!sPrivs
.isEmpty())
311 if((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
313 if(!sPrivs
.isEmpty())
318 if((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
320 if(!sPrivs
.isEmpty())
322 sPrivs
+= "REFERENCES";
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */