xref: /AOO41X/main/connectivity/source/drivers/adabas/BUser.cxx (revision 9b5730f6ddef7eb82608ca4d31dc0d7678e652cf)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "adabas/BUser.hxx"
27 #include "adabas/BGroups.hxx"
28 #include <com/sun/star/sdbc/XRow.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include "adabas/BConnection.hxx"
31 #include "connectivity/dbtools.hxx"
32 #include "connectivity/dbexception.hxx"
33 #include <com/sun/star/sdbcx/Privilege.hpp>
34 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
35 #include "resource/adabas_res.hrc"
36 
37 using namespace connectivity::adabas;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::sdbcx;
41 using namespace ::com::sun::star::sdbc;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::lang;
44 // -------------------------------------------------------------------------
OAdabasUser(OAdabasConnection * _pConnection)45 OAdabasUser::OAdabasUser(   OAdabasConnection* _pConnection) : connectivity::sdbcx::OUser(sal_True)
46                 ,m_pConnection(_pConnection)
47 {
48     construct();
49 }
50 // -------------------------------------------------------------------------
OAdabasUser(OAdabasConnection * _pConnection,const::rtl::OUString & _Name)51 OAdabasUser::OAdabasUser(   OAdabasConnection* _pConnection,
52                 const ::rtl::OUString& _Name
53             ) : connectivity::sdbcx::OUser(_Name,sal_True)
54                 ,m_pConnection(_pConnection)
55 {
56     construct();
57 }
58 // -------------------------------------------------------------------------
refreshGroups()59 void OAdabasUser::refreshGroups()
60 {
61     if(!m_pConnection)
62         return;
63 
64     TStringVector aVector;
65     aVector.reserve(7); // we don't know the excatly count of users but this should fit the normal need
66     Reference< XStatement > xStmt = m_pConnection->createStatement(  );
67     ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("SELECT DISTINCT GROUPNAME FROM DOMAIN.USERS WHERE GROUPNAME IS NOT NULL AND GROUPNAME <> ' ' AND USERNAME = '");
68     aSql += getName( );
69     aSql += ::rtl::OUString::createFromAscii("'");
70 
71     Reference< XResultSet > xResult = xStmt->executeQuery(aSql);
72     if(xResult.is())
73     {
74                 Reference< XRow > xRow(xResult,UNO_QUERY);
75         while(xResult->next())
76             aVector.push_back(xRow->getString(1));
77         ::comphelper::disposeComponent(xResult);
78     }
79     ::comphelper::disposeComponent(xStmt);
80 
81     if(m_pGroups)
82         m_pGroups->reFill(aVector);
83     else
84         m_pGroups = new OGroups(*this,m_aMutex,aVector,m_pConnection,this);
85 }
86 // -------------------------------------------------------------------------
OUserExtend(OAdabasConnection * _pConnection)87 OUserExtend::OUserExtend(   OAdabasConnection* _pConnection) : OAdabasUser(_pConnection)
88 {
89     construct();
90 }
91 // -------------------------------------------------------------------------
92 typedef connectivity::sdbcx::OUser  OUser_TYPEDEF;
construct()93 void OUserExtend::construct()
94 {
95 
96     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),    PROPERTY_ID_PASSWORD,0,&m_Password,::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
97 }
98 // -----------------------------------------------------------------------------
createArrayHelper() const99 cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
100 {
101     Sequence< Property > aProps;
102     describeProperties(aProps);
103     return new cppu::OPropertyArrayHelper(aProps);
104 }
105 // -------------------------------------------------------------------------
getInfoHelper()106 cppu::IPropertyArrayHelper & OUserExtend::getInfoHelper()
107 {
108     return *OUserExtend_PROP::getArrayHelper();
109 }
110 typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;
111 // -----------------------------------------------------------------------------
getPrivileges(const::rtl::OUString & objName,sal_Int32 objType)112 sal_Int32 SAL_CALL OAdabasUser::getPrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
113 {
114     if ( objType != PrivilegeObject::TABLE )
115         return 0;
116 
117     ::osl::MutexGuard aGuard(m_aMutex);
118     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
119 
120     sal_Int32 nRights,nRightsWithGrant;
121     getAnyTablePrivileges(objName,nRights,nRightsWithGrant);
122     return nRights;
123 }
124 // -----------------------------------------------------------------------------
getAnyTablePrivileges(const::rtl::OUString & objName,sal_Int32 & nRights,sal_Int32 & nRightsWithGrant)125 void OAdabasUser::getAnyTablePrivileges(const ::rtl::OUString& objName, sal_Int32& nRights,sal_Int32& nRightsWithGrant) throw(SQLException, RuntimeException)
126 {
127     nRightsWithGrant = nRights = 0;
128     // first we need to create the sql stmt to select the privs
129     Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
130     ::rtl::OUString sCatalog,sSchema,sTable;
131     ::dbtools::qualifiedNameComponents(xMeta,objName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
132     Reference<XStatement> xStmt = m_pConnection->createStatement();
133     ::rtl::OUString sSql = ::rtl::OUString::createFromAscii("SELECT REFTABLENAME,PRIVILEGES FROM DOMAIN.USR_USES_TAB WHERE REFOBJTYPE <> 'SYSTEM' AND DEFUSERNAME = '");
134     sSql += m_Name;
135     sSql += ::rtl::OUString::createFromAscii("' AND REFTABLENAME = '");
136     sSql += sTable;
137     sSql += ::rtl::OUString::createFromAscii("'");
138     if(xStmt.is())
139     {
140         Reference<XResultSet> xRes = xStmt->executeQuery(sSql);
141         if(xRes.is())
142         {
143             Reference<XRow> xRow(xRes,UNO_QUERY);
144             if(xRow.is() && xRes->next())
145             {
146                 ::rtl::OUString sPrivs = xRow->getString(2);
147 
148                 struct _priv_nam
149                 {
150                     const sal_Char* pAsciiName;
151                     sal_Int32       nNumericValue;
152                 } privileges[] =
153                 {
154                     { "INS", Privilege::INSERT },
155                     { "DEL", Privilege::DELETE },
156                     { "UPD", Privilege::UPDATE },
157                     { "ALT", Privilege::ALTER },
158                     { "SEL", Privilege::SELECT },
159                     { "REF", Privilege::REFERENCE }
160                 };
161                 for ( size_t i = 0; i < sizeof( privileges ) / sizeof( privileges[0] ); ++i )
162                 {
163                     sal_Int32 nIndex = sPrivs.indexOf( ::rtl::OUString::createFromAscii( privileges[i].pAsciiName ) );
164                     if ( nIndex == -1 )
165                         continue;
166 
167                     nRights |= privileges[i].nNumericValue;
168                     if ( sPrivs.copy( nIndex + 2, 1 ).equalsAscii( "+" ) )
169                         nRightsWithGrant |= privileges[i].nNumericValue;
170                 }
171             }
172             ::comphelper::disposeComponent(xRes);
173         }
174         ::comphelper::disposeComponent(xStmt);
175     }
176 }
177 // -------------------------------------------------------------------------
getGrantablePrivileges(const::rtl::OUString & objName,sal_Int32 objType)178 sal_Int32 SAL_CALL OAdabasUser::getGrantablePrivileges( const ::rtl::OUString& objName, sal_Int32 objType ) throw(SQLException, RuntimeException)
179 {
180     if ( objType != PrivilegeObject::TABLE )
181         return 0;
182 
183     ::osl::MutexGuard aGuard(m_aMutex);
184     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
185 
186     sal_Int32 nRights,nRightsWithGrant;
187     getAnyTablePrivileges(objName,nRights,nRightsWithGrant);
188     return nRightsWithGrant;
189 }
190 // -------------------------------------------------------------------------
grantPrivileges(const::rtl::OUString & objName,sal_Int32 objType,sal_Int32 objPrivileges)191 void SAL_CALL OAdabasUser::grantPrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
192 {
193     if ( objType != PrivilegeObject::TABLE )
194         m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_GRANTED,*this);
195 
196     ::osl::MutexGuard aGuard(m_aMutex);
197     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
198     if(sPrivs.getLength())
199     {
200         ::rtl::OUString sGrant;
201         sGrant += ::rtl::OUString::createFromAscii("GRANT ");
202         sGrant += sPrivs;
203         sGrant += ::rtl::OUString::createFromAscii(" ON ");
204         Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
205         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
206         sGrant += ::rtl::OUString::createFromAscii(" TO ");
207         sGrant += m_Name;
208 
209         Reference<XStatement> xStmt = m_pConnection->createStatement();
210         if(xStmt.is())
211             xStmt->execute(sGrant);
212         ::comphelper::disposeComponent(xStmt);
213     }
214 }
215 // -------------------------------------------------------------------------
revokePrivileges(const::rtl::OUString & objName,sal_Int32 objType,sal_Int32 objPrivileges)216 void SAL_CALL OAdabasUser::revokePrivileges( const ::rtl::OUString& objName, sal_Int32 objType, sal_Int32 objPrivileges ) throw(SQLException, RuntimeException)
217 {
218     if ( objType != PrivilegeObject::TABLE )
219         m_pConnection->throwGenericSQLException(STR_PRIVILEGE_NOT_REVOKED,*this);
220 
221     ::osl::MutexGuard aGuard(m_aMutex);
222     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
223     ::rtl::OUString sPrivs = getPrivilegeString(objPrivileges);
224     if(sPrivs.getLength())
225     {
226         ::rtl::OUString sGrant;
227         sGrant += ::rtl::OUString::createFromAscii("REVOKE ");
228         sGrant += sPrivs;
229         sGrant += ::rtl::OUString::createFromAscii(" ON ");
230         Reference<XDatabaseMetaData> xMeta = m_pConnection->getMetaData();
231         sGrant += ::dbtools::quoteTableName(xMeta,objName,::dbtools::eInDataManipulation);
232         sGrant += ::rtl::OUString::createFromAscii(" FROM ");
233         sGrant += m_Name;
234 
235         Reference<XStatement> xStmt = m_pConnection->createStatement();
236         if(xStmt.is())
237             xStmt->execute(sGrant);
238         ::comphelper::disposeComponent(xStmt);
239     }
240 }
241 // -----------------------------------------------------------------------------
242 // XUser
changePassword(const::rtl::OUString & objPassword,const::rtl::OUString & newPassword)243 void SAL_CALL OAdabasUser::changePassword( const ::rtl::OUString& objPassword, const ::rtl::OUString& newPassword ) throw(SQLException, RuntimeException)
244 {
245     ::osl::MutexGuard aGuard(m_aMutex);
246     checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);
247     ::rtl::OUString sAlterPwd;
248     sAlterPwd = ::rtl::OUString::createFromAscii("ALTER PASSWORD \"");
249     sAlterPwd += objPassword.toAsciiUpperCase();
250     sAlterPwd += ::rtl::OUString::createFromAscii("\" TO \"") ;
251     sAlterPwd += newPassword.toAsciiUpperCase();
252     sAlterPwd += ::rtl::OUString::createFromAscii("\"") ;
253 
254     sal_Bool bDisposeConnection = sal_False;
255     Reference<XConnection> xConnection = m_pConnection;
256     if(m_pConnection->getMetaData()->getUserName() != m_Name)
257     {
258         OAdabasConnection* pNewConnection = new OAdabasConnection(m_pConnection->getDriverHandle(),m_pConnection->getDriver());
259         xConnection = pNewConnection;
260         if(pNewConnection)
261         {
262             Sequence< PropertyValue> aSeq(2);
263             aSeq.getArray()[0].Name     = ::rtl::OUString::createFromAscii("user") ;
264             aSeq.getArray()[0].Value    <<= m_Name;
265             aSeq.getArray()[1].Name     = ::rtl::OUString::createFromAscii("password") ;
266             aSeq.getArray()[1].Value    <<= objPassword;
267             pNewConnection->Construct(m_pConnection->getMetaData()->getURL(),aSeq);
268         }
269         bDisposeConnection = sal_True;
270     }
271     if(xConnection.is())
272     {
273         Reference<XStatement> xStmt = xConnection->createStatement();
274         if(xStmt.is())
275             xStmt->execute(sAlterPwd);
276         ::comphelper::disposeComponent(xStmt);
277         if(bDisposeConnection)
278             ::comphelper::disposeComponent(xConnection);
279     }
280     else
281         ::dbtools::throwFunctionSequenceException(*this);
282 }
283 // -----------------------------------------------------------------------------
getPrivilegeString(sal_Int32 nRights) const284 ::rtl::OUString OAdabasUser::getPrivilegeString(sal_Int32 nRights) const
285 {
286     ::rtl::OUString sPrivs;
287     if((nRights & Privilege::INSERT) == Privilege::INSERT)
288         sPrivs += ::rtl::OUString::createFromAscii("INSERT");
289 
290     if((nRights & Privilege::DELETE) == Privilege::DELETE)
291     {
292         if(sPrivs.getLength())
293             sPrivs += ::rtl::OUString::createFromAscii(",");
294         sPrivs += ::rtl::OUString::createFromAscii("DELETE");
295     }
296 
297     if((nRights & Privilege::UPDATE) == Privilege::UPDATE)
298     {
299         if(sPrivs.getLength())
300             sPrivs += ::rtl::OUString::createFromAscii(",");
301         sPrivs += ::rtl::OUString::createFromAscii("UPDATE");
302     }
303 
304     if((nRights & Privilege::ALTER) == Privilege::ALTER)
305     {
306         if(sPrivs.getLength())
307             sPrivs += ::rtl::OUString::createFromAscii(",");
308         sPrivs += ::rtl::OUString::createFromAscii("ALTER");
309     }
310 
311     if((nRights & Privilege::SELECT) == Privilege::SELECT)
312     {
313         if(sPrivs.getLength())
314             sPrivs += ::rtl::OUString::createFromAscii(",");
315         sPrivs += ::rtl::OUString::createFromAscii("SELECT");
316     }
317 
318     if((nRights & Privilege::REFERENCE) == Privilege::REFERENCE)
319     {
320         if(sPrivs.getLength())
321             sPrivs += ::rtl::OUString::createFromAscii(",");
322         sPrivs += ::rtl::OUString::createFromAscii("REFERENCES");
323     }
324 
325     return sPrivs;
326 }
327 // -----------------------------------------------------------------------------
328 
329