LCOV - code coverage report
Current view: top level - connectivity/source/drivers/mysql - YUser.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 172 0.0 %
Date: 2015-06-13 12:38:46 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(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 OUString& _Name
      47             :             ) : connectivity::sdbcx::OUser(_Name, 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,::cppu::UnoType<OUString>::get());
      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 OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
      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 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 :     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,OUString("%"));
     116             :             }
     117           0 :             break;
     118             :     }
     119             : 
     120           0 :     if ( xRes.is() )
     121             :     {
     122             :         static const char sSELECT   [] = "SELECT";
     123             :         static const char sINSERT   [] = "INSERT";
     124             :         static const char sUPDATE   [] = "UPDATE";
     125             :         static const char sDELETE   [] = "DELETE";
     126             :         static const char sREAD     [] = "READ";
     127             :         static const char sCREATE   [] = "CREATE";
     128             :         static const char sALTER    [] = "ALTER";
     129             :         static const char sREFERENCE[] = "REFERENCES";
     130             :         static const char sDROP     [] = "DROP";
     131             :         static const char 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 :             OUString sGrantee    = xCurrentRow->getString(5);
     139           0 :             OUString sPrivilege  = xCurrentRow->getString(6);
     140           0 :             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 OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
     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 OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
     215             : {
     216           0 :     if ( objType != PrivilegeObject::TABLE )
     217             :     {
     218           0 :         ::connectivity::SharedResources aResources;
     219           0 :         const 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 :     OUString sPrivs = getPrivilegeString(objPrivileges);
     226           0 :     if(!sPrivs.isEmpty())
     227             :     {
     228           0 :         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
     229           0 :         OUString sGrant = "GRANT " + sPrivs +
     230           0 :             " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
     231           0 :             " TO " + m_Name;
     232             : 
     233           0 :         Reference<XStatement> xStmt = m_xConnection->createStatement();
     234           0 :         if(xStmt.is())
     235           0 :             xStmt->execute(sGrant);
     236           0 :         ::comphelper::disposeComponent(xStmt);
     237           0 :     }
     238           0 : }
     239             : 
     240           0 : void SAL_CALL OMySQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
     241             : {
     242           0 :     if ( objType != PrivilegeObject::TABLE )
     243             :     {
     244           0 :         ::connectivity::SharedResources aResources;
     245           0 :         const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
     246           0 :         ::dbtools::throwGenericSQLException(sError,*this);
     247             :     }
     248             : 
     249           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     250           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
     251           0 :     OUString sPrivs = getPrivilegeString(objPrivileges);
     252           0 :     if(!sPrivs.isEmpty())
     253             :     {
     254           0 :         Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
     255           0 :         OUString sGrant = "REVOKE " + sPrivs +
     256           0 :             " ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
     257           0 :             " FROM " + m_Name;
     258             : 
     259           0 :         Reference<XStatement> xStmt = m_xConnection->createStatement();
     260           0 :         if(xStmt.is())
     261           0 :             xStmt->execute(sGrant);
     262           0 :         ::comphelper::disposeComponent(xStmt);
     263           0 :     }
     264           0 : }
     265             : 
     266             : // XUser
     267           0 : void SAL_CALL OMySQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException, std::exception)
     268             : {
     269           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     270           0 :     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
     271           0 :     OUString sAlterPwd = "SET PASSWORD FOR " +
     272           0 :         m_Name + "@\"%\" = PASSWORD('" +
     273           0 :         newPassword + "')";
     274             : 
     275             : 
     276           0 :     Reference<XStatement> xStmt = m_xConnection->createStatement();
     277           0 :     if ( xStmt.is() )
     278             :     {
     279           0 :         xStmt->execute(sAlterPwd);
     280           0 :         ::comphelper::disposeComponent(xStmt);
     281           0 :     }
     282           0 : }
     283             : 
     284           0 : OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights)
     285             : {
     286           0 :     OUString sPrivs;
     287           0 :     if((nRights & Privilege::INSERT) == Privilege::INSERT)
     288           0 :         sPrivs += "INSERT";
     289             : 
     290           0 :     if((nRights & Privilege::DELETE) == Privilege::DELETE)
     291             :     {
     292           0 :         if(!sPrivs.isEmpty())
     293           0 :             sPrivs += ",";
     294           0 :         sPrivs += "DELETE";
     295             :     }
     296             : 
     297           0 :     if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
     298             :     {
     299           0 :         if(!sPrivs.isEmpty())
     300           0 :             sPrivs += ",";
     301           0 :         sPrivs += "UPDATE";
     302             :     }
     303             : 
     304           0 :     if((nRights & Privilege::ALTER) == Privilege::ALTER)
     305             :     {
     306           0 :         if(!sPrivs.isEmpty())
     307           0 :             sPrivs += ",";
     308           0 :         sPrivs += "ALTER";
     309             :     }
     310             : 
     311           0 :     if((nRights & Privilege::SELECT) == Privilege::SELECT)
     312             :     {
     313           0 :         if(!sPrivs.isEmpty())
     314           0 :             sPrivs += ",";
     315           0 :         sPrivs += "SELECT";
     316             :     }
     317             : 
     318           0 :     if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
     319             :     {
     320           0 :         if(!sPrivs.isEmpty())
     321           0 :             sPrivs += ",";
     322           0 :         sPrivs += "REFERENCES";
     323             :     }
     324             : 
     325           0 :     return sPrivs;
     326             : }
     327             : 
     328             : 
     329             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11