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