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/YUsers.hxx"
31 #include "mysql/YUser.hxx"
32 #include "mysql/YTable.hxx"
33 #include <com/sun/star/sdbc/XRow.hpp>
34 #include <com/sun/star/sdbc/XResultSet.hpp>
35 #include "connectivity/sdbcx/IRefreshable.hxx"
36 #include <comphelper/types.hxx>
37 #include "connectivity/dbexception.hxx"
38 #include "connectivity/dbtools.hxx"
39 #include "TConnection.hxx"
40 
41 using namespace ::comphelper;
42 using namespace connectivity;
43 using namespace connectivity::mysql;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
46 //	using namespace ::com::sun::star::sdbcx;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::lang;
50 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
51 
52 OUsers::OUsers( ::cppu::OWeakObject& _rParent,
53 				::osl::Mutex& _rMutex,
54 				const TStringVector &_rVector,
55 				const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
56 				connectivity::sdbcx::IRefreshableUsers* _pParent)
57 	: sdbcx::OCollection(_rParent,sal_True,_rMutex,_rVector)
58 	,m_xConnection(_xConnection)
59 	,m_pParent(_pParent)
60 {
61 }
62 // -----------------------------------------------------------------------------
63 
64 sdbcx::ObjectType OUsers::createObject(const ::rtl::OUString& _rName)
65 {
66 	return new OMySQLUser(m_xConnection,_rName);
67 }
68 // -------------------------------------------------------------------------
69 void OUsers::impl_refresh() throw(RuntimeException)
70 {
71 	m_pParent->refreshUsers();
72 }
73 // -------------------------------------------------------------------------
74 Reference< XPropertySet > OUsers::createDescriptor()
75 {
76 	OUserExtend* pNew = new OUserExtend(m_xConnection);
77 	return pNew;
78 }
79 // -------------------------------------------------------------------------
80 // XAppend
81 sdbcx::ObjectType OUsers::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
82 {
83 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("GRANT USAGE ON * TO ");
84 	::rtl::OUString aQuote	= m_xConnection->getMetaData()->getIdentifierQuoteString(  );
85 	::rtl::OUString sUserName( _rForName );
86 	aSql += ::dbtools::quoteName(aQuote,sUserName)
87 				+ ::rtl::OUString::createFromAscii(" @\"%\" ");
88 	::rtl::OUString sPassword;
89 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD)) >>= sPassword;
90 	if ( sPassword.getLength() )
91 	{
92 		aSql += ::rtl::OUString::createFromAscii(" IDENTIFIED BY '");
93 		aSql += sPassword;
94 		aSql += ::rtl::OUString::createFromAscii("'");
95 	}
96 
97 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
98 	if(xStmt.is())
99 		xStmt->execute(aSql);
100 	::comphelper::disposeComponent(xStmt);
101 
102     return createObject( _rForName );
103 }
104 // -------------------------------------------------------------------------
105 // XDrop
106 void OUsers::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
107 {
108 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("REVOKE ALL ON * FROM ");
109 	::rtl::OUString aQuote	= m_xConnection->getMetaData()->getIdentifierQuoteString(  );
110 	aSql += ::dbtools::quoteName(aQuote,_sElementName);
111 
112 	Reference< XStatement > xStmt = m_xConnection->createStatement(  );
113 	if(xStmt.is())
114 		xStmt->execute(aSql);
115 	::comphelper::disposeComponent(xStmt);
116 }
117 
118 // -------------------------------------------------------------------------
119