LCOV - code coverage report
Current view: top level - libreoffice/connectivity/source/drivers/mysql - YUser.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 193 0.0 %
Date: 2012-12-27 Functions: 0 14 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "mysql/YUser.hxx"
      21             : #include <com/sun/star/sdbc/XRow.hpp>
      22             : #include <com/sun/star/sdbc/XResultSet.hpp>
      23             : #include "connectivity/dbtools.hxx"
      24             : #include "connectivity/dbexception.hxx"
      25             : #include <com/sun/star/sdbcx/Privilege.hpp>
      26             : #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
      27             : #include "TConnection.hxx"
      28             : #include "resource/common_res.hrc"
      29             : 
      30             : using namespace connectivity;
      31             : using namespace connectivity::mysql;
      32             : using namespace ::com::sun::star::uno;
      33             : using namespace ::com::sun::star::beans;
      34             : using namespace ::com::sun::star::sdbcx;
      35             : using namespace ::com::sun::star::sdbc;
      36             : using namespace ::com::sun::star::container;
      37             : using namespace ::com::sun::star::lang;
      38             : // -------------------------------------------------------------------------
      39           0 : OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
      40           0 :                 ,m_xConnection(_xConnection)
      41             : {
      42           0 :     construct();
      43           0 : }
      44             : // -------------------------------------------------------------------------
      45           0 : OMySQLUser::OMySQLUser(   const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
      46             :                 const ::rtl::OUString& _Name
      47             :             ) : connectivity::sdbcx::OUser(_Name,sal_True)
      48           0 :                 ,m_xConnection(_xConnection)
      49             : {
      50           0 :     construct();
      51           0 : }
      52             : // -------------------------------------------------------------------------
      53           0 : void OMySQLUser::refreshGroups()
      54             : {
      55           0 : }
      56             : // -------------------------------------------------------------------------
      57           0 : OUserExtend::OUserExtend(   const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OMySQLUser(_xConnection)
      58             : {
      59           0 :     construct();
      60           0 : }
      61             : // -------------------------------------------------------------------------
      62           0 : void OUserExtend::construct()
      63             : {
      64           0 :     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),    PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< ::rtl::OUString*>(0)));
      65           0 : }
      66             : // -----------------------------------------------------------------------------
      67           0 : cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
      68             : {
      69           0 :     Sequence< Property > aProps;
      70           0 :     describeProperties(aProps);
      71           0 :     return new cppu::OPropertyArrayHelper(aProps);
      72             : }
      73             : // -------------------------------------------------------------------------
      74           0 : cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
      75             : {
      76           0 :     return *OUserExtend_PROP::getArrayHelper();
      77             : }
      78             : typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
      79             : // -----------------------------------------------------------------------------
      80           0 : sal_Int32 SAL_CALL OMySQLUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
      81             : {
      82           0 :     ::osl::MutexGuard aGuard(m_aMutex);
      83           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
      84             : 
      85             :     sal_Int32 nRights,nRightsWithGrant;
      86           0 :     findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
      87           0 :     return nRights;
      88             : }
      89             : // -----------------------------------------------------------------------------
      90           0 : void OMySQLUser::findPrivilegesAndGrantPrivileges(const ::rtl::OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
      91             : {
      92           0 :     nRightsWithGrant = nRights = 0;
      93             :     // first we need to create the sql stmt to select the privs
      94           0 :     Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
      95           0 :     ::rtl::OUString sCatalog,sSchema,sTable;
      96           0 :     ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
      97           0 :     Reference<XResultSet> xRes;
      98           0 :     switch(objType)
      99             :     {
     100             :         case PrivilegeObject::TABLE:
     101             :         case PrivilegeObject::VIEW:
     102             :             {
     103           0 :                 Any aCatalog;
     104           0 :                 if ( !sCatalog.isEmpty() )
     105           0 :                     aCatalog <<= sCatalog;
     106           0 :                 xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
     107             :             }
     108           0 :             break;
     109             : 
     110             :         case PrivilegeObject::COLUMN:
     111             :             {
     112           0 :                 Any aCatalog;
     113           0 :                 if ( !sCatalog.isEmpty() )
     114           0 :                     aCatalog <<= sCatalog;
     115           0 :                 xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,::rtl::OUString("%"));
     116             :             }
     117           0 :             break;
     118             :     }
     119             : 
     120           0 :     if ( xRes.is() )
     121             :     {
     122           0 :         static const ::rtl::OUString sSELECT(  "SELECT" );
     123           0 :         static const ::rtl::OUString sINSERT(  "INSERT" );
     124           0 :         static const ::rtl::OUString sUPDATE(  "UPDATE" );
     125           0 :         static const ::rtl::OUString sDELETE(  "DELETE" );
     126           0 :         static const ::rtl::OUString sREAD(  "READ" );
     127           0 :         static const ::rtl::OUString sCREATE(  "CREATE" );
     128           0 :         static const ::rtl::OUString sALTER(  "ALTER" );
     129           0 :         static const ::rtl::OUString sREFERENCE(  "REFERENCE" );
     130           0 :         static const ::rtl::OUString sDROP(  "DROP" );
     131           0 :         static const ::rtl::OUString sYes(  "YES" );
     132             : 
     133           0 :         nRightsWithGrant = nRights = 0;
     134             : 
     135           0 :         Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
     136           0 :         while( xCurrentRow.is() && xRes->next() )
     137             :         {
     138           0 :             ::rtl::OUString sGrantee    = xCurrentRow->getString(5);
     139           0 :             ::rtl::OUString sPrivilege  = xCurrentRow->getString(6);
     140           0 :             ::rtl::OUString sGrantable  = xCurrentRow->getString(7);
     141             : 
     142           0 :             if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
     143           0 :                 continue;
     144             : 
     145           0 :             if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
     146             :             {
     147           0 :                 nRights |= Privilege::SELECT;
     148           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     149           0 :                     nRightsWithGrant |= Privilege::SELECT;
     150             :             }
     151           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
     152             :             {
     153           0 :                 nRights |= Privilege::INSERT;
     154           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     155           0 :                     nRightsWithGrant |= Privilege::INSERT;
     156             :             }
     157           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
     158             :             {
     159           0 :                 nRights |= Privilege::UPDATE;
     160           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     161           0 :                     nRightsWithGrant |= Privilege::UPDATE;
     162             :             }
     163           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
     164             :             {
     165           0 :                 nRights |= Privilege::DELETE;
     166           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     167           0 :                     nRightsWithGrant |= Privilege::DELETE;
     168             :             }
     169           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
     170             :             {
     171           0 :                 nRights |= Privilege::READ;
     172           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     173           0 :                     nRightsWithGrant |= Privilege::READ;
     174             :             }
     175           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
     176             :             {
     177           0 :                 nRights |= Privilege::CREATE;
     178           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     179           0 :                     nRightsWithGrant |= Privilege::CREATE;
     180             :             }
     181           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
     182             :             {
     183           0 :                 nRights |= Privilege::ALTER;
     184           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     185           0 :                     nRightsWithGrant |= Privilege::ALTER;
     186             :             }
     187           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
     188             :             {
     189           0 :                 nRights |= Privilege::REFERENCE;
     190           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     191           0 :                     nRightsWithGrant |= Privilege::REFERENCE;
     192             :             }
     193           0 :             else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
     194             :             {
     195           0 :                 nRights |= Privilege::DROP;
     196           0 :                 if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
     197           0 :                     nRightsWithGrant |= Privilege::DROP;
     198             :             }
     199           0 :         }
     200           0 :         ::comphelper::disposeComponent(xRes);
     201           0 :     }
     202           0 : }
     203             : // -------------------------------------------------------------------------
     204           0 : sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
     205             : {
     206           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     207           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
     208             : 
     209             :     sal_Int32 nRights,nRightsWithGrant;
     210           0 :     findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
     211           0 :     return nRightsWithGrant;
     212             : }
     213             : // -------------------------------------------------------------------------
     214           0 : void SAL_CALL OMySQLUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
     215             : {
     216           0 :     if ( objType != PrivilegeObject::TABLE )
     217             :     {
     218           0 :         ::connectivity::SharedResources aResources;
     219           0 :         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
     220           0 :         ::dbtools::throwGenericSQLException(sError,*this);
     221             :     } // if ( objType != PrivilegeObject::TABLE )
     222             : 
     223           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     224             : 
     225           0 :     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
     226           0 :     if(!sPrivs.isEmpty())
     227             :     {
     228           0 :         ::rtl::OUString sGrant;
     229           0 :         sGrant += ::rtl::OUString("GRANT ");
     230           0 :         sGrant += sPrivs;
     231           0 :         sGrant += ::rtl::OUString(" ON ");
     232           0 :         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
     233           0 :         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
     234           0 :         sGrant += ::rtl::OUString(" TO ");
     235           0 :         sGrant += m_Name;
     236             : 
     237           0 :         Reference<XStatement> xStmt = m_xConnection->createStatement();
     238           0 :         if(xStmt.is())
     239           0 :             xStmt->execute(sGrant);
     240           0 :         ::comphelper::disposeComponent(xStmt);
     241           0 :     }
     242           0 : }
     243             : // -------------------------------------------------------------------------
     244           0 : void SAL_CALL OMySQLUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
     245             : {
     246           0 :     if ( objType != PrivilegeObject::TABLE )
     247             :     {
     248           0 :         ::connectivity::SharedResources aResources;
     249           0 :         const ::rtl::OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
     250           0 :         ::dbtools::throwGenericSQLException(sError,*this);
     251             :     }
     252             : 
     253           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     254           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
     255           0 :     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
     256           0 :     if(!sPrivs.isEmpty())
     257             :     {
     258           0 :         ::rtl::OUString sGrant;
     259           0 :         sGrant += ::rtl::OUString("REVOKE ");
     260           0 :         sGrant += sPrivs;
     261           0 :         sGrant += ::rtl::OUString(" ON ");
     262           0 :         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
     263           0 :         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
     264           0 :         sGrant += ::rtl::OUString(" FROM ");
     265           0 :         sGrant += m_Name;
     266             : 
     267           0 :         Reference<XStatement> xStmt = m_xConnection->createStatement();
     268           0 :         if(xStmt.is())
     269           0 :             xStmt->execute(sGrant);
     270           0 :         ::comphelper::disposeComponent(xStmt);
     271           0 :     }
     272           0 : }
     273             : // -----------------------------------------------------------------------------
     274             : // XUser
     275           0 : void SAL_CALL OMySQLUser::changePassword( const ::rtl::OUString& /*oldPassword*/, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
     276             : {
     277           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     278           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
     279           0 :     ::rtl::OUString sAlterPwd;
     280           0 :     sAlterPwd = ::rtl::OUString("SET PASSWORD FOR ");
     281           0 :     sAlterPwd += m_Name;
     282           0 :     sAlterPwd += ::rtl::OUString("@\"%\" = PASSWORD('") ;
     283           0 :     sAlterPwd += newPassword;
     284           0 :     sAlterPwd += ::rtl::OUString("')") ;
     285             : 
     286             : 
     287           0 :     Reference<XStatement> xStmt = m_xConnection->createStatement();
     288           0 :     if ( xStmt.is() )
     289             :     {
     290           0 :         xStmt->execute(sAlterPwd);
     291           0 :         ::comphelper::disposeComponent(xStmt);
     292           0 :     }
     293           0 : }
     294             : // -----------------------------------------------------------------------------
     295           0 : ::rtl::OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights) const
     296             : {
     297           0 :     ::rtl::OUString sPrivs;
     298           0 :     if((nRights & Privilege::INSERT) == Privilege::INSERT)
     299           0 :         sPrivs += ::rtl::OUString("INSERT");
     300             : 
     301           0 :     if((nRights & Privilege::DELETE) == Privilege::DELETE)
     302             :     {
     303           0 :         if(!sPrivs.isEmpty())
     304           0 :             sPrivs += ::rtl::OUString(",");
     305           0 :         sPrivs += ::rtl::OUString("DELETE");
     306             :     }
     307             : 
     308           0 :     if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
     309             :     {
     310           0 :         if(!sPrivs.isEmpty())
     311           0 :             sPrivs += ::rtl::OUString(",");
     312           0 :         sPrivs += ::rtl::OUString("UPDATE");
     313             :     }
     314             : 
     315           0 :     if((nRights & Privilege::ALTER) == Privilege::ALTER)
     316             :     {
     317           0 :         if(!sPrivs.isEmpty())
     318           0 :             sPrivs += ::rtl::OUString(",");
     319           0 :         sPrivs += ::rtl::OUString("ALTER");
     320             :     }
     321             : 
     322           0 :     if((nRights & Privilege::SELECT) == Privilege::SELECT)
     323             :     {
     324           0 :         if(!sPrivs.isEmpty())
     325           0 :             sPrivs += ::rtl::OUString(",");
     326           0 :         sPrivs += ::rtl::OUString("SELECT");
     327             :     }
     328             : 
     329           0 :     if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
     330             :     {
     331           0 :         if(!sPrivs.isEmpty())
     332           0 :             sPrivs += ::rtl::OUString(",");
     333           0 :         sPrivs += ::rtl::OUString("REFERENCES");
     334             :     }
     335             : 
     336           0 :     return sPrivs;
     337             : }
     338             : // -----------------------------------------------------------------------------
     339             : 
     340             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10