19b5730f6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 39b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 49b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file 59b5730f6SAndrew Rist * distributed with this work for additional information 69b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 79b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the 89b5730f6SAndrew Rist * "License"); you may not use this file except in compliance 99b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 119b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 139b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing, 149b5730f6SAndrew Rist * software distributed under the License is distributed on an 159b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 169b5730f6SAndrew Rist * KIND, either express or implied. See the License for the 179b5730f6SAndrew Rist * specific language governing permissions and limitations 189b5730f6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 209b5730f6SAndrew Rist *************************************************************/ 219b5730f6SAndrew Rist 229b5730f6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 26cdf0e10cSrcweir #include "adabas/BUser.hxx" 27cdf0e10cSrcweir #include "adabas/BGroups.hxx" 28cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp> 29cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp> 30cdf0e10cSrcweir #include "adabas/BConnection.hxx" 31cdf0e10cSrcweir #include "connectivity/dbtools.hxx" 32cdf0e10cSrcweir #include "connectivity/dbexception.hxx" 33cdf0e10cSrcweir #include <com/sun/star/sdbcx/Privilege.hpp> 34cdf0e10cSrcweir #include <com/sun/star/sdbcx/PrivilegeObject.hpp> 35cdf0e10cSrcweir #include "resource/adabas_res.hrc" 36cdf0e10cSrcweir 37cdf0e10cSrcweir using namespace connectivity::adabas; 38cdf0e10cSrcweir using namespace ::com::sun::star::uno; 39cdf0e10cSrcweir using namespace ::com::sun::star::beans; 40cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx; 41cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 42cdf0e10cSrcweir using namespace ::com::sun::star::container; 43cdf0e10cSrcweir using namespace ::com::sun::star::lang; 44cdf0e10cSrcweir // ------------------------------------------------------------------------- 45cdf0e10cSrcweir OAdabasUser::OAdabasUser( OAdabasConnection* _pConnection) : connectivity::sdbcx::OUser(sal_True) 46cdf0e10cSrcweir ,m_pConnection(_pConnection) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir construct(); 49cdf0e10cSrcweir } 50cdf0e10cSrcweir // ------------------------------------------------------------------------- 51cdf0e10cSrcweir OAdabasUser::OAdabasUser( OAdabasConnection* _pConnection, 52cdf0e10cSrcweir const ::rtl::OUString& _Name 53cdf0e10cSrcweir ) : connectivity::sdbcx::OUser(_Name,sal_True) 54cdf0e10cSrcweir ,m_pConnection(_pConnection) 55cdf0e10cSrcweir { 56cdf0e10cSrcweir construct(); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir // ------------------------------------------------------------------------- 59cdf0e10cSrcweir void OAdabasUser::refreshGroups() 60cdf0e10cSrcweir { 61cdf0e10cSrcweir if(!m_pConnection) 62cdf0e10cSrcweir return; 63cdf0e10cSrcweir 64cdf0e10cSrcweir TStringVector aVector; 65cdf0e10cSrcweir aVector.reserve(7); // we don't know the excatly count of users but this should fit the normal need 66cdf0e10cSrcweir Reference< XStatement > xStmt = m_pConnection->createStatement( ); 67cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("SELECT DISTINCT GROUPNAME FROM DOMAIN.USERS WHERE GROUPNAME IS NOT NULL AND GROUPNAME <> ' ' AND USERNAME = '"); 68cdf0e10cSrcweir aSql += getName( ); 69cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii("'"); 70cdf0e10cSrcweir 71cdf0e10cSrcweir Reference< XResultSet > xResult = xStmt->executeQuery(aSql); 72cdf0e10cSrcweir if(xResult.is()) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY); 75cdf0e10cSrcweir while(xResult->next()) 76cdf0e10cSrcweir aVector.push_back(xRow->getString(1)); 77cdf0e10cSrcweir ::comphelper::disposeComponent(xResult); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 80cdf0e10cSrcweir 81cdf0e10cSrcweir if(m_pGroups) 82cdf0e10cSrcweir m_pGroups->reFill(aVector); 83cdf0e10cSrcweir else 84cdf0e10cSrcweir m_pGroups = new OGroups(*this,m_aMutex,aVector,m_pConnection,this); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir // ------------------------------------------------------------------------- 87cdf0e10cSrcweir OUserExtend::OUserExtend( OAdabasConnection* _pConnection) : OAdabasUser(_pConnection) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir construct(); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir // ------------------------------------------------------------------------- 92cdf0e10cSrcweir typedef connectivity::sdbcx::OUser OUser_TYPEDEF; 93cdf0e10cSrcweir void OUserExtend::construct() 94cdf0e10cSrcweir { 95cdf0e10cSrcweir 96*bccc1572SDon Lewis registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< ::rtl::OUString*>(NULL))); 97cdf0e10cSrcweir } 98cdf0e10cSrcweir // ----------------------------------------------------------------------------- 99cdf0e10cSrcweir cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const 100cdf0e10cSrcweir { 101cdf0e10cSrcweir Sequence< Property > aProps; 102cdf0e10cSrcweir describeProperties(aProps); 103cdf0e10cSrcweir return new cppu::OPropertyArrayHelper(aProps); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir // ------------------------------------------------------------------------- 106cdf0e10cSrcweir cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper() 107cdf0e10cSrcweir { 108cdf0e10cSrcweir return *OUserExtend_PROP::getArrayHelper(); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER; 111cdf0e10cSrcweir // ----------------------------------------------------------------------------- 112cdf0e10cSrcweir sal_Int32 SAL_CALL OAdabasUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir if ( objType != PrivilegeObject::TABLE ) 115cdf0e10cSrcweir return 0; 116cdf0e10cSrcweir 117cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 118cdf0e10cSrcweir checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed); 119cdf0e10cSrcweir 120cdf0e10cSrcweir sal_Int32 nRights,nRightsWithGrant; 121cdf0e10cSrcweir getAnyTablePrivileges(objName,nRights,nRightsWithGrant); 122cdf0e10cSrcweir return nRights; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir // ----------------------------------------------------------------------------- 125cdf0e10cSrcweir void OAdabasUser::getAnyTablePrivileges(const ::rtl::OUString& objName, sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir nRightsWithGrant = nRights = 0; 128cdf0e10cSrcweir // first we need to create the sql stmt to select the privs 129cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData(); 130cdf0e10cSrcweir ::rtl::OUString sCatalog,sSchema,sTable; 131cdf0e10cSrcweir ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation); 132cdf0e10cSrcweir Reference<XStatement> xStmt = m_pConnection->createStatement(); 133cdf0e10cSrcweir ::rtl::OUString sSql = ::rtl::OUString::createFromAscii("SELECT REFTABLENAME,PRIVILEGES FROM DOMAIN.USR_USES_TAB WHERE REFOBJTYPE <> 'SYSTEM' AND DEFUSERNAME = '"); 134cdf0e10cSrcweir sSql += m_Name; 135cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("' AND REFTABLENAME = '"); 136cdf0e10cSrcweir sSql += sTable; 137cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("'"); 138cdf0e10cSrcweir if(xStmt.is()) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir Reference<XResultSet> xRes = xStmt->executeQuery(sSql); 141cdf0e10cSrcweir if(xRes.is()) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir Reference<XRow> xRow(xRes,UNO_QUERY); 144cdf0e10cSrcweir if(xRow.is() && xRes->next()) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir ::rtl::OUString sPrivs = xRow->getString(2); 147cdf0e10cSrcweir 148cdf0e10cSrcweir struct _priv_nam 149cdf0e10cSrcweir { 150cdf0e10cSrcweir const sal_Char* pAsciiName; 151cdf0e10cSrcweir sal_Int32 nNumericValue; 152cdf0e10cSrcweir } privileges[] = 153cdf0e10cSrcweir { 154cdf0e10cSrcweir { "INS", Privilege::INSERT }, 155cdf0e10cSrcweir { "DEL", Privilege::DELETE }, 156cdf0e10cSrcweir { "UPD", Privilege::UPDATE }, 157cdf0e10cSrcweir { "ALT", Privilege::ALTER }, 158cdf0e10cSrcweir { "SEL", Privilege::SELECT }, 159cdf0e10cSrcweir { "REF", Privilege::REFERENCE } 160cdf0e10cSrcweir }; 161cdf0e10cSrcweir for ( size_t i = 0; i < sizeof( privileges ) / sizeof( privileges[0] ); ++i ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir sal_Int32 nIndex = sPrivs.indexOf( ::rtl::OUString::createFromAscii( privileges[i].pAsciiName ) ); 164cdf0e10cSrcweir if ( nIndex == -1 ) 165cdf0e10cSrcweir continue; 166cdf0e10cSrcweir 167cdf0e10cSrcweir nRights |= privileges[i].nNumericValue; 168cdf0e10cSrcweir if ( sPrivs.copy( nIndex + 2, 1 ).equalsAscii( "+" ) ) 169cdf0e10cSrcweir nRightsWithGrant |= privileges[i].nNumericValue; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir } 172cdf0e10cSrcweir ::comphelper::disposeComponent(xRes); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir // ------------------------------------------------------------------------- 178cdf0e10cSrcweir sal_Int32 SAL_CALL OAdabasUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir if ( objType != PrivilegeObject::TABLE ) 181cdf0e10cSrcweir return 0; 182cdf0e10cSrcweir 183cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 184cdf0e10cSrcweir checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed); 185cdf0e10cSrcweir 186cdf0e10cSrcweir sal_Int32 nRights,nRightsWithGrant; 187cdf0e10cSrcweir getAnyTablePrivileges(objName,nRights,nRightsWithGrant); 188cdf0e10cSrcweir return nRightsWithGrant; 189cdf0e10cSrcweir } 190cdf0e10cSrcweir // ------------------------------------------------------------------------- 191cdf0e10cSrcweir void SAL_CALL OAdabasUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir if ( objType != PrivilegeObject::TABLE ) 194cdf0e10cSrcweir m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_GRANTED,*this); 195cdf0e10cSrcweir 196cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 197cdf0e10cSrcweir ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges); 198cdf0e10cSrcweir if(sPrivs.getLength()) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir ::rtl::OUString sGrant; 201cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii("GRANT "); 202cdf0e10cSrcweir sGrant += sPrivs; 203cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii(" ON "); 204cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData(); 205cdf0e10cSrcweir sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation); 206cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii(" TO "); 207cdf0e10cSrcweir sGrant += m_Name; 208cdf0e10cSrcweir 209cdf0e10cSrcweir Reference<XStatement> xStmt = m_pConnection->createStatement(); 210cdf0e10cSrcweir if(xStmt.is()) 211cdf0e10cSrcweir xStmt->execute(sGrant); 212cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir } 215cdf0e10cSrcweir // ------------------------------------------------------------------------- 216cdf0e10cSrcweir void SAL_CALL OAdabasUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir if ( objType != PrivilegeObject::TABLE ) 219cdf0e10cSrcweir m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_REVOKED,*this); 220cdf0e10cSrcweir 221cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 222cdf0e10cSrcweir checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed); 223cdf0e10cSrcweir ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges); 224cdf0e10cSrcweir if(sPrivs.getLength()) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir ::rtl::OUString sGrant; 227cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii("REVOKE "); 228cdf0e10cSrcweir sGrant += sPrivs; 229cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii(" ON "); 230cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData(); 231cdf0e10cSrcweir sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation); 232cdf0e10cSrcweir sGrant += ::rtl::OUString::createFromAscii(" FROM "); 233cdf0e10cSrcweir sGrant += m_Name; 234cdf0e10cSrcweir 235cdf0e10cSrcweir Reference<XStatement> xStmt = m_pConnection->createStatement(); 236cdf0e10cSrcweir if(xStmt.is()) 237cdf0e10cSrcweir xStmt->execute(sGrant); 238cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 239cdf0e10cSrcweir } 240cdf0e10cSrcweir } 241cdf0e10cSrcweir // ----------------------------------------------------------------------------- 242cdf0e10cSrcweir // XUser 243cdf0e10cSrcweir void SAL_CALL OAdabasUser::changePassword( const ::rtl::OUString& objPassword, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 246cdf0e10cSrcweir checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed); 247cdf0e10cSrcweir ::rtl::OUString sAlterPwd; 248cdf0e10cSrcweir sAlterPwd = ::rtl::OUString::createFromAscii("ALTER PASSWORD \""); 249cdf0e10cSrcweir sAlterPwd += objPassword.toAsciiUpperCase(); 250cdf0e10cSrcweir sAlterPwd += ::rtl::OUString::createFromAscii("\" TO \"") ; 251cdf0e10cSrcweir sAlterPwd += newPassword.toAsciiUpperCase(); 252cdf0e10cSrcweir sAlterPwd += ::rtl::OUString::createFromAscii("\"") ; 253cdf0e10cSrcweir 254cdf0e10cSrcweir sal_Bool bDisposeConnection = sal_False; 255cdf0e10cSrcweir Reference<XConnection> xConnection = m_pConnection; 256cdf0e10cSrcweir if(m_pConnection->getMetaData()->getUserName() != m_Name) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir OAdabasConnection* pNewConnection = new OAdabasConnection(m_pConnection->getDriverHandle(),m_pConnection->getDriver()); 259cdf0e10cSrcweir xConnection = pNewConnection; 260cdf0e10cSrcweir if(pNewConnection) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir Sequence< PropertyValue> aSeq(2); 263cdf0e10cSrcweir aSeq.getArray()[0].Name = ::rtl::OUString::createFromAscii("user") ; 264cdf0e10cSrcweir aSeq.getArray()[0].Value <<= m_Name; 265cdf0e10cSrcweir aSeq.getArray()[1].Name = ::rtl::OUString::createFromAscii("password") ; 266cdf0e10cSrcweir aSeq.getArray()[1].Value <<= objPassword; 267cdf0e10cSrcweir pNewConnection->Construct(m_pConnection->getMetaData()->getURL(),aSeq); 268cdf0e10cSrcweir } 269cdf0e10cSrcweir bDisposeConnection = sal_True; 270cdf0e10cSrcweir } 271cdf0e10cSrcweir if(xConnection.is()) 272cdf0e10cSrcweir { 273cdf0e10cSrcweir Reference<XStatement> xStmt = xConnection->createStatement(); 274cdf0e10cSrcweir if(xStmt.is()) 275cdf0e10cSrcweir xStmt->execute(sAlterPwd); 276cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 277cdf0e10cSrcweir if(bDisposeConnection) 278cdf0e10cSrcweir ::comphelper::disposeComponent(xConnection); 279cdf0e10cSrcweir } 280cdf0e10cSrcweir else 281cdf0e10cSrcweir ::dbtools::throwFunctionSequenceException(*this); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir // ----------------------------------------------------------------------------- 284cdf0e10cSrcweir ::rtl::OUString OAdabasUser::getPrivilegeString(sal_Int32 nRights) const 285cdf0e10cSrcweir { 286cdf0e10cSrcweir ::rtl::OUString sPrivs; 287cdf0e10cSrcweir if((nRights & Privilege::INSERT) == Privilege::INSERT) 288cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("INSERT"); 289cdf0e10cSrcweir 290cdf0e10cSrcweir if((nRights & Privilege::DELETE) == Privilege::DELETE) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir if(sPrivs.getLength()) 293cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii(","); 294cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("DELETE"); 295cdf0e10cSrcweir } 296cdf0e10cSrcweir 297cdf0e10cSrcweir if((nRights & Privilege::UPDATE) == Privilege::UPDATE) 298cdf0e10cSrcweir { 299cdf0e10cSrcweir if(sPrivs.getLength()) 300cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii(","); 301cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("UPDATE"); 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir if((nRights & Privilege::ALTER) == Privilege::ALTER) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir if(sPrivs.getLength()) 307cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii(","); 308cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("ALTER"); 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir if((nRights & Privilege::SELECT) == Privilege::SELECT) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir if(sPrivs.getLength()) 314cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii(","); 315cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("SELECT"); 316cdf0e10cSrcweir } 317cdf0e10cSrcweir 318cdf0e10cSrcweir if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir if(sPrivs.getLength()) 321cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii(","); 322cdf0e10cSrcweir sPrivs += ::rtl::OUString::createFromAscii("REFERENCES"); 323cdf0e10cSrcweir } 324cdf0e10cSrcweir 325cdf0e10cSrcweir return sPrivs; 326cdf0e10cSrcweir } 327cdf0e10cSrcweir // ----------------------------------------------------------------------------- 328cdf0e10cSrcweir 329