Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "TableGrantCtrl.hxx"
21 : #include <tools/debug.hxx>
22 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
23 : #include <com/sun/star/sdbcx/Privilege.hpp>
24 : #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
25 : #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
26 : #include <com/sun/star/sdbcx/XAuthorizable.hpp>
27 : #include "dbu_control.hrc"
28 : #include "UITools.hxx"
29 :
30 : using namespace ::com::sun::star::accessibility;
31 : using namespace ::com::sun::star::container;
32 : using namespace ::com::sun::star::sdbcx;
33 : using namespace ::com::sun::star::sdbc;
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::dbaui;
36 : using namespace ::svt;
37 :
38 : const sal_uInt16 COL_TABLE_NAME = 1;
39 : const sal_uInt16 COL_SELECT = 2;
40 : const sal_uInt16 COL_INSERT = 3;
41 : const sal_uInt16 COL_DELETE = 4;
42 : const sal_uInt16 COL_UPDATE = 5;
43 : const sal_uInt16 COL_ALTER = 6;
44 : const sal_uInt16 COL_REF = 7;
45 : const sal_uInt16 COL_DROP = 8;
46 :
47 : DBG_NAME(OTableGrantControl)
48 :
49 : //================================================================================
50 : // OTableGrantControl
51 : //================================================================================
52 0 : OTableGrantControl::OTableGrantControl( Window* pParent,const ResId& _RsId)
53 : :EditBrowseBox( pParent,_RsId, EBBF_SMART_TAB_TRAVEL | EBBF_NOROWPICTURE )
54 : ,m_pCheckCell( NULL )
55 : ,m_pEdit( NULL )
56 : ,m_nDataPos( 0 )
57 0 : ,m_nDeactivateEvent(0)
58 : {
59 : DBG_CTOR(OTableGrantControl,NULL);
60 : //////////////////////////////////////////////////////////////////////
61 : // Spalten einfuegen
62 0 : sal_uInt16 i=1;
63 0 : InsertDataColumn( i, String(ModuleRes(STR_TABLE_PRIV_NAME) ), 75);
64 0 : FreezeColumn(i++);
65 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_SELECT)), 75);
66 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_INSERT)), 75);
67 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DELETE)), 75);
68 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_UPDATE)), 75);
69 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_ALTER)), 75);
70 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE)), 75);
71 0 : InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DROP)), 75);
72 :
73 0 : while(--i)
74 0 : SetColumnWidth(i,GetAutoColumnWidth(i));
75 0 : }
76 :
77 : //------------------------------------------------------------------------
78 0 : OTableGrantControl::~OTableGrantControl()
79 : {
80 : DBG_DTOR(OTableGrantControl,NULL);
81 0 : if (m_nDeactivateEvent)
82 : {
83 0 : Application::RemoveUserEvent(m_nDeactivateEvent);
84 0 : m_nDeactivateEvent = 0;
85 : }
86 :
87 0 : delete m_pCheckCell;
88 0 : delete m_pEdit;
89 :
90 0 : m_xTables = NULL;
91 0 : }
92 : // -----------------------------------------------------------------------------
93 0 : void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
94 : {
95 : // first we need the users
96 0 : Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
97 0 : if(xUserSup.is())
98 0 : m_xUsers = xUserSup->getUsers();
99 :
100 : // second we need the tables to determine which privileges the user has
101 0 : if(_xTablesSup.is())
102 0 : m_xTables = _xTablesSup->getTables();
103 :
104 0 : if(m_xTables.is())
105 0 : m_aTableNames = m_xTables->getElementNames();
106 :
107 : OSL_ENSURE(m_xUsers.is(),"No user access supported!");
108 0 : OSL_ENSURE(m_xTables.is(),"No tables supported!");
109 0 : }
110 : // -----------------------------------------------------------------------------
111 0 : void OTableGrantControl::setComponentContext(const Reference< ::com::sun::star::uno::XComponentContext>& _rxContext)
112 : {
113 0 : m_xContext = _rxContext;
114 0 : }
115 : //------------------------------------------------------------------------
116 0 : void OTableGrantControl::UpdateTables()
117 : {
118 0 : RemoveRows();
119 :
120 0 : if(m_xTables.is())
121 0 : RowInserted(0, m_aTableNames.getLength());
122 : // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
123 0 : }
124 : //------------------------------------------------------------------------
125 0 : void OTableGrantControl::Init()
126 : {
127 : DBG_CHKTHIS(OTableGrantControl,NULL);
128 0 : EditBrowseBox::Init();
129 :
130 : //////////////////////////////////////////////////////////////////////
131 : // ComboBox instanzieren
132 0 : if(!m_pCheckCell)
133 : {
134 0 : m_pCheckCell = new CheckBoxControl( &GetDataWindow() );
135 0 : m_pCheckCell->GetBox().EnableTriState(sal_False);
136 :
137 0 : m_pEdit = new Edit( &GetDataWindow() );
138 0 : m_pEdit->SetReadOnly();
139 0 : m_pEdit->Enable(sal_False);
140 : }
141 :
142 0 : UpdateTables();
143 : //////////////////////////////////////////////////////////////////////
144 : // Browser Mode setzen
145 : BrowserMode nMode = BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL |
146 0 : BROWSER_HIDECURSOR | BROWSER_HIDESELECT;
147 :
148 0 : SetMode(nMode);
149 0 : }
150 :
151 : //------------------------------------------------------------------------------
152 0 : void OTableGrantControl::Resize()
153 : {
154 : DBG_CHKTHIS(OTableGrantControl,NULL);
155 0 : EditBrowseBox::Resize();
156 0 : }
157 :
158 : //------------------------------------------------------------------------------
159 0 : long OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
160 : {
161 : DBG_CHKTHIS(OTableGrantControl,NULL);
162 0 : if (rNEvt.GetType() == EVENT_LOSEFOCUS)
163 0 : if (!HasChildPathFocus())
164 : {
165 0 : if (m_nDeactivateEvent)
166 0 : Application::RemoveUserEvent(m_nDeactivateEvent);
167 0 : m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate));
168 : }
169 0 : if (rNEvt.GetType() == EVENT_GETFOCUS)
170 : {
171 0 : if (m_nDeactivateEvent)
172 0 : Application::RemoveUserEvent(m_nDeactivateEvent);
173 0 : m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate));
174 : }
175 0 : return EditBrowseBox::PreNotify(rNEvt);
176 : }
177 :
178 : //------------------------------------------------------------------------------
179 0 : IMPL_LINK_NOARG(OTableGrantControl, AsynchActivate)
180 : {
181 0 : m_nDeactivateEvent = 0;
182 0 : ActivateCell();
183 0 : return 0L;
184 : }
185 :
186 : //------------------------------------------------------------------------------
187 0 : IMPL_LINK_NOARG(OTableGrantControl, AsynchDeactivate)
188 : {
189 0 : m_nDeactivateEvent = 0;
190 0 : DeactivateCell();
191 0 : return 0L;
192 : }
193 :
194 : //------------------------------------------------------------------------------
195 0 : sal_Bool OTableGrantControl::IsTabAllowed(sal_Bool bForward) const
196 : {
197 : DBG_CHKTHIS(OTableGrantControl,NULL);
198 0 : long nRow = GetCurRow();
199 0 : sal_uInt16 nCol = GetCurColumnId();
200 :
201 0 : if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
202 0 : return sal_False;
203 :
204 0 : if (!bForward && (nCol == 1) && (nRow == 0))
205 0 : return sal_False;
206 :
207 0 : return EditBrowseBox::IsTabAllowed(bForward);
208 : }
209 : //------------------------------------------------------------------------------
210 : #define GRANT_REVOKE_RIGHT(what) \
211 : if(m_pCheckCell->GetBox().IsChecked()) \
212 : xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
213 : else \
214 : xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
215 :
216 : //------------------------------------------------------------------------------
217 0 : sal_Bool OTableGrantControl::SaveModified()
218 : {
219 : DBG_CHKTHIS(OTableGrantControl,NULL);
220 :
221 0 : sal_Int32 nRow = GetCurRow();
222 0 : if(nRow == -1 || nRow >= m_aTableNames.getLength())
223 0 : return sal_False;
224 :
225 0 : ::rtl::OUString sTableName = m_aTableNames[nRow];
226 0 : sal_Bool bErg = sal_True;
227 : try
228 : {
229 :
230 0 : if ( m_xUsers->hasByName(m_sUserName) )
231 : {
232 0 : Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
233 0 : if ( xAuth.is() )
234 : {
235 0 : switch( GetCurColumnId() )
236 : {
237 : case COL_INSERT:
238 0 : GRANT_REVOKE_RIGHT(Privilege::INSERT);
239 0 : break;
240 : case COL_DELETE:
241 0 : GRANT_REVOKE_RIGHT(Privilege::DELETE);
242 0 : break;
243 : case COL_UPDATE:
244 0 : GRANT_REVOKE_RIGHT(Privilege::UPDATE);
245 0 : break;
246 : case COL_ALTER:
247 0 : GRANT_REVOKE_RIGHT(Privilege::ALTER);
248 0 : break;
249 : case COL_SELECT:
250 0 : GRANT_REVOKE_RIGHT(Privilege::SELECT);
251 0 : break;
252 : case COL_REF:
253 0 : GRANT_REVOKE_RIGHT(Privilege::REFERENCE);
254 0 : break;
255 : case COL_DROP:
256 0 : GRANT_REVOKE_RIGHT(Privilege::DROP);
257 0 : break;
258 : }
259 0 : fillPrivilege(nRow);
260 0 : }
261 : }
262 : }
263 0 : catch(SQLException& e)
264 : {
265 0 : bErg = sal_False;
266 0 : ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xContext);
267 : }
268 0 : if(bErg && Controller().Is())
269 0 : Controller()->ClearModified();
270 0 : if(!bErg)
271 0 : UpdateTables();
272 :
273 0 : return bErg;
274 : }
275 :
276 : //------------------------------------------------------------------------------
277 0 : String OTableGrantControl::GetCellText( long nRow, sal_uInt16 nColId ) const
278 : {
279 : DBG_CHKTHIS(OTableGrantControl,NULL);
280 0 : if(COL_TABLE_NAME == nColId)
281 0 : return m_aTableNames[nRow];
282 :
283 0 : sal_Int32 nPriv = 0;
284 0 : TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
285 0 : if(aFind != m_aPrivMap.end())
286 0 : nPriv = aFind->second.nRights;
287 :
288 0 : return String::CreateFromInt32(isAllowed(nColId,nPriv) ? 1 :0);
289 : }
290 :
291 : //------------------------------------------------------------------------------
292 0 : void OTableGrantControl::InitController( CellControllerRef& /*rController*/, long nRow, sal_uInt16 nColumnId )
293 : {
294 : DBG_CHKTHIS(OTableGrantControl,NULL);
295 0 : String sTablename = m_aTableNames[nRow];
296 : // special case for tablename
297 0 : if(nColumnId == COL_TABLE_NAME)
298 0 : m_pEdit->SetText(sTablename);
299 : else
300 : {
301 : // get the privileges from the user
302 0 : TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
303 0 : m_pCheckCell->GetBox().Check(aFind != m_aPrivMap.end() ? isAllowed(nColumnId,aFind->second.nRights) : sal_False);
304 0 : }
305 0 : }
306 : // -----------------------------------------------------------------------------
307 0 : void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
308 : {
309 :
310 0 : if ( m_xUsers->hasByName(m_sUserName) )
311 : {
312 : try
313 : {
314 0 : Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
315 0 : if ( xAuth.is() )
316 : {
317 : // get the privileges
318 : TPrivileges nRights;
319 0 : nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
320 0 : if(m_xGrantUser.is())
321 0 : nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
322 : else
323 0 : nRights.nWithGrant = 0;
324 :
325 0 : m_aPrivMap[m_aTableNames[_nRow]] = nRights;
326 0 : }
327 : }
328 0 : catch(SQLException& e)
329 : {
330 0 : ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xContext);
331 : }
332 0 : catch(Exception& )
333 : {
334 : }
335 : }
336 0 : }
337 : // -----------------------------------------------------------------------------
338 0 : sal_Bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege) const
339 : {
340 0 : sal_Bool bAllowed = sal_False;
341 0 : switch (_nColumnId)
342 : {
343 : case COL_INSERT:
344 0 : bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
345 0 : break;
346 : case COL_DELETE:
347 0 : bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
348 0 : break;
349 : case COL_UPDATE:
350 0 : bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
351 0 : break;
352 : case COL_ALTER:
353 0 : bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
354 0 : break;
355 : case COL_SELECT:
356 0 : bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
357 0 : break;
358 : case COL_REF:
359 0 : bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
360 0 : break;
361 : case COL_DROP:
362 0 : bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
363 0 : break;
364 : }
365 0 : return bAllowed;
366 : }
367 : // -----------------------------------------------------------------------------
368 0 : void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName)
369 : {
370 0 : m_sUserName = _sUserName;
371 0 : m_aPrivMap = TTablePrivilegeMap();
372 0 : }
373 : // -----------------------------------------------------------------------------
374 0 : void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
375 : {
376 : OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
377 0 : m_xGrantUser = _xGrantUser;
378 0 : }
379 : //------------------------------------------------------------------------------
380 0 : CellController* OTableGrantControl::GetController( long nRow, sal_uInt16 nColumnId )
381 : {
382 : DBG_CHKTHIS(OTableGrantControl,NULL);
383 :
384 0 : CellController* pController = NULL;
385 0 : switch( nColumnId )
386 : {
387 : case COL_TABLE_NAME:
388 0 : break;
389 : case COL_INSERT:
390 : case COL_DELETE:
391 : case COL_UPDATE:
392 : case COL_ALTER:
393 : case COL_SELECT:
394 : case COL_REF:
395 : case COL_DROP:
396 : {
397 0 : TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
398 0 : if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
399 0 : pController = new CheckBoxCellController( m_pCheckCell );
400 : }
401 0 : break;
402 : default:
403 : ;
404 : }
405 0 : return pController;
406 : }
407 : //------------------------------------------------------------------------------
408 0 : sal_Bool OTableGrantControl::SeekRow( long nRow )
409 : {
410 : DBG_CHKTHIS(OTableGrantControl,NULL);
411 0 : m_nDataPos = nRow;
412 :
413 0 : return (nRow <= m_aTableNames.getLength());
414 : }
415 :
416 : //------------------------------------------------------------------------------
417 0 : void OTableGrantControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColumnId ) const
418 : {
419 : DBG_CHKTHIS(OTableGrantControl,NULL);
420 :
421 0 : if(nColumnId != COL_TABLE_NAME)
422 : {
423 0 : TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
424 0 : if(aFind != m_aPrivMap.end())
425 0 : PaintTristate(rDev, rRect, isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK,isAllowed(nColumnId,aFind->second.nWithGrant));
426 : else
427 0 : PaintTristate(rDev, rRect, STATE_NOCHECK,sal_False);
428 : }
429 : else
430 : {
431 0 : String aText(((OTableGrantControl*)this)->GetCellText( m_nDataPos, nColumnId ));
432 0 : Point aPos( rRect.TopLeft() );
433 0 : sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
434 0 : sal_Int32 nHeight = GetDataWindow().GetTextHeight();
435 :
436 0 : if( aPos.X() < rRect.Right() || aPos.X() + nWidth > rRect.Right() ||
437 0 : aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
438 0 : rDev.SetClipRegion( rRect );
439 :
440 0 : rDev.DrawText( aPos, aText );
441 : }
442 :
443 0 : if( rDev.IsClipRegion() )
444 0 : rDev.SetClipRegion();
445 0 : }
446 :
447 : //------------------------------------------------------------------------
448 0 : void OTableGrantControl::CellModified()
449 : {
450 : DBG_CHKTHIS(OTableGrantControl,NULL);
451 0 : EditBrowseBox::CellModified();
452 0 : SaveModified();
453 0 : }
454 : // -----------------------------------------------------------------------------
455 0 : OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
456 : {
457 0 : TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
458 0 : if(aFind == m_aPrivMap.end())
459 : {
460 0 : fillPrivilege(_nRow);
461 0 : aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
462 : }
463 0 : return aFind;
464 : }
465 : // -----------------------------------------------------------------------------
466 0 : Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
467 : {
468 0 : sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
469 0 : if(nColumnId != COL_TABLE_NAME)
470 : {
471 0 : TriState eState = STATE_NOCHECK;
472 0 : TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
473 0 : if(aFind != m_aPrivMap.end())
474 : {
475 0 : eState = isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK;
476 : }
477 : else
478 0 : eState = STATE_NOCHECK;
479 :
480 0 : return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState );
481 : }
482 0 : return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
483 : }
484 : // -----------------------------------------------------------------------------
485 :
486 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|