Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / control / TableGrantCtrl.cxx
bloba23925ae254d1019e3e6f853d86d1e28e1c0f8fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "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"
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;
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;
48 // OTableGrantControl
49 OTableGrantControl::OTableGrantControl( vcl::Window* pParent, WinBits nBits)
50 :EditBrowseBox( pParent, EditBrowseBoxFlags::SMART_TAB_TRAVEL | EditBrowseBoxFlags::NO_HANDLE_COLUMN_CONTENT, nBits )
51 ,m_pCheckCell( NULL )
52 ,m_pEdit( NULL )
53 ,m_nDataPos( 0 )
54 ,m_nDeactivateEvent(0)
56 // Spalten einfuegen
57 sal_uInt16 i=1;
58 InsertDataColumn( i, OUString(ModuleRes(STR_TABLE_PRIV_NAME) ), 75);
59 FreezeColumn(i++);
60 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_SELECT)), 75);
61 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_INSERT)), 75);
62 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_DELETE)), 75);
63 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_UPDATE)), 75);
64 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_ALTER)), 75);
65 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_REFERENCE)), 75);
66 InsertDataColumn( i++, OUString(ModuleRes(STR_TABLE_PRIV_DROP)), 75);
68 while(--i)
69 SetColumnWidth(i,GetAutoColumnWidth(i));
72 OTableGrantControl::~OTableGrantControl()
74 disposeOnce();
77 void OTableGrantControl::dispose()
79 if (m_nDeactivateEvent)
81 Application::RemoveUserEvent(m_nDeactivateEvent);
82 m_nDeactivateEvent = 0;
85 m_pCheckCell.disposeAndClear();
86 m_pEdit.disposeAndClear();
88 m_xTables = NULL;
89 ::svt::EditBrowseBox::dispose();
92 void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
94 // first we need the users
95 Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
96 if(xUserSup.is())
97 m_xUsers = xUserSup->getUsers();
99 // second we need the tables to determine which privileges the user has
100 if(_xTablesSup.is())
101 m_xTables = _xTablesSup->getTables();
103 if(m_xTables.is())
104 m_aTableNames = m_xTables->getElementNames();
106 OSL_ENSURE(m_xUsers.is(),"No user access supported!");
107 OSL_ENSURE(m_xTables.is(),"No tables supported!");
110 void OTableGrantControl::setComponentContext(const Reference< ::com::sun::star::uno::XComponentContext>& _rxContext)
112 m_xContext = _rxContext;
115 void OTableGrantControl::UpdateTables()
117 RemoveRows();
119 if(m_xTables.is())
120 RowInserted(0, m_aTableNames.getLength());
121 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
124 void OTableGrantControl::Init()
126 EditBrowseBox::Init();
128 // ComboBox instanzieren
129 if(!m_pCheckCell)
131 m_pCheckCell = VclPtr<CheckBoxControl>::Create( &GetDataWindow() );
132 m_pCheckCell->GetBox().EnableTriState(false);
134 m_pEdit = VclPtr<Edit>::Create( &GetDataWindow() );
135 m_pEdit->SetReadOnly();
136 m_pEdit->Enable(false);
139 UpdateTables();
140 // Browser Mode setzen
141 BrowserMode nMode = BrowserMode::COLUMNSELECTION | BrowserMode::HLINES | BrowserMode::VLINES |
142 BrowserMode::HIDECURSOR | BrowserMode::HIDESELECT;
144 SetMode(nMode);
147 void OTableGrantControl::Resize()
149 EditBrowseBox::Resize();
152 bool OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
154 if (rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS)
155 if (!HasChildPathFocus())
157 if (m_nDeactivateEvent)
158 Application::RemoveUserEvent(m_nDeactivateEvent);
159 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate), NULL, true);
161 if (rNEvt.GetType() == MouseNotifyEvent::GETFOCUS)
163 if (m_nDeactivateEvent)
164 Application::RemoveUserEvent(m_nDeactivateEvent);
165 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate), NULL, true);
167 return EditBrowseBox::PreNotify(rNEvt);
170 IMPL_LINK_NOARG(OTableGrantControl, AsynchActivate)
172 m_nDeactivateEvent = 0;
173 ActivateCell();
174 return 0L;
177 IMPL_LINK_NOARG(OTableGrantControl, AsynchDeactivate)
179 m_nDeactivateEvent = 0;
180 DeactivateCell();
181 return 0L;
184 bool OTableGrantControl::IsTabAllowed(bool bForward) const
186 long nRow = GetCurRow();
187 sal_uInt16 nCol = GetCurColumnId();
189 if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
190 return false;
192 if (!bForward && (nCol == 1) && (nRow == 0))
193 return false;
195 return EditBrowseBox::IsTabAllowed(bForward);
198 #define GRANT_REVOKE_RIGHT(what) \
199 if(m_pCheckCell->GetBox().IsChecked()) \
200 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
201 else \
202 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
204 bool OTableGrantControl::SaveModified()
207 sal_Int32 nRow = GetCurRow();
208 if(nRow == -1 || nRow >= m_aTableNames.getLength())
209 return false;
211 OUString sTableName = m_aTableNames[nRow];
212 bool bErg = true;
216 if ( m_xUsers->hasByName(m_sUserName) )
218 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
219 if ( xAuth.is() )
221 switch( GetCurColumnId() )
223 case COL_INSERT:
224 GRANT_REVOKE_RIGHT(Privilege::INSERT);
225 break;
226 case COL_DELETE:
227 GRANT_REVOKE_RIGHT(Privilege::DELETE);
228 break;
229 case COL_UPDATE:
230 GRANT_REVOKE_RIGHT(Privilege::UPDATE);
231 break;
232 case COL_ALTER:
233 GRANT_REVOKE_RIGHT(Privilege::ALTER);
234 break;
235 case COL_SELECT:
236 GRANT_REVOKE_RIGHT(Privilege::SELECT);
237 break;
238 case COL_REF:
239 GRANT_REVOKE_RIGHT(Privilege::REFERENCE);
240 break;
241 case COL_DROP:
242 GRANT_REVOKE_RIGHT(Privilege::DROP);
243 break;
245 fillPrivilege(nRow);
249 catch(SQLException& e)
251 bErg = false;
252 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xContext);
254 if(bErg && Controller().Is())
255 Controller()->ClearModified();
256 if(!bErg)
257 UpdateTables();
259 return bErg;
262 OUString OTableGrantControl::GetCellText( long nRow, sal_uInt16 nColId ) const
264 if(COL_TABLE_NAME == nColId)
265 return m_aTableNames[nRow];
267 sal_Int32 nPriv = 0;
268 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
269 if(aFind != m_aPrivMap.end())
270 nPriv = aFind->second.nRights;
272 return OUString::number(isAllowed(nColId,nPriv) ? 1 :0);
275 void OTableGrantControl::InitController( CellControllerRef& /*rController*/, long nRow, sal_uInt16 nColumnId )
277 OUString sTablename = m_aTableNames[nRow];
278 // special case for tablename
279 if(nColumnId == COL_TABLE_NAME)
280 m_pEdit->SetText(sTablename);
281 else
283 // get the privileges from the user
284 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
285 m_pCheckCell->GetBox().Check(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nRights));
289 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
292 if ( m_xUsers->hasByName(m_sUserName) )
296 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
297 if ( xAuth.is() )
299 // get the privileges
300 TPrivileges nRights;
301 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
302 if(m_xGrantUser.is())
303 nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
304 else
305 nRights.nWithGrant = 0;
307 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
310 catch(SQLException& e)
312 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xContext);
314 catch(Exception& )
320 bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege)
322 bool bAllowed = false;
323 switch (_nColumnId)
325 case COL_INSERT:
326 bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
327 break;
328 case COL_DELETE:
329 bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
330 break;
331 case COL_UPDATE:
332 bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
333 break;
334 case COL_ALTER:
335 bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
336 break;
337 case COL_SELECT:
338 bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
339 break;
340 case COL_REF:
341 bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
342 break;
343 case COL_DROP:
344 bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
345 break;
347 return bAllowed;
350 void OTableGrantControl::setUserName(const OUString& _sUserName)
352 m_sUserName = _sUserName;
353 m_aPrivMap = TTablePrivilegeMap();
356 void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
358 OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
359 m_xGrantUser = _xGrantUser;
362 CellController* OTableGrantControl::GetController( long nRow, sal_uInt16 nColumnId )
365 CellController* pController = NULL;
366 switch( nColumnId )
368 case COL_TABLE_NAME:
369 break;
370 case COL_INSERT:
371 case COL_DELETE:
372 case COL_UPDATE:
373 case COL_ALTER:
374 case COL_SELECT:
375 case COL_REF:
376 case COL_DROP:
378 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
379 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
380 pController = new CheckBoxCellController( m_pCheckCell );
382 break;
383 default:
386 return pController;
389 bool OTableGrantControl::SeekRow( long nRow )
391 m_nDataPos = nRow;
393 return (nRow <= m_aTableNames.getLength());
396 void OTableGrantControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColumnId ) const
399 if(nColumnId != COL_TABLE_NAME)
401 TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
402 if(aFind != m_aPrivMap.end())
403 PaintTristate(rDev, rRect, isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE,isAllowed(nColumnId,aFind->second.nWithGrant));
404 else
405 PaintTristate(rDev, rRect, TRISTATE_FALSE, false);
407 else
409 OUString aText(GetCellText( m_nDataPos, nColumnId ));
410 Point aPos( rRect.TopLeft() );
411 sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
412 sal_Int32 nHeight = GetDataWindow().GetTextHeight();
414 if( aPos.X() < rRect.Left() || aPos.X() + nWidth > rRect.Right() ||
415 aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
417 rDev.SetClipRegion(vcl::Region(rRect));
420 rDev.DrawText( aPos, aText );
423 if( rDev.IsClipRegion() )
424 rDev.SetClipRegion();
427 void OTableGrantControl::CellModified()
429 EditBrowseBox::CellModified();
430 SaveModified();
433 OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
435 TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
436 if(aFind == m_aPrivMap.end())
438 fillPrivilege(_nRow);
439 aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
441 return aFind;
444 Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
446 sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
447 if(nColumnId != COL_TABLE_NAME)
449 TriState eState = TRISTATE_FALSE;
450 TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
451 if(aFind != m_aPrivMap.end())
453 eState = isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE;
455 else
456 eState = TRISTATE_FALSE;
458 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState );
460 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */