1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "hsqldb/HUser.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/hsqldb_res.hrc"
30 using namespace connectivity
;
31 using namespace connectivity::hsqldb
;
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
;
39 OHSQLUser::OHSQLUser( const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
) : connectivity::sdbcx::OUser(true)
40 ,m_xConnection(_xConnection
)
45 OHSQLUser::OHSQLUser( const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,
47 ) : connectivity::sdbcx::OUser(Name
,true)
48 ,m_xConnection(_xConnection
)
53 void OHSQLUser::refreshGroups()
57 OUserExtend::OUserExtend( const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
) : OHSQLUser(_xConnection
)
62 void OUserExtend::construct()
64 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD
), PROPERTY_ID_PASSWORD
,0,&m_Password
,::cppu::UnoType
<OUString
>::get());
67 cppu::IPropertyArrayHelper
* OUserExtend::createArrayHelper() const
69 Sequence
< Property
> aProps
;
70 describeProperties(aProps
);
71 return new cppu::OPropertyArrayHelper(aProps
);
74 cppu::IPropertyArrayHelper
& OUserExtend::getInfoHelper()
76 return *OUserExtend_PROP::getArrayHelper();
78 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
;
80 sal_Int32 SAL_CALL
OHSQLUser::getPrivileges( const OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
, std::exception
)
82 ::osl::MutexGuard
aGuard(m_aMutex
);
83 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
85 sal_Int32 nRights
,nRightsWithGrant
;
86 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
90 void OHSQLUser::findPrivilegesAndGrantPrivileges(const OUString
& objName
, sal_Int32 objType
,sal_Int32
& nRights
,sal_Int32
& nRightsWithGrant
) throw(SQLException
, RuntimeException
)
92 nRightsWithGrant
= nRights
= 0;
93 // first we need to create the sql stmt to select the privs
94 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
95 OUString sCatalog
,sSchema
,sTable
;
96 ::dbtools::qualifiedNameComponents(xMeta
,objName
,sCatalog
,sSchema
,sTable
,::dbtools::EComposeRule::InDataManipulation
);
97 Reference
<XResultSet
> xRes
;
100 case PrivilegeObject::TABLE
:
101 case PrivilegeObject::VIEW
:
104 if ( !sCatalog
.isEmpty() )
105 aCatalog
<<= sCatalog
;
106 xRes
= xMeta
->getTablePrivileges(aCatalog
,sSchema
,sTable
);
110 case PrivilegeObject::COLUMN
:
113 if ( !sCatalog
.isEmpty() )
114 aCatalog
<<= sCatalog
;
115 xRes
= xMeta
->getColumnPrivileges(aCatalog
,sSchema
,sTable
,"%");
122 static const char sYes
[] = "YES";
124 nRightsWithGrant
= nRights
= 0;
126 Reference
<XRow
> xCurrentRow(xRes
,UNO_QUERY
);
127 while( xCurrentRow
.is() && xRes
->next() )
129 OUString sGrantee
= xCurrentRow
->getString(5);
130 OUString sPrivilege
= xCurrentRow
->getString(6);
131 OUString sGrantable
= xCurrentRow
->getString(7);
133 if (!m_Name
.equalsIgnoreAsciiCase(sGrantee
))
136 if (sPrivilege
.equalsIgnoreAsciiCase("SELECT"))
138 nRights
|= Privilege::SELECT
;
139 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
140 nRightsWithGrant
|= Privilege::SELECT
;
142 else if (sPrivilege
.equalsIgnoreAsciiCase("INSERT"))
144 nRights
|= Privilege::INSERT
;
145 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
146 nRightsWithGrant
|= Privilege::INSERT
;
148 else if (sPrivilege
.equalsIgnoreAsciiCase("UPDATE"))
150 nRights
|= Privilege::UPDATE
;
151 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
152 nRightsWithGrant
|= Privilege::UPDATE
;
154 else if (sPrivilege
.equalsIgnoreAsciiCase("DELETE"))
156 nRights
|= Privilege::DELETE
;
157 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
158 nRightsWithGrant
|= Privilege::DELETE
;
160 else if (sPrivilege
.equalsIgnoreAsciiCase("READ"))
162 nRights
|= Privilege::READ
;
163 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
164 nRightsWithGrant
|= Privilege::READ
;
166 else if (sPrivilege
.equalsIgnoreAsciiCase("CREATE"))
168 nRights
|= Privilege::CREATE
;
169 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
170 nRightsWithGrant
|= Privilege::CREATE
;
172 else if (sPrivilege
.equalsIgnoreAsciiCase("ALTER"))
174 nRights
|= Privilege::ALTER
;
175 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
176 nRightsWithGrant
|= Privilege::ALTER
;
178 else if (sPrivilege
.equalsIgnoreAsciiCase("REFERENCE"))
180 nRights
|= Privilege::REFERENCE
;
181 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
182 nRightsWithGrant
|= Privilege::REFERENCE
;
184 else if (sPrivilege
.equalsIgnoreAsciiCase("DROP"))
186 nRights
|= Privilege::DROP
;
187 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
188 nRightsWithGrant
|= Privilege::DROP
;
191 ::comphelper::disposeComponent(xRes
);
195 sal_Int32 SAL_CALL
OHSQLUser::getGrantablePrivileges( const OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
, std::exception
)
197 ::osl::MutexGuard
aGuard(m_aMutex
);
198 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
200 sal_Int32 nRights
,nRightsWithGrant
;
201 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
202 return nRightsWithGrant
;
205 void SAL_CALL
OHSQLUser::grantPrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
, std::exception
)
207 if ( objType
!= PrivilegeObject::TABLE
)
209 ::connectivity::SharedResources aResources
;
210 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_GRANTED
));
211 ::dbtools::throwGenericSQLException(sError
,*this);
212 } // if ( objType != PrivilegeObject::TABLE )
215 ::osl::MutexGuard
aGuard(m_aMutex
);
217 OUString sPrivs
= getPrivilegeString(objPrivileges
);
218 if(!sPrivs
.isEmpty())
220 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
221 OUString sGrant
= "GRANT " + sPrivs
+
222 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::EComposeRule::InDataManipulation
) +
223 " TO " + ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), m_Name
);
225 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
227 xStmt
->execute(sGrant
);
228 ::comphelper::disposeComponent(xStmt
);
232 void SAL_CALL
OHSQLUser::revokePrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
, std::exception
)
234 if ( objType
!= PrivilegeObject::TABLE
)
236 ::connectivity::SharedResources aResources
;
237 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_REVOKED
));
238 ::dbtools::throwGenericSQLException(sError
,*this);
239 } // if ( objType != PrivilegeObject::TABLE )
241 ::osl::MutexGuard
aGuard(m_aMutex
);
242 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
243 OUString sPrivs
= getPrivilegeString(objPrivileges
);
244 if(!sPrivs
.isEmpty())
246 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
247 OUString sGrant
= "REVOKE " + sPrivs
+
248 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::EComposeRule::InDataManipulation
) +
249 " FROM " + ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), m_Name
);
251 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
253 xStmt
->execute(sGrant
);
254 ::comphelper::disposeComponent(xStmt
);
259 void SAL_CALL
OHSQLUser::changePassword( const OUString
& /*oldPassword*/, const OUString
& newPassword
) throw(SQLException
, RuntimeException
, std::exception
)
261 ::osl::MutexGuard
aGuard(m_aMutex
);
262 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
264 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
266 if( m_Name
!= xMeta
->getUserName() )
268 ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
271 OUString sAlterPwd
= "SET PASSWORD " +
272 ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), newPassword
);
274 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
277 xStmt
->execute(sAlterPwd
);
278 ::comphelper::disposeComponent(xStmt
);
282 OUString
OHSQLUser::getPrivilegeString(sal_Int32 nRights
)
285 if((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
288 if((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
290 if(!sPrivs
.isEmpty())
295 if((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
297 if(!sPrivs
.isEmpty())
302 if((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
304 if(!sPrivs
.isEmpty())
309 if((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
311 if(!sPrivs
.isEmpty())
316 if((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
318 if(!sPrivs
.isEmpty())
320 sPrivs
+= "REFERENCES";
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */