1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "mysql/YUsers.hxx"
27 #include "mysql/YUser.hxx"
28 #include "mysql/YTable.hxx"
29 #include <com/sun/star/sdbc/XRow.hpp>
30 #include <com/sun/star/sdbc/XResultSet.hpp>
31 #include "connectivity/sdbcx/IRefreshable.hxx"
32 #include <comphelper/types.hxx>
33 #include "connectivity/dbexception.hxx"
34 #include "connectivity/dbtools.hxx"
35 #include "TConnection.hxx"
36 
37 using namespace ::comphelper;
38 using namespace connectivity;
39 using namespace connectivity::mysql;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 //	using namespace ::com::sun::star::sdbcx;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
46 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
47 
OUsers(::cppu::OWeakObject & _rParent,::osl::Mutex & _rMutex,const TStringVector & _rVector,const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XConnection> & _xConnection,connectivity::sdbcx::IRefreshableUsers * _pParent)48 OUsers::OUsers( ::cppu::OWeakObject& _rParent,
49 				::osl::Mutex& _rMutex,
50 				const TStringVector &_rVector,
51 				const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
52 				connectivity::sdbcx::IRefreshableUsers* _pParent)
53 	: sdbcx::OCollection(_rParent,sal_True,_rMutex,_rVector)
54 	,m_xConnection(_xConnection)
55 	,m_pParent(_pParent)
56 {
57 }
58 // -----------------------------------------------------------------------------
59 
createObject(const::rtl::OUString & _rName)60 sdbcx::ObjectType OUsers::createObject(const ::rtl::OUString& _rName)
61 {
62 	return new OMySQLUser(m_xConnection,_rName);
63 }
64 // -------------------------------------------------------------------------
impl_refresh()65 void OUsers::impl_refresh() throw(RuntimeException)
66 {
67 	m_pParent->refreshUsers();
68 }
69 // -------------------------------------------------------------------------
createDescriptor()70 Reference< XPropertySet > OUsers::createDescriptor()
71 {
72 	OUserExtend* pNew = new OUserExtend(m_xConnection);
73 	return pNew;
74 }
75 // -------------------------------------------------------------------------
76 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)77 sdbcx::ObjectType OUsers::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
78 {
79 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("GRANT USAGE ON * TO ");
80 	::rtl::OUString aQuote	= m_xConnection->getMetaData()->getIdentifierQuoteString(  );
81 	::rtl::OUString sUserName( _rForName );
82 	aSql += ::dbtools::quoteName(aQuote,sUserName)
83 				+ ::rtl::OUString::createFromAscii(" @\"%\" ");
84 	::rtl::OUString sPassword;
85 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD)) >>= sPassword;
86 	if ( sPassword.getLength() )
87 	{
88 		aSql += ::rtl::OUString::createFromAscii(" IDENTIFIED BY '");
89 		aSql += sPassword;
90 		aSql += ::rtl::OUString::createFromAscii("'");
91 	}
92 
93 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
94 	if(xStmt.is())
95 		xStmt->execute(aSql);
96 	::comphelper::disposeComponent(xStmt);
97 
98     return createObject( _rForName );
99 }
100 // -------------------------------------------------------------------------
101 // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)102 void OUsers::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
103 {
104 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("REVOKE ALL ON * FROM ");
105 	::rtl::OUString aQuote	= m_xConnection->getMetaData()->getIdentifierQuoteString(  );
106 	aSql += ::dbtools::quoteName(aQuote,_sElementName);
107 
108 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
109 	if(xStmt.is())
110 		xStmt->execute(aSql);
111 	::comphelper::disposeComponent(xStmt);
112 }
113 
114 // -------------------------------------------------------------------------
115