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 <comphelper/types.hxx>
26 #include <com/sun/star/sdbcx/Privilege.hpp>
27 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
28 #include <TConnection.hxx>
29 #include <strings.hrc>
32 using namespace connectivity
;
33 using namespace connectivity::hsqldb
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::sdbcx
;
37 using namespace ::com::sun::star::sdbc
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::lang
;
41 OHSQLUser::OHSQLUser( css::uno::Reference
< css::sdbc::XConnection
> _xConnection
) : connectivity::sdbcx::OUser(true)
42 ,m_xConnection(std::move(_xConnection
))
47 OHSQLUser::OHSQLUser( css::uno::Reference
< css::sdbc::XConnection
> _xConnection
,
49 ) : connectivity::sdbcx::OUser(Name
,true)
50 ,m_xConnection(std::move(_xConnection
))
55 void OHSQLUser::refreshGroups()
59 OUserExtend::OUserExtend( const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
) : OHSQLUser(_xConnection
)
64 void OUserExtend::construct()
66 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD
), PROPERTY_ID_PASSWORD
,0,&m_Password
,::cppu::UnoType
<OUString
>::get());
69 cppu::IPropertyArrayHelper
* OUserExtend::createArrayHelper() const
71 Sequence
< Property
> aProps
;
72 describeProperties(aProps
);
73 return new cppu::OPropertyArrayHelper(aProps
);
76 cppu::IPropertyArrayHelper
& OUserExtend::getInfoHelper()
78 return *OUserExtend_PROP::getArrayHelper();
80 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
;
82 sal_Int32 SAL_CALL
OHSQLUser::getPrivileges( const OUString
& objName
, sal_Int32 objType
)
84 ::osl::MutexGuard
aGuard(m_aMutex
);
85 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
87 sal_Int32 nRights
,nRightsWithGrant
;
88 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
92 void OHSQLUser::findPrivilegesAndGrantPrivileges(const OUString
& objName
, sal_Int32 objType
,sal_Int32
& nRights
,sal_Int32
& nRightsWithGrant
)
94 nRightsWithGrant
= nRights
= 0;
95 // first we need to create the sql stmt to select the privs
96 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
97 OUString sCatalog
,sSchema
,sTable
;
98 ::dbtools::qualifiedNameComponents(xMeta
,objName
,sCatalog
,sSchema
,sTable
,::dbtools::EComposeRule::InDataManipulation
);
99 Reference
<XResultSet
> xRes
;
102 case PrivilegeObject::TABLE
:
103 case PrivilegeObject::VIEW
:
106 if ( !sCatalog
.isEmpty() )
107 aCatalog
<<= sCatalog
;
108 xRes
= xMeta
->getTablePrivileges(aCatalog
,sSchema
,sTable
);
112 case PrivilegeObject::COLUMN
:
115 if ( !sCatalog
.isEmpty() )
116 aCatalog
<<= sCatalog
;
117 xRes
= xMeta
->getColumnPrivileges(aCatalog
,sSchema
,sTable
,"%");
125 static const char sYes
[] = "YES";
127 nRightsWithGrant
= nRights
= 0;
129 Reference
<XRow
> xCurrentRow(xRes
,UNO_QUERY
);
130 while( xCurrentRow
.is() && xRes
->next() )
132 OUString sGrantee
= xCurrentRow
->getString(5);
133 OUString sPrivilege
= xCurrentRow
->getString(6);
134 OUString sGrantable
= xCurrentRow
->getString(7);
136 if (!m_Name
.equalsIgnoreAsciiCase(sGrantee
))
139 if (sPrivilege
.equalsIgnoreAsciiCase("SELECT"))
141 nRights
|= Privilege::SELECT
;
142 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
143 nRightsWithGrant
|= Privilege::SELECT
;
145 else if (sPrivilege
.equalsIgnoreAsciiCase("INSERT"))
147 nRights
|= Privilege::INSERT
;
148 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
149 nRightsWithGrant
|= Privilege::INSERT
;
151 else if (sPrivilege
.equalsIgnoreAsciiCase("UPDATE"))
153 nRights
|= Privilege::UPDATE
;
154 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
155 nRightsWithGrant
|= Privilege::UPDATE
;
157 else if (sPrivilege
.equalsIgnoreAsciiCase("DELETE"))
159 nRights
|= Privilege::DELETE
;
160 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
161 nRightsWithGrant
|= Privilege::DELETE
;
163 else if (sPrivilege
.equalsIgnoreAsciiCase("READ"))
165 nRights
|= Privilege::READ
;
166 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
167 nRightsWithGrant
|= Privilege::READ
;
169 else if (sPrivilege
.equalsIgnoreAsciiCase("CREATE"))
171 nRights
|= Privilege::CREATE
;
172 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
173 nRightsWithGrant
|= Privilege::CREATE
;
175 else if (sPrivilege
.equalsIgnoreAsciiCase("ALTER"))
177 nRights
|= Privilege::ALTER
;
178 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
179 nRightsWithGrant
|= Privilege::ALTER
;
181 else if (sPrivilege
.equalsIgnoreAsciiCase("REFERENCE"))
183 nRights
|= Privilege::REFERENCE
;
184 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
185 nRightsWithGrant
|= Privilege::REFERENCE
;
187 else if (sPrivilege
.equalsIgnoreAsciiCase("DROP"))
189 nRights
|= Privilege::DROP
;
190 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
191 nRightsWithGrant
|= Privilege::DROP
;
194 ::comphelper::disposeComponent(xRes
);
197 sal_Int32 SAL_CALL
OHSQLUser::getGrantablePrivileges( const OUString
& objName
, sal_Int32 objType
)
199 ::osl::MutexGuard
aGuard(m_aMutex
);
200 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
202 sal_Int32 nRights
,nRightsWithGrant
;
203 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
204 return nRightsWithGrant
;
207 void SAL_CALL
OHSQLUser::grantPrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
)
209 if ( objType
!= PrivilegeObject::TABLE
)
211 ::connectivity::SharedResources aResources
;
212 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_GRANTED
));
213 ::dbtools::throwGenericSQLException(sError
,*this);
214 } // if ( objType != PrivilegeObject::TABLE )
217 ::osl::MutexGuard
aGuard(m_aMutex
);
219 OUString sPrivs
= getPrivilegeString(objPrivileges
);
220 if(!sPrivs
.isEmpty())
222 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
223 OUString sGrant
= "GRANT " + sPrivs
+
224 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::EComposeRule::InDataManipulation
) +
225 " TO " + ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), m_Name
);
227 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
229 xStmt
->execute(sGrant
);
230 ::comphelper::disposeComponent(xStmt
);
234 void SAL_CALL
OHSQLUser::revokePrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
)
236 if ( objType
!= PrivilegeObject::TABLE
)
238 ::connectivity::SharedResources aResources
;
239 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_REVOKED
));
240 ::dbtools::throwGenericSQLException(sError
,*this);
241 } // if ( objType != PrivilegeObject::TABLE )
243 ::osl::MutexGuard
aGuard(m_aMutex
);
244 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
245 OUString sPrivs
= getPrivilegeString(objPrivileges
);
246 if(!sPrivs
.isEmpty())
248 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
249 OUString sGrant
= "REVOKE " + sPrivs
+
250 " ON " + ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::EComposeRule::InDataManipulation
) +
251 " FROM " + ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), m_Name
);
253 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
255 xStmt
->execute(sGrant
);
256 ::comphelper::disposeComponent(xStmt
);
261 void SAL_CALL
OHSQLUser::changePassword( const OUString
& /*oldPassword*/, const OUString
& newPassword
)
263 ::osl::MutexGuard
aGuard(m_aMutex
);
264 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
266 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
268 if( m_Name
!= xMeta
->getUserName() )
270 ::dbtools::throwGenericSQLException("HSQLDB can only change password of the current user.", *this);
273 OUString sAlterPwd
= "SET PASSWORD " +
274 ::dbtools::quoteName(xMeta
->getIdentifierQuoteString(), newPassword
);
276 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
279 xStmt
->execute(sAlterPwd
);
280 ::comphelper::disposeComponent(xStmt
);
284 OUString
OHSQLUser::getPrivilegeString(sal_Int32 nRights
)
287 if((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
290 if((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
292 if(!sPrivs
.isEmpty())
297 if((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
299 if(!sPrivs
.isEmpty())
304 if((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
306 if(!sPrivs
.isEmpty())
311 if((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
313 if(!sPrivs
.isEmpty())
318 if((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
320 if(!sPrivs
.isEmpty())
322 sPrivs
+= "REFERENCES";
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */