update dev300-m58
[ooovba.git] / connectivity / source / drivers / adabas / BUser.cxx
bloba672cf906efb5671479b7d547658554c1dd271de
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: BUser.cxx,v $
10 * $Revision: 1.17.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 "adabas/BUser.hxx"
34 #include "adabas/BGroups.hxx"
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include "adabas/BConnection.hxx"
38 #include "connectivity/dbtools.hxx"
39 #include "connectivity/dbexception.hxx"
40 #include <com/sun/star/sdbcx/Privilege.hpp>
41 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
42 #include "resource/adabas_res.hrc"
44 using namespace connectivity::adabas;
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 OAdabasUser::OAdabasUser( OAdabasConnection* _pConnection) : connectivity::sdbcx::OUser(sal_True)
53 ,m_pConnection(_pConnection)
55 construct();
57 // -------------------------------------------------------------------------
58 OAdabasUser::OAdabasUser( OAdabasConnection* _pConnection,
59 const ::rtl::OUString& _Name
60 ) : connectivity::sdbcx::OUser(_Name,sal_True)
61 ,m_pConnection(_pConnection)
63 construct();
65 // -------------------------------------------------------------------------
66 void OAdabasUser::refreshGroups()
68 if(!m_pConnection)
69 return;
71 TStringVector aVector;
72 aVector.reserve(7); // we don't know the excatly count of users but this should fit the normal need
73 Reference< XStatement > xStmt = m_pConnection->createStatement( );
74 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("SELECT DISTINCT GROUPNAME FROM DOMAIN.USERS WHERE GROUPNAME IS NOT NULL AND GROUPNAME <> ' ' AND USERNAME = '");
75 aSql += getName( );
76 aSql += ::rtl::OUString::createFromAscii("'");
78 Reference< XResultSet > xResult = xStmt->executeQuery(aSql);
79 if(xResult.is())
81 Reference< XRow > xRow(xResult,UNO_QUERY);
82 while(xResult->next())
83 aVector.push_back(xRow->getString(1));
84 ::comphelper::disposeComponent(xResult);
86 ::comphelper::disposeComponent(xStmt);
88 if(m_pGroups)
89 m_pGroups->reFill(aVector);
90 else
91 m_pGroups = new OGroups(*this,m_aMutex,aVector,m_pConnection,this);
93 // -------------------------------------------------------------------------
94 OUserExtend::OUserExtend( OAdabasConnection* _pConnection) : OAdabasUser(_pConnection)
96 construct();
98 // -------------------------------------------------------------------------
99 typedef connectivity::sdbcx::OUser OUser_TYPEDEF;
100 void OUserExtend::construct()
103 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
105 // -----------------------------------------------------------------------------
106 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
108 Sequence< Property > aProps;
109 describeProperties(aProps);
110 return new cppu::OPropertyArrayHelper(aProps);
112 // -------------------------------------------------------------------------
113 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
115 return *OUserExtend_PROP::getArrayHelper();
117 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
118 // -----------------------------------------------------------------------------
119 sal_Int32 SAL_CALL OAdabasUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
121 if ( objType != PrivilegeObject::TABLE )
122 return 0;
124 ::osl::MutexGuard aGuard(m_aMutex);
125 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
127 sal_Int32 nRights,nRightsWithGrant;
128 getAnyTablePrivileges(objName,nRights,nRightsWithGrant);
129 return nRights;
131 // -----------------------------------------------------------------------------
132 void OAdabasUser::getAnyTablePrivileges(const ::rtl::OUString& objName, sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
134 nRightsWithGrant = nRights = 0;
135 // first we need to create the sql stmt to select the privs
136 Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
137 ::rtl::OUString sCatalog,sSchema,sTable;
138 ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
139 Reference<XStatement> xStmt = m_pConnection->createStatement();
140 ::rtl::OUString sSql = ::rtl::OUString::createFromAscii("SELECT REFTABLENAME,PRIVILEGES FROM DOMAIN.USR_USES_TAB WHERE REFOBJTYPE <> 'SYSTEM' AND DEFUSERNAME = '");
141 sSql += m_Name;
142 sSql += ::rtl::OUString::createFromAscii("' AND REFTABLENAME = '");
143 sSql += sTable;
144 sSql += ::rtl::OUString::createFromAscii("'");
145 if(xStmt.is())
147 Reference<XResultSet> xRes = xStmt->executeQuery(sSql);
148 if(xRes.is())
150 Reference<XRow> xRow(xRes,UNO_QUERY);
151 if(xRow.is() && xRes->next())
153 ::rtl::OUString sPrivs = xRow->getString(2);
155 struct _priv_nam
157 const sal_Char* pAsciiName;
158 sal_Int32 nNumericValue;
159 } privileges[] =
161 { "INS", Privilege::INSERT },
162 { "DEL", Privilege::DELETE },
163 { "UPD", Privilege::UPDATE },
164 { "ALT", Privilege::ALTER },
165 { "SEL", Privilege::SELECT },
166 { "REF", Privilege::REFERENCE }
168 for ( size_t i = 0; i < sizeof( privileges ) / sizeof( privileges[0] ); ++i )
170 sal_Int32 nIndex = sPrivs.indexOf( ::rtl::OUString::createFromAscii( privileges[i].pAsciiName ) );
171 if ( nIndex == -1 )
172 continue;
174 nRights |= privileges[i].nNumericValue;
175 if ( sPrivs.copy( nIndex + 2, 1 ).equalsAscii( "+" ) )
176 nRightsWithGrant |= privileges[i].nNumericValue;
179 ::comphelper::disposeComponent(xRes);
181 ::comphelper::disposeComponent(xStmt);
184 // -------------------------------------------------------------------------
185 sal_Int32 SAL_CALL OAdabasUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
187 if ( objType != PrivilegeObject::TABLE )
188 return 0;
190 ::osl::MutexGuard aGuard(m_aMutex);
191 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
193 sal_Int32 nRights,nRightsWithGrant;
194 getAnyTablePrivileges(objName,nRights,nRightsWithGrant);
195 return nRightsWithGrant;
197 // -------------------------------------------------------------------------
198 void SAL_CALL OAdabasUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
200 if ( objType != PrivilegeObject::TABLE )
201 m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_GRANTED,*this);
203 ::osl::MutexGuard aGuard(m_aMutex);
204 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
205 if(sPrivs.getLength())
207 ::rtl::OUString sGrant;
208 sGrant += ::rtl::OUString::createFromAscii("GRANT ");
209 sGrant += sPrivs;
210 sGrant += ::rtl::OUString::createFromAscii(" ON ");
211 Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
212 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
213 sGrant += ::rtl::OUString::createFromAscii(" TO ");
214 sGrant += m_Name;
216 Reference<XStatement> xStmt = m_pConnection->createStatement();
217 if(xStmt.is())
218 xStmt->execute(sGrant);
219 ::comphelper::disposeComponent(xStmt);
222 // -------------------------------------------------------------------------
223 void SAL_CALL OAdabasUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
225 if ( objType != PrivilegeObject::TABLE )
226 m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_REVOKED,*this);
228 ::osl::MutexGuard aGuard(m_aMutex);
229 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
230 ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
231 if(sPrivs.getLength())
233 ::rtl::OUString sGrant;
234 sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
235 sGrant += sPrivs;
236 sGrant += ::rtl::OUString::createFromAscii(" ON ");
237 Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
238 sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
239 sGrant += ::rtl::OUString::createFromAscii(" FROM ");
240 sGrant += m_Name;
242 Reference<XStatement> xStmt = m_pConnection->createStatement();
243 if(xStmt.is())
244 xStmt->execute(sGrant);
245 ::comphelper::disposeComponent(xStmt);
248 // -----------------------------------------------------------------------------
249 // XUser
250 void SAL_CALL OAdabasUser::changePassword( const ::rtl::OUString& objPassword, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
252 ::osl::MutexGuard aGuard(m_aMutex);
253 checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
254 ::rtl::OUString sAlterPwd;
255 sAlterPwd = ::rtl::OUString::createFromAscii("ALTER PASSWORD \"");
256 sAlterPwd += objPassword.toAsciiUpperCase();
257 sAlterPwd += ::rtl::OUString::createFromAscii("\" TO \"") ;
258 sAlterPwd += newPassword.toAsciiUpperCase();
259 sAlterPwd += ::rtl::OUString::createFromAscii("\"") ;
261 sal_Bool bDisposeConnection = sal_False;
262 Reference<XConnection> xConnection = m_pConnection;
263 if(m_pConnection->getMetaData()->getUserName() != m_Name)
265 OAdabasConnection* pNewConnection = new OAdabasConnection(m_pConnection->getDriverHandle(),m_pConnection->getDriver());
266 xConnection = pNewConnection;
267 if(pNewConnection)
269 Sequence< PropertyValue> aSeq(2);
270 aSeq.getArray()[0].Name = ::rtl::OUString::createFromAscii("user") ;
271 aSeq.getArray()[0].Value <<= m_Name;
272 aSeq.getArray()[1].Name = ::rtl::OUString::createFromAscii("password") ;
273 aSeq.getArray()[1].Value <<= objPassword;
274 pNewConnection->Construct(m_pConnection->getMetaData()->getURL(),aSeq);
276 bDisposeConnection = sal_True;
278 if(xConnection.is())
280 Reference<XStatement> xStmt = xConnection->createStatement();
281 if(xStmt.is())
282 xStmt->execute(sAlterPwd);
283 ::comphelper::disposeComponent(xStmt);
284 if(bDisposeConnection)
285 ::comphelper::disposeComponent(xConnection);
287 else
288 ::dbtools::throwFunctionSequenceException(*this);
290 // -----------------------------------------------------------------------------
291 ::rtl::OUString OAdabasUser::getPrivilegeString(sal_Int32 nRights) const
293 ::rtl::OUString sPrivs;
294 if((nRights & Privilege::INSERT) == Privilege::INSERT)
295 sPrivs += ::rtl::OUString::createFromAscii("INSERT");
297 if((nRights & Privilege::DELETE) == Privilege::DELETE)
299 if(sPrivs.getLength())
300 sPrivs += ::rtl::OUString::createFromAscii(",");
301 sPrivs += ::rtl::OUString::createFromAscii("DELETE");
304 if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
306 if(sPrivs.getLength())
307 sPrivs += ::rtl::OUString::createFromAscii(",");
308 sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
311 if((nRights & Privilege::ALTER) == Privilege::ALTER)
313 if(sPrivs.getLength())
314 sPrivs += ::rtl::OUString::createFromAscii(",");
315 sPrivs += ::rtl::OUString::createFromAscii("ALTER");
318 if((nRights & Privilege::SELECT) == Privilege::SELECT)
320 if(sPrivs.getLength())
321 sPrivs += ::rtl::OUString::createFromAscii(",");
322 sPrivs += ::rtl::OUString::createFromAscii("SELECT");
325 if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
327 if(sPrivs.getLength())
328 sPrivs += ::rtl::OUString::createFromAscii(",");
329 sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
332 return sPrivs;
334 // -----------------------------------------------------------------------------