1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BUser.cxx,v $
10 * $Revision: 1.17.56.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "adabas/BUser.hxx"
34 #include "adabas/BGroups.hxx"
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include "adabas/BConnection.hxx"
38 #include "connectivity/dbtools.hxx"
39 #include "connectivity/dbexception.hxx"
40 #include <com/sun/star/sdbcx/Privilege.hpp>
41 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
42 #include "resource/adabas_res.hrc"
44 using namespace connectivity::adabas
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::beans
;
47 using namespace ::com::sun::star::sdbcx
;
48 using namespace ::com::sun::star::sdbc
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::com::sun::star::lang
;
51 // -------------------------------------------------------------------------
52 OAdabasUser::OAdabasUser( OAdabasConnection
* _pConnection
) : connectivity::sdbcx::OUser(sal_True
)
53 ,m_pConnection(_pConnection
)
57 // -------------------------------------------------------------------------
58 OAdabasUser::OAdabasUser( OAdabasConnection
* _pConnection
,
59 const ::rtl::OUString
& _Name
60 ) : connectivity::sdbcx::OUser(_Name
,sal_True
)
61 ,m_pConnection(_pConnection
)
65 // -------------------------------------------------------------------------
66 void OAdabasUser::refreshGroups()
71 TStringVector aVector
;
72 aVector
.reserve(7); // we don't know the excatly count of users but this should fit the normal need
73 Reference
< XStatement
> xStmt
= m_pConnection
->createStatement( );
74 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("SELECT DISTINCT GROUPNAME FROM DOMAIN.USERS WHERE GROUPNAME IS NOT NULL AND GROUPNAME <> ' ' AND USERNAME = '");
76 aSql
+= ::rtl::OUString::createFromAscii("'");
78 Reference
< XResultSet
> xResult
= xStmt
->executeQuery(aSql
);
81 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
82 while(xResult
->next())
83 aVector
.push_back(xRow
->getString(1));
84 ::comphelper::disposeComponent(xResult
);
86 ::comphelper::disposeComponent(xStmt
);
89 m_pGroups
->reFill(aVector
);
91 m_pGroups
= new OGroups(*this,m_aMutex
,aVector
,m_pConnection
,this);
93 // -------------------------------------------------------------------------
94 OUserExtend::OUserExtend( OAdabasConnection
* _pConnection
) : OAdabasUser(_pConnection
)
98 // -------------------------------------------------------------------------
99 typedef connectivity::sdbcx::OUser OUser_TYPEDEF
;
100 void OUserExtend::construct()
103 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD
), PROPERTY_ID_PASSWORD
,0,&m_Password
,::getCppuType(reinterpret_cast< ::rtl::OUString
*>(NULL
)));
105 // -----------------------------------------------------------------------------
106 cppu::IPropertyArrayHelper
* OUserExtend::createArrayHelper() const
108 Sequence
< Property
> aProps
;
109 describeProperties(aProps
);
110 return new cppu::OPropertyArrayHelper(aProps
);
112 // -------------------------------------------------------------------------
113 cppu::IPropertyArrayHelper
& OUserExtend::getInfoHelper()
115 return *OUserExtend_PROP::getArrayHelper();
117 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER
;
118 // -----------------------------------------------------------------------------
119 sal_Int32 SAL_CALL
OAdabasUser::getPrivileges( const ::rtl::OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
)
121 if ( objType
!= PrivilegeObject::TABLE
)
124 ::osl::MutexGuard
aGuard(m_aMutex
);
125 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
127 sal_Int32 nRights
,nRightsWithGrant
;
128 getAnyTablePrivileges(objName
,nRights
,nRightsWithGrant
);
131 // -----------------------------------------------------------------------------
132 void OAdabasUser::getAnyTablePrivileges(const ::rtl::OUString
& objName
, sal_Int32
& nRights
,sal_Int32
& nRightsWithGrant
) throw(SQLException
, RuntimeException
)
134 nRightsWithGrant
= nRights
= 0;
135 // first we need to create the sql stmt to select the privs
136 Reference
<XDatabaseMetaData
> xMeta
= m_pConnection
->getMetaData();
137 ::rtl::OUString sCatalog
,sSchema
,sTable
;
138 ::dbtools::qualifiedNameComponents(xMeta
,objName
,sCatalog
,sSchema
,sTable
,::dbtools::eInDataManipulation
);
139 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
140 ::rtl::OUString sSql
= ::rtl::OUString::createFromAscii("SELECT REFTABLENAME,PRIVILEGES FROM DOMAIN.USR_USES_TAB WHERE REFOBJTYPE <> 'SYSTEM' AND DEFUSERNAME = '");
142 sSql
+= ::rtl::OUString::createFromAscii("' AND REFTABLENAME = '");
144 sSql
+= ::rtl::OUString::createFromAscii("'");
147 Reference
<XResultSet
> xRes
= xStmt
->executeQuery(sSql
);
150 Reference
<XRow
> xRow(xRes
,UNO_QUERY
);
151 if(xRow
.is() && xRes
->next())
153 ::rtl::OUString sPrivs
= xRow
->getString(2);
157 const sal_Char
* pAsciiName
;
158 sal_Int32 nNumericValue
;
161 { "INS", Privilege::INSERT
},
162 { "DEL", Privilege::DELETE
},
163 { "UPD", Privilege::UPDATE
},
164 { "ALT", Privilege::ALTER
},
165 { "SEL", Privilege::SELECT
},
166 { "REF", Privilege::REFERENCE
}
168 for ( size_t i
= 0; i
< sizeof( privileges
) / sizeof( privileges
[0] ); ++i
)
170 sal_Int32 nIndex
= sPrivs
.indexOf( ::rtl::OUString::createFromAscii( privileges
[i
].pAsciiName
) );
174 nRights
|= privileges
[i
].nNumericValue
;
175 if ( sPrivs
.copy( nIndex
+ 2, 1 ).equalsAscii( "+" ) )
176 nRightsWithGrant
|= privileges
[i
].nNumericValue
;
179 ::comphelper::disposeComponent(xRes
);
181 ::comphelper::disposeComponent(xStmt
);
184 // -------------------------------------------------------------------------
185 sal_Int32 SAL_CALL
OAdabasUser::getGrantablePrivileges( const ::rtl::OUString
& objName
, sal_Int32 objType
) throw(SQLException
, RuntimeException
)
187 if ( objType
!= PrivilegeObject::TABLE
)
190 ::osl::MutexGuard
aGuard(m_aMutex
);
191 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
193 sal_Int32 nRights
,nRightsWithGrant
;
194 getAnyTablePrivileges(objName
,nRights
,nRightsWithGrant
);
195 return nRightsWithGrant
;
197 // -------------------------------------------------------------------------
198 void SAL_CALL
OAdabasUser::grantPrivileges( const ::rtl::OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
)
200 if ( objType
!= PrivilegeObject::TABLE
)
201 m_pConnection
->throwGenericSQLException(STR_PRIVILEGE_NOT_GRANTED
,*this);
203 ::osl::MutexGuard
aGuard(m_aMutex
);
204 ::rtl::OUString sPrivs
= getPrivilegeString(objPrivileges
);
205 if(sPrivs
.getLength())
207 ::rtl::OUString sGrant
;
208 sGrant
+= ::rtl::OUString::createFromAscii("GRANT ");
210 sGrant
+= ::rtl::OUString::createFromAscii(" ON ");
211 Reference
<XDatabaseMetaData
> xMeta
= m_pConnection
->getMetaData();
212 sGrant
+= ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
);
213 sGrant
+= ::rtl::OUString::createFromAscii(" TO ");
216 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
218 xStmt
->execute(sGrant
);
219 ::comphelper::disposeComponent(xStmt
);
222 // -------------------------------------------------------------------------
223 void SAL_CALL
OAdabasUser::revokePrivileges( const ::rtl::OUString
& objName
, sal_Int32 objType
, sal_Int32 objPrivileges
) throw(SQLException
, RuntimeException
)
225 if ( objType
!= PrivilegeObject::TABLE
)
226 m_pConnection
->throwGenericSQLException(STR_PRIVILEGE_NOT_REVOKED
,*this);
228 ::osl::MutexGuard
aGuard(m_aMutex
);
229 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
230 ::rtl::OUString sPrivs
= getPrivilegeString(objPrivileges
);
231 if(sPrivs
.getLength())
233 ::rtl::OUString sGrant
;
234 sGrant
+= ::rtl::OUString::createFromAscii("REVOKE ");
236 sGrant
+= ::rtl::OUString::createFromAscii(" ON ");
237 Reference
<XDatabaseMetaData
> xMeta
= m_pConnection
->getMetaData();
238 sGrant
+= ::dbtools::quoteTableName(xMeta
,objName
,::dbtools::eInDataManipulation
);
239 sGrant
+= ::rtl::OUString::createFromAscii(" FROM ");
242 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
244 xStmt
->execute(sGrant
);
245 ::comphelper::disposeComponent(xStmt
);
248 // -----------------------------------------------------------------------------
250 void SAL_CALL
OAdabasUser::changePassword( const ::rtl::OUString
& objPassword
, const ::rtl::OUString
& newPassword
) throw(SQLException
, RuntimeException
)
252 ::osl::MutexGuard
aGuard(m_aMutex
);
253 checkDisposed(OUser_BASE_RBHELPER::rBHelper
.bDisposed
);
254 ::rtl::OUString sAlterPwd
;
255 sAlterPwd
= ::rtl::OUString::createFromAscii("ALTER PASSWORD \"");
256 sAlterPwd
+= objPassword
.toAsciiUpperCase();
257 sAlterPwd
+= ::rtl::OUString::createFromAscii("\" TO \"") ;
258 sAlterPwd
+= newPassword
.toAsciiUpperCase();
259 sAlterPwd
+= ::rtl::OUString::createFromAscii("\"") ;
261 sal_Bool bDisposeConnection
= sal_False
;
262 Reference
<XConnection
> xConnection
= m_pConnection
;
263 if(m_pConnection
->getMetaData()->getUserName() != m_Name
)
265 OAdabasConnection
* pNewConnection
= new OAdabasConnection(m_pConnection
->getDriverHandle(),m_pConnection
->getDriver());
266 xConnection
= pNewConnection
;
269 Sequence
< PropertyValue
> aSeq(2);
270 aSeq
.getArray()[0].Name
= ::rtl::OUString::createFromAscii("user") ;
271 aSeq
.getArray()[0].Value
<<= m_Name
;
272 aSeq
.getArray()[1].Name
= ::rtl::OUString::createFromAscii("password") ;
273 aSeq
.getArray()[1].Value
<<= objPassword
;
274 pNewConnection
->Construct(m_pConnection
->getMetaData()->getURL(),aSeq
);
276 bDisposeConnection
= sal_True
;
280 Reference
<XStatement
> xStmt
= xConnection
->createStatement();
282 xStmt
->execute(sAlterPwd
);
283 ::comphelper::disposeComponent(xStmt
);
284 if(bDisposeConnection
)
285 ::comphelper::disposeComponent(xConnection
);
288 ::dbtools::throwFunctionSequenceException(*this);
290 // -----------------------------------------------------------------------------
291 ::rtl::OUString
OAdabasUser::getPrivilegeString(sal_Int32 nRights
) const
293 ::rtl::OUString sPrivs
;
294 if((nRights
& Privilege::INSERT
) == Privilege::INSERT
)
295 sPrivs
+= ::rtl::OUString::createFromAscii("INSERT");
297 if((nRights
& Privilege::DELETE
) == Privilege::DELETE
)
299 if(sPrivs
.getLength())
300 sPrivs
+= ::rtl::OUString::createFromAscii(",");
301 sPrivs
+= ::rtl::OUString::createFromAscii("DELETE");
304 if((nRights
& Privilege::UPDATE
) == Privilege::UPDATE
)
306 if(sPrivs
.getLength())
307 sPrivs
+= ::rtl::OUString::createFromAscii(",");
308 sPrivs
+= ::rtl::OUString::createFromAscii("UPDATE");
311 if((nRights
& Privilege::ALTER
) == Privilege::ALTER
)
313 if(sPrivs
.getLength())
314 sPrivs
+= ::rtl::OUString::createFromAscii(",");
315 sPrivs
+= ::rtl::OUString::createFromAscii("ALTER");
318 if((nRights
& Privilege::SELECT
) == Privilege::SELECT
)
320 if(sPrivs
.getLength())
321 sPrivs
+= ::rtl::OUString::createFromAscii(",");
322 sPrivs
+= ::rtl::OUString::createFromAscii("SELECT");
325 if((nRights
& Privilege::REFERENCE
) == Privilege::REFERENCE
)
327 if(sPrivs
.getLength())
328 sPrivs
+= ::rtl::OUString::createFromAscii(",");
329 sPrivs
+= ::rtl::OUString::createFromAscii("REFERENCES");
334 // -----------------------------------------------------------------------------