Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / control / TableGrantCtrl.cxx
blobc9589e0307020c06c0a37e5144323c5643d587a6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "TableGrantCtrl.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 #include <com/sun/star/sdbcx/Privilege.hpp>
33 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
34 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
35 #include <com/sun/star/sdbcx/XAuthorizable.hpp>
36 #include "dbu_control.hrc"
37 #include "UITools.hxx"
39 using namespace ::com::sun::star::accessibility;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::uno;
44 using namespace ::dbaui;
45 using namespace ::svt;
47 const sal_uInt16 COL_TABLE_NAME = 1;
48 const sal_uInt16 COL_SELECT = 2;
49 const sal_uInt16 COL_INSERT = 3;
50 const sal_uInt16 COL_DELETE = 4;
51 const sal_uInt16 COL_UPDATE = 5;
52 const sal_uInt16 COL_ALTER = 6;
53 const sal_uInt16 COL_REF = 7;
54 const sal_uInt16 COL_DROP = 8;
56 DBG_NAME(OTableGrantControl)
58 //================================================================================
59 // OTableGrantControl
60 //================================================================================
61 OTableGrantControl::OTableGrantControl( Window* pParent,const ResId& _RsId)
62 :EditBrowseBox( pParent,_RsId, EBBF_SMART_TAB_TRAVEL | EBBF_NOROWPICTURE )
63 ,m_pCheckCell( NULL )
64 ,m_pEdit( NULL )
65 ,m_nDataPos( 0 )
66 ,m_bEnable(sal_True)
67 ,m_nDeactivateEvent(0)
69 DBG_CTOR(OTableGrantControl,NULL);
70 //////////////////////////////////////////////////////////////////////
71 // Spalten einfuegen
72 sal_uInt16 i=1;
73 InsertDataColumn( i, String(ModuleRes(STR_TABLE_PRIV_NAME) ), 75);
74 FreezeColumn(i++);
75 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_SELECT)), 75);
76 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_INSERT)), 75);
77 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DELETE)), 75);
78 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_UPDATE)), 75);
79 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_ALTER)), 75);
80 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE)), 75);
81 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DROP)), 75);
83 while(--i)
84 SetColumnWidth(i,GetAutoColumnWidth(i));
87 //------------------------------------------------------------------------
88 OTableGrantControl::~OTableGrantControl()
90 DBG_DTOR(OTableGrantControl,NULL);
91 if (m_nDeactivateEvent)
93 Application::RemoveUserEvent(m_nDeactivateEvent);
94 m_nDeactivateEvent = 0;
97 delete m_pCheckCell;
98 delete m_pEdit;
100 m_xTables = NULL;
102 // -----------------------------------------------------------------------------
103 void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
105 // first we need the users
106 Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
107 if(xUserSup.is())
108 m_xUsers = xUserSup->getUsers();
110 // second we need the tables to determine which privileges the user has
111 if(_xTablesSup.is())
112 m_xTables = _xTablesSup->getTables();
114 if(m_xTables.is())
115 m_aTableNames = m_xTables->getElementNames();
117 OSL_ENSURE(m_xUsers.is(),"No user access supported!");
118 OSL_ENSURE(m_xTables.is(),"No tables supported!");
120 // -----------------------------------------------------------------------------
121 void OTableGrantControl::setORB(const Reference< ::com::sun::star::lang::XMultiServiceFactory>& _xORB)
123 m_xORB = _xORB;
125 //------------------------------------------------------------------------
126 void OTableGrantControl::UpdateTables()
128 RemoveRows();
130 if(m_xTables.is())
131 RowInserted(0, m_aTableNames.getLength());
132 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
134 //------------------------------------------------------------------------
135 void OTableGrantControl::Init()
137 DBG_CHKTHIS(OTableGrantControl,NULL);
138 EditBrowseBox::Init();
140 //////////////////////////////////////////////////////////////////////
141 // ComboBox instanzieren
142 if(!m_pCheckCell)
144 m_pCheckCell = new CheckBoxControl( &GetDataWindow() );
145 m_pCheckCell->GetBox().EnableTriState(sal_False);
147 m_pEdit = new Edit( &GetDataWindow() );
148 m_pEdit->SetReadOnly();
149 m_pEdit->Enable(sal_False);
152 UpdateTables();
153 //////////////////////////////////////////////////////////////////////
154 // Browser Mode setzen
155 BrowserMode nMode = BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL |
156 BROWSER_HIDECURSOR | BROWSER_HIDESELECT;
158 SetMode(nMode);
161 //------------------------------------------------------------------------------
162 void OTableGrantControl::Resize()
164 DBG_CHKTHIS(OTableGrantControl,NULL);
165 EditBrowseBox::Resize();
168 //------------------------------------------------------------------------------
169 long OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
171 DBG_CHKTHIS(OTableGrantControl,NULL);
172 if (rNEvt.GetType() == EVENT_LOSEFOCUS)
173 if (!HasChildPathFocus())
175 if (m_nDeactivateEvent)
176 Application::RemoveUserEvent(m_nDeactivateEvent);
177 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate));
179 if (rNEvt.GetType() == EVENT_GETFOCUS)
181 if (m_nDeactivateEvent)
182 Application::RemoveUserEvent(m_nDeactivateEvent);
183 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate));
185 return EditBrowseBox::PreNotify(rNEvt);
188 //------------------------------------------------------------------------------
189 IMPL_LINK_NOARG(OTableGrantControl, AsynchActivate)
191 m_nDeactivateEvent = 0;
192 ActivateCell();
193 return 0L;
196 //------------------------------------------------------------------------------
197 IMPL_LINK_NOARG(OTableGrantControl, AsynchDeactivate)
199 m_nDeactivateEvent = 0;
200 DeactivateCell();
201 return 0L;
204 //------------------------------------------------------------------------------
205 sal_Bool OTableGrantControl::IsTabAllowed(sal_Bool bForward) const
207 DBG_CHKTHIS(OTableGrantControl,NULL);
208 long nRow = GetCurRow();
209 sal_uInt16 nCol = GetCurColumnId();
211 if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
212 return sal_False;
214 if (!bForward && (nCol == 1) && (nRow == 0))
215 return sal_False;
217 return EditBrowseBox::IsTabAllowed(bForward);
219 //------------------------------------------------------------------------------
220 #define GRANT_REVOKE_RIGHT(what) \
221 if(m_pCheckCell->GetBox().IsChecked()) \
222 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
223 else \
224 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
226 //------------------------------------------------------------------------------
227 sal_Bool OTableGrantControl::SaveModified()
229 DBG_CHKTHIS(OTableGrantControl,NULL);
231 sal_Int32 nRow = GetCurRow();
232 if(nRow == -1 || nRow >= m_aTableNames.getLength())
233 return sal_False;
235 ::rtl::OUString sTableName = m_aTableNames[nRow];
236 sal_Bool bErg = sal_True;
240 if ( m_xUsers->hasByName(m_sUserName) )
242 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
243 if ( xAuth.is() )
245 switch( GetCurColumnId() )
247 case COL_INSERT:
248 GRANT_REVOKE_RIGHT(Privilege::INSERT);
249 break;
250 case COL_DELETE:
251 GRANT_REVOKE_RIGHT(Privilege::DELETE);
252 break;
253 case COL_UPDATE:
254 GRANT_REVOKE_RIGHT(Privilege::UPDATE);
255 break;
256 case COL_ALTER:
257 GRANT_REVOKE_RIGHT(Privilege::ALTER);
258 break;
259 case COL_SELECT:
260 GRANT_REVOKE_RIGHT(Privilege::SELECT);
261 break;
262 case COL_REF:
263 GRANT_REVOKE_RIGHT(Privilege::REFERENCE);
264 break;
265 case COL_DROP:
266 GRANT_REVOKE_RIGHT(Privilege::DROP);
267 break;
269 fillPrivilege(nRow);
273 catch(SQLException& e)
275 bErg = sal_False;
276 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
278 if(bErg && Controller().Is())
279 Controller()->ClearModified();
280 if(!bErg)
281 UpdateTables();
283 return bErg;
286 //------------------------------------------------------------------------------
287 String OTableGrantControl::GetCellText( long nRow, sal_uInt16 nColId ) const
289 DBG_CHKTHIS(OTableGrantControl,NULL);
290 if(COL_TABLE_NAME == nColId)
291 return m_aTableNames[nRow];
293 sal_Int32 nPriv = 0;
294 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
295 if(aFind != m_aPrivMap.end())
296 nPriv = aFind->second.nRights;
298 return String::CreateFromInt32(isAllowed(nColId,nPriv) ? 1 :0);
301 //------------------------------------------------------------------------------
302 void OTableGrantControl::InitController( CellControllerRef& /*rController*/, long nRow, sal_uInt16 nColumnId )
304 DBG_CHKTHIS(OTableGrantControl,NULL);
305 String sTablename = m_aTableNames[nRow];
306 // special case for tablename
307 if(nColumnId == COL_TABLE_NAME)
308 m_pEdit->SetText(sTablename);
309 else
311 // get the privileges from the user
312 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
313 m_pCheckCell->GetBox().Check(aFind != m_aPrivMap.end() ? isAllowed(nColumnId,aFind->second.nRights) : sal_False);
316 // -----------------------------------------------------------------------------
317 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
320 if ( m_xUsers->hasByName(m_sUserName) )
324 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
325 if ( xAuth.is() )
327 // get the privileges
328 TPrivileges nRights;
329 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
330 if(m_xGrantUser.is())
331 nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
332 else
333 nRights.nWithGrant = 0;
335 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
338 catch(SQLException& e)
340 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
342 catch(Exception& )
347 // -----------------------------------------------------------------------------
348 sal_Bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege) const
350 sal_Bool bAllowed = sal_False;
351 switch (_nColumnId)
353 case COL_INSERT:
354 bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
355 break;
356 case COL_DELETE:
357 bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
358 break;
359 case COL_UPDATE:
360 bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
361 break;
362 case COL_ALTER:
363 bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
364 break;
365 case COL_SELECT:
366 bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
367 break;
368 case COL_REF:
369 bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
370 break;
371 case COL_DROP:
372 bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
373 break;
375 return bAllowed;
377 // -----------------------------------------------------------------------------
378 void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName)
380 m_sUserName = _sUserName;
381 m_aPrivMap = TTablePrivilegeMap();
383 // -----------------------------------------------------------------------------
384 void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
386 OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
387 m_xGrantUser = _xGrantUser;
389 //------------------------------------------------------------------------------
390 CellController* OTableGrantControl::GetController( long nRow, sal_uInt16 nColumnId )
392 DBG_CHKTHIS(OTableGrantControl,NULL);
394 CellController* pController = NULL;
395 switch( nColumnId )
397 case COL_TABLE_NAME:
398 break;
399 case COL_INSERT:
400 case COL_DELETE:
401 case COL_UPDATE:
402 case COL_ALTER:
403 case COL_SELECT:
404 case COL_REF:
405 case COL_DROP:
407 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
408 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
409 pController = new CheckBoxCellController( m_pCheckCell );
411 break;
412 default:
415 return pController;
417 //------------------------------------------------------------------------------
418 sal_Bool OTableGrantControl::SeekRow( long nRow )
420 DBG_CHKTHIS(OTableGrantControl,NULL);
421 m_nDataPos = nRow;
423 return (nRow <= m_aTableNames.getLength());
426 //------------------------------------------------------------------------------
427 void OTableGrantControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColumnId ) const
429 DBG_CHKTHIS(OTableGrantControl,NULL);
431 if(nColumnId != COL_TABLE_NAME)
433 TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
434 if(aFind != m_aPrivMap.end())
435 PaintTristate(rDev, rRect, isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK,isAllowed(nColumnId,aFind->second.nWithGrant));
436 else
437 PaintTristate(rDev, rRect, STATE_NOCHECK,sal_False);
439 else
441 String aText(((OTableGrantControl*)this)->GetCellText( m_nDataPos, nColumnId ));
442 Point aPos( rRect.TopLeft() );
443 sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
444 sal_Int32 nHeight = GetDataWindow().GetTextHeight();
446 if( aPos.X() < rRect.Right() || aPos.X() + nWidth > rRect.Right() ||
447 aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
448 rDev.SetClipRegion( rRect );
450 rDev.DrawText( aPos, aText );
453 if( rDev.IsClipRegion() )
454 rDev.SetClipRegion();
457 //------------------------------------------------------------------------
458 void OTableGrantControl::CellModified()
460 DBG_CHKTHIS(OTableGrantControl,NULL);
461 EditBrowseBox::CellModified();
462 SaveModified();
464 // -----------------------------------------------------------------------------
465 OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
467 TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
468 if(aFind == m_aPrivMap.end())
470 fillPrivilege(_nRow);
471 aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
473 return aFind;
475 // -----------------------------------------------------------------------------
476 Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
478 sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
479 if(nColumnId != COL_TABLE_NAME)
481 TriState eState = STATE_NOCHECK;
482 sal_Bool bEnable = sal_False;
483 TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
484 if(aFind != m_aPrivMap.end())
486 eState = isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK;
487 bEnable = isAllowed(nColumnId,aFind->second.nWithGrant);
489 else
490 eState = STATE_NOCHECK;
492 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState,bEnable );
494 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
496 // -----------------------------------------------------------------------------
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */