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
;
38 // -------------------------------------------------------------------------
39 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
) : connectivity::sdbcx::OUser(sal_True
)
40 ,m_xConnection(_xConnection
)
44 // -------------------------------------------------------------------------
45 OHSQLUser::OHSQLUser( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
47 ) : connectivity::sdbcx::OUser(_Name
,sal_True
)
48 ,m_xConnection(_xConnection
)
52 // -------------------------------------------------------------------------
53 void OHSQLUser::refreshGroups()
56 // -------------------------------------------------------------------------
57 OUserExtend::OUserExtend( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
) : OHSQLUser(_xConnection
)
61 // -------------------------------------------------------------------------
62 void OUserExtend::construct()
64 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD
), PROPERTY_ID_PASSWORD
,0,&m_Password
,::getCppuType(static_cast< OUString
*>(0)));
66 // -----------------------------------------------------------------------------
67 cppu::IPropertyArrayHelper
* OUserExtend::createArrayHelper() const
69 Sequence
< Property
> aProps
;
70 describeProperties(aProps
);
71 return new cppu::OPropertyArrayHelper(aProps
);
73 // -------------------------------------------------------------------------
74 cppu::IPropertyArrayHelper
& OUserExtend::getInfoHelper()
76 return *OUserExtend_PROP::getArrayHelper();
78 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
;
79 // -----------------------------------------------------------------------------
80 sal_Int32 SAL_CALL
OHSQLUser::getPrivileges( const OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
)
82 ::osl::MutexGuard
aGuard(m_aMutex
);
83 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
85 sal_Int32 nRights
,nRightsWithGrant
;
86 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
89 // -----------------------------------------------------------------------------
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::eInDataManipulation
);
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
,OUString("%"));
122 static const OUString
sSELECT( "SELECT" );
123 static const OUString
sINSERT( "INSERT" );
124 static const OUString
sUPDATE( "UPDATE" );
125 static const OUString
sDELETE( "DELETE" );
126 static const OUString
sREAD( "READ" );
127 static const OUString
sCREATE( "CREATE" );
128 static const OUString
sALTER( "ALTER" );
129 static const OUString
sREFERENCE( "REFERENCE" );
130 static const OUString
sDROP( "DROP" );
131 static const OUString
sYes( "YES" );
133 nRightsWithGrant
= nRights
= 0;
135 Reference
<XRow
> xCurrentRow(xRes
,UNO_QUERY
);
136 while( xCurrentRow
.is() && xRes
->next() )
138 OUString sGrantee
= xCurrentRow
->getString(5);
139 OUString sPrivilege
= xCurrentRow
->getString(6);
140 OUString sGrantable
= xCurrentRow
->getString(7);
142 if (!m_Name
.equalsIgnoreAsciiCase(sGrantee
))
145 if (sPrivilege
.equalsIgnoreAsciiCase(sSELECT
))
147 nRights
|= Privilege::SELECT
;
148 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
149 nRightsWithGrant
|= Privilege::SELECT
;
151 else if (sPrivilege
.equalsIgnoreAsciiCase(sINSERT
))
153 nRights
|= Privilege::INSERT
;
154 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
155 nRightsWithGrant
|= Privilege::INSERT
;
157 else if (sPrivilege
.equalsIgnoreAsciiCase(sUPDATE
))
159 nRights
|= Privilege::UPDATE
;
160 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
161 nRightsWithGrant
|= Privilege::UPDATE
;
163 else if (sPrivilege
.equalsIgnoreAsciiCase(sDELETE
))
165 nRights
|= Privilege::DELETE
;
166 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
167 nRightsWithGrant
|= Privilege::DELETE
;
169 else if (sPrivilege
.equalsIgnoreAsciiCase(sREAD
))
171 nRights
|= Privilege::READ
;
172 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
173 nRightsWithGrant
|= Privilege::READ
;
175 else if (sPrivilege
.equalsIgnoreAsciiCase(sCREATE
))
177 nRights
|= Privilege::CREATE
;
178 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
179 nRightsWithGrant
|= Privilege::CREATE
;
181 else if (sPrivilege
.equalsIgnoreAsciiCase(sALTER
))
183 nRights
|= Privilege::ALTER
;
184 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
185 nRightsWithGrant
|= Privilege::ALTER
;
187 else if (sPrivilege
.equalsIgnoreAsciiCase(sREFERENCE
))
189 nRights
|= Privilege::REFERENCE
;
190 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
191 nRightsWithGrant
|= Privilege::REFERENCE
;
193 else if (sPrivilege
.equalsIgnoreAsciiCase(sDROP
))
195 nRights
|= Privilege::DROP
;
196 if ( sGrantable
.equalsIgnoreAsciiCase(sYes
) )
197 nRightsWithGrant
|= Privilege::DROP
;
200 ::comphelper::disposeComponent(xRes
);
203 // -------------------------------------------------------------------------
204 sal_Int32 SAL_CALL
OHSQLUser::getGrantablePrivileges( const OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
)
206 ::osl::MutexGuard
aGuard(m_aMutex
);
207 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
209 sal_Int32 nRights
,nRightsWithGrant
;
210 findPrivilegesAndGrantPrivileges(objName
,objType
,nRights
,nRightsWithGrant
);
211 return nRightsWithGrant
;
213 // -------------------------------------------------------------------------
214 void SAL_CALL
OHSQLUser::grantPrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
)
216 if ( objType
!= PrivilegeObject::TABLE
)
218 ::connectivity::SharedResources aResources
;
219 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_GRANTED
));
220 ::dbtools::throwGenericSQLException(sError
,*this);
221 } // if ( objType != PrivilegeObject::TABLE )
224 ::osl::MutexGuard
aGuard(m_aMutex
);
226 OUString sPrivs
= getPrivilegeString(objPrivileges
);
227 if(!sPrivs
.isEmpty())
230 sGrant
+= OUString("GRANT ");
232 sGrant
+= OUString(" ON ");
233 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
234 sGrant
+= ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
);
235 sGrant
+= OUString(" TO ");
238 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
240 xStmt
->execute(sGrant
);
241 ::comphelper::disposeComponent(xStmt
);
244 // -------------------------------------------------------------------------
245 void SAL_CALL
OHSQLUser::revokePrivileges( const OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
)
247 if ( objType
!= PrivilegeObject::TABLE
)
249 ::connectivity::SharedResources aResources
;
250 const OUString
sError( aResources
.getResourceString(STR_PRIVILEGE_NOT_REVOKED
));
251 ::dbtools::throwGenericSQLException(sError
,*this);
252 } // if ( objType != PrivilegeObject::TABLE )
254 ::osl::MutexGuard
aGuard(m_aMutex
);
255 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
256 OUString sPrivs
= getPrivilegeString(objPrivileges
);
257 if(!sPrivs
.isEmpty())
260 sGrant
+= OUString("REVOKE ");
262 sGrant
+= OUString(" ON ");
263 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
264 sGrant
+= ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
);
265 sGrant
+= OUString(" FROM ");
268 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
270 xStmt
->execute(sGrant
);
271 ::comphelper::disposeComponent(xStmt
);
274 // -----------------------------------------------------------------------------
276 void SAL_CALL
OHSQLUser::changePassword( const OUString
& /*oldPassword*/, const OUString
& newPassword
) throw(SQLException
, RuntimeException
)
278 ::osl::MutexGuard
aGuard(m_aMutex
);
279 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
281 sAlterPwd
= OUString("SET PASSWORD FOR ");
283 sAlterPwd
+= OUString("@\"%\" = PASSWORD('") ;
284 sAlterPwd
+= newPassword
;
285 sAlterPwd
+= OUString("')") ;
288 Reference
<XStatement
> xStmt
= m_xConnection
->createStatement();
291 xStmt
->execute(sAlterPwd
);
292 ::comphelper::disposeComponent(xStmt
);
295 // -----------------------------------------------------------------------------
296 OUString
OHSQLUser::getPrivilegeString(sal_Int32 nRights
) const
299 if((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
300 sPrivs
+= OUString("INSERT");
302 if((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
304 if(!sPrivs
.isEmpty())
305 sPrivs
+= OUString(",");
306 sPrivs
+= OUString("DELETE");
309 if((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
311 if(!sPrivs
.isEmpty())
312 sPrivs
+= OUString(",");
313 sPrivs
+= OUString("UPDATE");
316 if((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
318 if(!sPrivs
.isEmpty())
319 sPrivs
+= OUString(",");
320 sPrivs
+= OUString("ALTER");
323 if((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
325 if(!sPrivs
.isEmpty())
326 sPrivs
+= OUString(",");
327 sPrivs
+= OUString("SELECT");
330 if((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
332 if(!sPrivs
.isEmpty())
333 sPrivs
+= OUString(",");
334 sPrivs
+= OUString("REFERENCES");
339 // -----------------------------------------------------------------------------
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */