xref: /trunk/main/connectivity/source/drivers/mysql/YUser.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "mysql/YUser.hxx"
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include "connectivity/dbtools.hxx"
34 #include "connectivity/dbexception.hxx"
35 #include <com/sun/star/sdbcx/Privilege.hpp>
36 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
37 #include "TConnection.hxx"
38 #include "resource/common_res.hrc"
39 
40 using namespace connectivity;
41 using namespace connectivity::mysql;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::lang;
48 // -------------------------------------------------------------------------
49 OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
50                 ,m_xConnection(_xConnection)
51 {
52     construct();
53 }
54 // -------------------------------------------------------------------------
55 OMySQLUser::OMySQLUser(   const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
56                 const ::rtl::OUString& _Name
57             ) : connectivity::sdbcx::OUser(_Name,sal_True)
58                 ,m_xConnection(_xConnection)
59 {
60     construct();
61 }
62 // -------------------------------------------------------------------------
63 void OMySQLUser::refreshGroups()
64 {
65 }
66 // -------------------------------------------------------------------------
67 OUserExtend::OUserExtend(   const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OMySQLUser(_xConnection)
68 {
69     construct();
70 }
71 // -------------------------------------------------------------------------
72 typedef connectivity::sdbcx::OUser  OUser_TYPEDEF;
73 void OUserExtend::construct()
74 {
75     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),    PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
76 }
77 // -----------------------------------------------------------------------------
78 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
79 {
80     Sequence< Property > aProps;
81     describeProperties(aProps);
82     return new cppu::OPropertyArrayHelper(aProps);
83 }
84 // -------------------------------------------------------------------------
85 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
86 {
87     return *OUserExtend_PROP::getArrayHelper();
88 }
89 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
90 // -----------------------------------------------------------------------------
91 sal_Int32 SAL_CALL OMySQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
92 {
93     ::osl::MutexGuard aGuard(m_aMutex);
94     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
95 
96     sal_Int32 nRights,nRightsWithGrant;
97     findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
98     return nRights;
99 }
100 // -----------------------------------------------------------------------------
101 void OMySQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
102 {
103     nRightsWithGrant = nRights = 0;
104     // first we need to create the sql stmt to select the privs
105     Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
106     ::rtl::OUString sCatalog,sSchema,sTable;
107     ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
108     Reference<XResultSet> xRes;
109     switch(objType)
110     {
111         case PrivilegeObject::TABLE:
112         case PrivilegeObject::VIEW:
113             {
114                 Any aCatalog;
115                 if ( sCatalog.getLength() )
116                     aCatalog <<= sCatalog;
117                 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
118             }
119             break;
120 
121         case PrivilegeObject::COLUMN:
122             {
123                 Any aCatalog;
124                 if ( sCatalog.getLength() )
125                     aCatalog <<= sCatalog;
126                 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%")));
127             }
128             break;
129     }
130 
131     if ( xRes.is() )
132     {
133         static const ::rtl::OUString sSELECT    = ::rtl::OUString::createFromAscii("SELECT");
134         static const ::rtl::OUString sINSERT    = ::rtl::OUString::createFromAscii("INSERT");
135         static const ::rtl::OUString sUPDATE    = ::rtl::OUString::createFromAscii("UPDATE");
136         static const ::rtl::OUString sDELETE    = ::rtl::OUString::createFromAscii("DELETE");
137         static const ::rtl::OUString sREAD      = ::rtl::OUString::createFromAscii("READ");
138         static const ::rtl::OUString sCREATE    = ::rtl::OUString::createFromAscii("CREATE");
139         static const ::rtl::OUString sALTER     = ::rtl::OUString::createFromAscii("ALTER");
140         static const ::rtl::OUString sREFERENCE = ::rtl::OUString::createFromAscii("REFERENCE");
141         static const ::rtl::OUString sDROP      = ::rtl::OUString::createFromAscii("DROP");
142         static const ::rtl::OUString sYes       = ::rtl::OUString::createFromAscii("YES");
143 
144         nRightsWithGrant = nRights = 0;
145 
146         Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
147         while( xCurrentRow.is() && xRes->next() )
148         {
149             ::rtl::OUString sGrantee    = xCurrentRow->getString(5);
150             ::rtl::OUString sPrivilege  = xCurrentRow->getString(6);
151             ::rtl::OUString sGrantable  = xCurrentRow->getString(7);
152 
153             if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
154                 continue;
155 
156             if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
157             {
158                 nRights |= Privilege::SELECT;
159                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
160                     nRightsWithGrant |= Privilege::SELECT;
161             }
162             else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
163             {
164                 nRights |= Privilege::INSERT;
165                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
166                     nRightsWithGrant |= Privilege::INSERT;
167             }
168             else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
169             {
170                 nRights |= Privilege::UPDATE;
171                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
172                     nRightsWithGrant |= Privilege::UPDATE;
173             }
174             else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
175             {
176                 nRights |= Privilege::DELETE;
177                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
178                     nRightsWithGrant |= Privilege::DELETE;
179             }
180             else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
181             {
182                 nRights |= Privilege::READ;
183                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
184                     nRightsWithGrant |= Privilege::READ;
185             }
186             else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
187             {
188                 nRights |= Privilege::CREATE;
189                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
190                     nRightsWithGrant |= Privilege::CREATE;
191             }
192             else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
193             {
194                 nRights |= Privilege::ALTER;
195                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
196                     nRightsWithGrant |= Privilege::ALTER;
197             }
198             else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
199             {
200                 nRights |= Privilege::REFERENCE;
201                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
202                     nRightsWithGrant |= Privilege::REFERENCE;
203             }
204             else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
205             {
206                 nRights |= Privilege::DROP;
207                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
208                     nRightsWithGrant |= Privilege::DROP;
209             }
210         }
211         ::comphelper::disposeComponent(xRes);
212     }
213 }
214 // -------------------------------------------------------------------------
215 sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
216 {
217     ::osl::MutexGuard aGuard(m_aMutex);
218     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
219 
220     sal_Int32 nRights,nRightsWithGrant;
221     findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
222     return nRightsWithGrant;
223 }
224 // -------------------------------------------------------------------------
225 void SAL_CALL OMySQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
226 {
227     if ( objType != PrivilegeObject::TABLE )
228     {
229         ::connectivity::SharedResources aResources;
230         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
231         ::dbtools::throwGenericSQLException(sError,*this);
232     } // if ( objType != PrivilegeObject::TABLE )
233 
234     ::osl::MutexGuard aGuard(m_aMutex);
235 
236     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
237     if(sPrivs.getLength())
238     {
239         ::rtl::OUString sGrant;
240         sGrant += ::rtl::OUString::createFromAscii("GRANT ");
241         sGrant += sPrivs;
242         sGrant += ::rtl::OUString::createFromAscii(" ON ");
243         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
244         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
245         sGrant += ::rtl::OUString::createFromAscii(" TO ");
246         sGrant += m_Name;
247 
248         Reference<XStatement> xStmt = m_xConnection->createStatement();
249         if(xStmt.is())
250             xStmt->execute(sGrant);
251         ::comphelper::disposeComponent(xStmt);
252     }
253 }
254 // -------------------------------------------------------------------------
255 void SAL_CALL OMySQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
256 {
257     if ( objType != PrivilegeObject::TABLE )
258     {
259         ::connectivity::SharedResources aResources;
260         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
261         ::dbtools::throwGenericSQLException(sError,*this);
262     }
263 
264     ::osl::MutexGuard aGuard(m_aMutex);
265     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
266     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
267     if(sPrivs.getLength())
268     {
269         ::rtl::OUString sGrant;
270         sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
271         sGrant += sPrivs;
272         sGrant += ::rtl::OUString::createFromAscii(" ON ");
273         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
274         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
275         sGrant += ::rtl::OUString::createFromAscii(" FROM ");
276         sGrant += m_Name;
277 
278         Reference<XStatement> xStmt = m_xConnection->createStatement();
279         if(xStmt.is())
280             xStmt->execute(sGrant);
281         ::comphelper::disposeComponent(xStmt);
282     }
283 }
284 // -----------------------------------------------------------------------------
285 // XUser
286 void SAL_CALL OMySQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
287 {
288     ::osl::MutexGuard aGuard(m_aMutex);
289     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
290     ::rtl::OUString sAlterPwd;
291     sAlterPwd = ::rtl::OUString::createFromAscii("SET PASSWORD FOR ");
292     sAlterPwd += m_Name;
293     sAlterPwd += ::rtl::OUString::createFromAscii("@\"%\" = PASSWORD('") ;
294     sAlterPwd += newPassword;
295     sAlterPwd += ::rtl::OUString::createFromAscii("')") ;
296 
297 
298     Reference<XStatement> xStmt = m_xConnection->createStatement();
299     if ( xStmt.is() )
300     {
301         xStmt->execute(sAlterPwd);
302         ::comphelper::disposeComponent(xStmt);
303     }
304 }
305 // -----------------------------------------------------------------------------
306 ::rtl::OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights) const
307 {
308     ::rtl::OUString sPrivs;
309     if((nRights & Privilege::INSERT) == Privilege::INSERT)
310         sPrivs += ::rtl::OUString::createFromAscii("INSERT");
311 
312     if((nRights & Privilege::DELETE) == Privilege::DELETE)
313     {
314         if(sPrivs.getLength())
315             sPrivs += ::rtl::OUString::createFromAscii(",");
316         sPrivs += ::rtl::OUString::createFromAscii("DELETE");
317     }
318 
319     if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
320     {
321         if(sPrivs.getLength())
322             sPrivs += ::rtl::OUString::createFromAscii(",");
323         sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
324     }
325 
326     if((nRights & Privilege::ALTER) == Privilege::ALTER)
327     {
328         if(sPrivs.getLength())
329             sPrivs += ::rtl::OUString::createFromAscii(",");
330         sPrivs += ::rtl::OUString::createFromAscii("ALTER");
331     }
332 
333     if((nRights & Privilege::SELECT) == Privilege::SELECT)
334     {
335         if(sPrivs.getLength())
336             sPrivs += ::rtl::OUString::createFromAscii(",");
337         sPrivs += ::rtl::OUString::createFromAscii("SELECT");
338     }
339 
340     if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
341     {
342         if(sPrivs.getLength())
343             sPrivs += ::rtl::OUString::createFromAscii(",");
344         sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
345     }
346 
347     return sPrivs;
348 }
349 // -----------------------------------------------------------------------------
350 
351