Files
libreoffice/connectivity/source/drivers/mysql/YUser.cxx

330 lines
12 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-12 22:04:38 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2002-11-11 07:30:50 +00:00
#include "mysql/YUser.hxx"
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include "connectivity/dbtools.hxx"
#include "connectivity/dbexception.hxx"
#include <com/sun/star/sdbcx/Privilege.hpp>
#include <com/sun/star/sdbcx/PrivilegeObject.hpp>
#include "TConnection.hxx"
2008-10-01 12:28:29 +00:00
#include "resource/common_res.hrc"
2002-11-11 07:30:50 +00:00
using namespace connectivity;
using namespace connectivity::mysql;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
2002-11-11 07:30:50 +00:00
OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : connectivity::sdbcx::OUser(sal_True)
,m_xConnection(_xConnection)
{
construct();
}
2002-11-11 07:30:50 +00:00
OMySQLUser::OMySQLUser( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
const OUString& _Name
2002-11-11 07:30:50 +00:00
) : connectivity::sdbcx::OUser(_Name,sal_True)
,m_xConnection(_xConnection)
{
construct();
}
2002-11-11 07:30:50 +00:00
void OMySQLUser::refreshGroups()
{
}
2002-11-11 07:30:50 +00:00
OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection) : OMySQLUser(_xConnection)
{
construct();
}
2002-11-11 07:30:50 +00:00
void OUserExtend::construct()
{
registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD), PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(static_cast< OUString*>(0)));
2002-11-11 07:30:50 +00:00
}
2002-11-11 07:30:50 +00:00
cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
{
Sequence< Property > aProps;
describeProperties(aProps);
return new cppu::OPropertyArrayHelper(aProps);
}
2002-11-11 07:30:50 +00:00
cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
{
return *OUserExtend_PROP::getArrayHelper();
}
typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
sal_Int32 SAL_CALL OMySQLUser::getPrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
2002-11-11 07:30:50 +00:00
{
::osl::MutexGuard aGuard(m_aMutex);
checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
sal_Int32 nRights,nRightsWithGrant;
findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
return nRights;
}
void OMySQLUser::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
2002-11-11 07:30:50 +00:00
{
nRightsWithGrant = nRights = 0;
// first we need to create the sql stmt to select the privs
Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
OUString sCatalog,sSchema,sTable;
2002-11-11 07:30:50 +00:00
::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
Reference<XResultSet> xRes;
switch(objType)
{
case PrivilegeObject::TABLE:
case PrivilegeObject::VIEW:
{
Any aCatalog;
if ( !sCatalog.isEmpty() )
2002-11-11 07:30:50 +00:00
aCatalog <<= sCatalog;
xRes = xMeta->getTablePrivileges(aCatalog,sSchema,sTable);
}
break;
case PrivilegeObject::COLUMN:
{
Any aCatalog;
if ( !sCatalog.isEmpty() )
2002-11-11 07:30:50 +00:00
aCatalog <<= sCatalog;
xRes = xMeta->getColumnPrivileges(aCatalog,sSchema,sTable,OUString("%"));
2002-11-11 07:30:50 +00:00
}
break;
}
if ( xRes.is() )
{
static const OUString sSELECT( "SELECT" );
static const OUString sINSERT( "INSERT" );
static const OUString sUPDATE( "UPDATE" );
static const OUString sDELETE( "DELETE" );
static const OUString sREAD( "READ" );
static const OUString sCREATE( "CREATE" );
static const OUString sALTER( "ALTER" );
static const OUString sREFERENCE( "REFERENCES" );
static const OUString sDROP( "DROP" );
static const OUString sYes( "YES" );
2002-11-11 07:30:50 +00:00
nRightsWithGrant = nRights = 0;
Reference<XRow> xCurrentRow(xRes,UNO_QUERY);
while( xCurrentRow.is() && xRes->next() )
{
OUString sGrantee = xCurrentRow->getString(5);
OUString sPrivilege = xCurrentRow->getString(6);
OUString sGrantable = xCurrentRow->getString(7);
2002-11-11 07:30:50 +00:00
if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
continue;
if (sPrivilege.equalsIgnoreAsciiCase(sSELECT))
{
nRights |= Privilege::SELECT;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::SELECT;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sINSERT))
{
nRights |= Privilege::INSERT;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::INSERT;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sUPDATE))
{
nRights |= Privilege::UPDATE;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::UPDATE;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sDELETE))
{
nRights |= Privilege::DELETE;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::DELETE;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sREAD))
{
nRights |= Privilege::READ;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::READ;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sCREATE))
{
nRights |= Privilege::CREATE;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::CREATE;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sALTER))
{
nRights |= Privilege::ALTER;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::ALTER;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sREFERENCE))
{
nRights |= Privilege::REFERENCE;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::REFERENCE;
}
else if (sPrivilege.equalsIgnoreAsciiCase(sDROP))
{
nRights |= Privilege::DROP;
if ( sGrantable.equalsIgnoreAsciiCase(sYes) )
nRightsWithGrant |= Privilege::DROP;
}
}
::comphelper::disposeComponent(xRes);
}
}
sal_Int32 SAL_CALL OMySQLUser::getGrantablePrivileges( const OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException, std::exception)
2002-11-11 07:30:50 +00:00
{
::osl::MutexGuard aGuard(m_aMutex);
checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
sal_Int32 nRights,nRightsWithGrant;
findPrivilegesAndGrantPrivileges(objName,objType,nRights,nRightsWithGrant);
return nRightsWithGrant;
}
void SAL_CALL OMySQLUser::grantPrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
2002-11-11 07:30:50 +00:00
{
if ( objType != PrivilegeObject::TABLE )
2008-10-01 12:28:29 +00:00
{
::connectivity::SharedResources aResources;
const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_GRANTED));
2008-10-01 12:28:29 +00:00
::dbtools::throwGenericSQLException(sError,*this);
} // if ( objType != PrivilegeObject::TABLE )
2002-11-11 07:30:50 +00:00
::osl::MutexGuard aGuard(m_aMutex);
OUString sPrivs = getPrivilegeString(objPrivileges);
if(!sPrivs.isEmpty())
2002-11-11 07:30:50 +00:00
{
Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
OUString sGrant = "GRANT " + sPrivs +
" ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
" TO " + m_Name;
2002-11-11 07:30:50 +00:00
Reference<XStatement> xStmt = m_xConnection->createStatement();
if(xStmt.is())
xStmt->execute(sGrant);
::comphelper::disposeComponent(xStmt);
}
}
void SAL_CALL OMySQLUser::revokePrivileges( const OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException, std::exception)
2002-11-11 07:30:50 +00:00
{
if ( objType != PrivilegeObject::TABLE )
2008-10-01 12:28:29 +00:00
{
::connectivity::SharedResources aResources;
const OUString sError( aResources.getResourceString(STR_PRIVILEGE_NOT_REVOKED));
2008-10-01 12:28:29 +00:00
::dbtools::throwGenericSQLException(sError,*this);
}
2002-11-11 07:30:50 +00:00
::osl::MutexGuard aGuard(m_aMutex);
checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
OUString sPrivs = getPrivilegeString(objPrivileges);
if(!sPrivs.isEmpty())
2002-11-11 07:30:50 +00:00
{
Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
OUString sGrant = "REVOKE " + sPrivs +
" ON " + ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation) +
" FROM " + m_Name;
2002-11-11 07:30:50 +00:00
Reference<XStatement> xStmt = m_xConnection->createStatement();
if(xStmt.is())
xStmt->execute(sGrant);
::comphelper::disposeComponent(xStmt);
}
}
2002-11-11 07:30:50 +00:00
// XUser
void SAL_CALL OMySQLUser::changePassword( const OUString& /*oldPassword*/, const OUString& newPassword ) throw(SQLException, RuntimeException, std::exception)
2002-11-11 07:30:50 +00:00
{
::osl::MutexGuard aGuard(m_aMutex);
checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
OUString sAlterPwd = "SET PASSWORD FOR " +
m_Name + "@\"%\" = PASSWORD('" +
newPassword + "')";
2002-11-11 07:30:50 +00:00
Reference<XStatement> xStmt = m_xConnection->createStatement();
if ( xStmt.is() )
{
xStmt->execute(sAlterPwd);
::comphelper::disposeComponent(xStmt);
}
}
OUString OMySQLUser::getPrivilegeString(sal_Int32 nRights) const
2002-11-11 07:30:50 +00:00
{
OUString sPrivs;
2002-11-11 07:30:50 +00:00
if((nRights & Privilege::INSERT) == Privilege::INSERT)
sPrivs += "INSERT";
2002-11-11 07:30:50 +00:00
if((nRights & Privilege::DELETE) == Privilege::DELETE)
{
if(!sPrivs.isEmpty())
sPrivs += ",";
sPrivs += "DELETE";
2002-11-11 07:30:50 +00:00
}
if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
{
if(!sPrivs.isEmpty())
sPrivs += ",";
sPrivs += "UPDATE";
2002-11-11 07:30:50 +00:00
}
if((nRights & Privilege::ALTER) == Privilege::ALTER)
{
if(!sPrivs.isEmpty())
sPrivs += ",";
sPrivs += "ALTER";
2002-11-11 07:30:50 +00:00
}
if((nRights & Privilege::SELECT) == Privilege::SELECT)
{
if(!sPrivs.isEmpty())
sPrivs += ",";
sPrivs += "SELECT";
2002-11-11 07:30:50 +00:00
}
if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
{
if(!sPrivs.isEmpty())
sPrivs += ",";
sPrivs += "REFERENCES";
2002-11-11 07:30:50 +00:00
}
return sPrivs;
}
2002-11-11 07:30:50 +00:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */