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 "adabas/BUsers.hxx"
27 #include "adabas/BUser.hxx"
28 #include "adabas/BTable.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 "resource/adabas_res.hrc"
36 
37 using namespace ::comphelper;
38 using namespace connectivity;
39 using namespace connectivity::adabas;
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 
createObject(const::rtl::OUString & _rName)48 sdbcx::ObjectType OUsers::createObject(const ::rtl::OUString& _rName)
49 {
50 	return new OAdabasUser(m_pConnection,_rName);
51 }
52 // -------------------------------------------------------------------------
impl_refresh()53 void OUsers::impl_refresh() throw(RuntimeException)
54 {
55 	m_pParent->refreshUsers();
56 }
57 // -------------------------------------------------------------------------
createDescriptor()58 Reference< XPropertySet > OUsers::createDescriptor()
59 {
60 	OUserExtend* pNew = new OUserExtend(m_pConnection);
61 	return pNew;
62 }
63 // -------------------------------------------------------------------------
64 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)65 sdbcx::ObjectType OUsers::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
66 {
67 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("CREATE USER ");
68 	::rtl::OUString aQuote	= m_pConnection->getMetaData()->getIdentifierQuoteString(  );
69 
70     ::rtl::OUString sUserName( _rForName );
71 	sUserName = sUserName.toAsciiUpperCase();
72 	descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sUserName));
73 	aSql += ::dbtools::quoteName(aQuote,sUserName)
74 				+ ::rtl::OUString::createFromAscii(" PASSWORD ")
75 				+ getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD)));
76 	aSql += ::rtl::OUString::createFromAscii(" RESOURCE NOT EXCLUSIVE");
77 
78 	Reference< XStatement > xStmt = m_pConnection->createStatement(  );
79 	if(xStmt.is())
80 		xStmt->execute(aSql);
81 	::comphelper::disposeComponent(xStmt);
82 
83     return createObject( _rForName );
84 }
85 // -------------------------------------------------------------------------
86 // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)87 void OUsers::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
88 {
89 	{
90 		// first we have to check if this user is live relevaant for the database
91 		// which means with out these users the database will miss more than one important system table
92 		::rtl::OUString sUsers = ::rtl::OUString::createFromAscii("SELECT USERMODE,USERNAME FROM DOMAIN.USERS WHERE USERNAME = '");
93 		sUsers += _sElementName + ::rtl::OUString::createFromAscii("'");
94 		Reference< XStatement > xStmt = m_pConnection->createStatement();
95 		if(xStmt.is())
96 		{
97 			Reference<XResultSet> xRes = xStmt->executeQuery(sUsers);
98 			Reference<XRow> xRow(xRes,UNO_QUERY);
99 			if(xRes.is() && xRow.is() && xRes->next()) // there can only be one user with this name
100 			{
101 				static const ::rtl::OUString sDbaUser = ::rtl::OUString::createFromAscii("DBA");
102 				if(xRow->getString(1) == sDbaUser)
103 				{
104 					::comphelper::disposeComponent(xStmt);
105                     m_pConnection->throwGenericSQLException(STR_USER_NO_DELETE,static_cast< XDrop* >( this ));
106 				}
107 			}
108 			::comphelper::disposeComponent(xStmt);
109 		}
110 	}
111 
112 	{
113 		::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("DROP USER ");
114 		::rtl::OUString aQuote	= m_pConnection->getMetaData()->getIdentifierQuoteString(  );
115 		aSql += ::dbtools::quoteName(aQuote,_sElementName);
116 
117 		Reference< XStatement > xStmt = m_pConnection->createStatement(  );
118 		if(xStmt.is())
119 			xStmt->execute(aSql);
120 		::comphelper::disposeComponent(xStmt);
121 	}
122 }
123 
124 // -------------------------------------------------------------------------
125