LibreOffice Module dbaccess (master) 1
TableGrantCtrl.cxx
Go to the documentation of this file.
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 <core_resource.hxx>
22#include <com/sun/star/sdbc/SQLException.hpp>
23#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24#include <com/sun/star/sdbcx/Privilege.hpp>
25#include <com/sun/star/sdbcx/PrivilegeObject.hpp>
26#include <com/sun/star/sdbcx/XUsersSupplier.hpp>
27#include <com/sun/star/sdbcx/XAuthorizable.hpp>
30#include <vcl/svapp.hxx>
31#include <osl/diagnose.h>
32#include <strings.hrc>
33
34using namespace ::com::sun::star::accessibility;
35using namespace ::com::sun::star::container;
36using namespace ::com::sun::star::sdbcx;
37using namespace ::com::sun::star::sdbc;
38using namespace ::com::sun::star::uno;
39using namespace ::dbaui;
40using namespace ::svt;
41
42const sal_uInt16 COL_TABLE_NAME = 1;
43const sal_uInt16 COL_SELECT = 2;
44const sal_uInt16 COL_INSERT = 3;
45const sal_uInt16 COL_DELETE = 4;
46const sal_uInt16 COL_UPDATE = 5;
47const sal_uInt16 COL_ALTER = 6;
48const sal_uInt16 COL_REF = 7;
49const sal_uInt16 COL_DROP = 8;
50
51
52// OTableGrantControl
53OTableGrantControl::OTableGrantControl(const css::uno::Reference<css::awt::XWindow> &rParent)
55 ,m_pCheckCell( nullptr )
56 ,m_pEdit( nullptr )
57 ,m_nDataPos( 0 )
58 ,m_nDeactivateEvent(nullptr)
59{
60 // insert columns
61 sal_uInt16 i=1;
62 InsertDataColumn( i, DBA_RES(STR_TABLE_PRIV_NAME), 75);
63 FreezeColumn(i++);
64 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_SELECT), 75);
65 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_INSERT), 75);
66 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_DELETE), 75);
67 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_UPDATE), 75);
68 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_ALTER), 75);
69 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_REFERENCE), 75);
70 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_DROP), 75);
71
72 while(--i)
74}
75
77{
78 disposeOnce();
79}
80
82{
84 {
86 m_nDeactivateEvent = nullptr;
87 }
88
89 m_pCheckCell.disposeAndClear();
90 m_pEdit.disposeAndClear();
91
92 m_xTables = nullptr;
94}
95
97{
98 // first we need the users
99 Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
100 if(xUserSup.is())
101 m_xUsers = xUserSup->getUsers();
102
103 // second we need the tables to determine which privileges the user has
104 if(_xTablesSup.is())
105 m_xTables = _xTablesSup->getTables();
106
107 if(m_xTables.is())
108 m_aTableNames = m_xTables->getElementNames();
109
110 OSL_ENSURE(m_xUsers.is(),"No user access supported!");
111 OSL_ENSURE(m_xTables.is(),"No tables supported!");
112}
113
115{
116 m_xContext = _rxContext;
117}
118
120{
121 RemoveRows();
122
123 if(m_xTables.is())
124 RowInserted(0, m_aTableNames.getLength());
125 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
126}
127
129{
131
132 // instantiate ComboBox
133 if(!m_pCheckCell)
134 {
136 m_pCheckCell->EnableTriState(false);
137
139 weld::Entry& rEntry = m_pEdit->get_widget();
140 rEntry.set_editable(false);
141 rEntry.set_sensitive(false);
142 }
143
144 UpdateTables();
145 // set browser mode
146 BrowserMode const nMode = BrowserMode::COLUMNSELECTION | BrowserMode::HLINES | BrowserMode::VLINES |
147 BrowserMode::HIDECURSOR | BrowserMode::HIDESELECT;
148
149 SetMode(nMode);
150}
151
153{
154 if (rNEvt.GetType() == NotifyEventType::LOSEFOCUS)
155 if (!HasChildPathFocus())
156 {
159 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate), nullptr, true);
160 }
161 if (rNEvt.GetType() == NotifyEventType::GETFOCUS)
162 {
165 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate), nullptr, true);
166 }
167 return EditBrowseBox::PreNotify(rNEvt);
168}
169
170IMPL_LINK_NOARG(OTableGrantControl, AsynchActivate, void*, void)
171{
172 m_nDeactivateEvent = nullptr;
173 ActivateCell();
174}
175
176IMPL_LINK_NOARG(OTableGrantControl, AsynchDeactivate, void*, void)
177{
178 m_nDeactivateEvent = nullptr;
179 DeactivateCell();
180}
181
182bool OTableGrantControl::IsTabAllowed(bool bForward) const
183{
184 sal_Int32 nRow = GetCurRow();
185 sal_uInt16 nCol = GetCurColumnId();
186
187 if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
188 return false;
189
190 if (!bForward && (nCol == 1) && (nRow == 0))
191 return false;
192
193 return EditBrowseBox::IsTabAllowed(bForward);
194}
195
197{
198
199 sal_Int32 nRow = GetCurRow();
200 if(nRow == -1 || nRow >= m_aTableNames.getLength())
201 return false;
202
203 OUString sTableName = m_aTableNames[nRow];
204 bool bErg = true;
205 try
206 {
207
208 if ( m_xUsers->hasByName(m_sUserName) )
209 {
210 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
211 if ( xAuth.is() )
212 {
213 switch( GetCurColumnId() )
214 {
215 case COL_INSERT:
216 if (m_pCheckCell->GetBox().get_active())
217 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::INSERT);
218 else
219 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::INSERT);
220 break;
221 case COL_DELETE:
222 if (m_pCheckCell->GetBox().get_active())
223 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DELETE);
224 else
225 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DELETE);
226 break;
227 case COL_UPDATE:
228 if (m_pCheckCell->GetBox().get_active())
229 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::UPDATE);
230 else
231 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::UPDATE);
232 break;
233 case COL_ALTER:
234 if (m_pCheckCell->GetBox().get_active())
235 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::ALTER);
236 else
237 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::ALTER);
238 break;
239 case COL_SELECT:
240 if (m_pCheckCell->GetBox().get_active())
241 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::SELECT);
242 else
243 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::SELECT);
244 break;
245 case COL_REF:
246 if (m_pCheckCell->GetBox().get_active())
247 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::REFERENCE);
248 else
249 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::REFERENCE);
250 break;
251 case COL_DROP:
252 if (m_pCheckCell->GetBox().get_active())
253 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DROP);
254 else
255 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DROP);
256 break;
257 }
258 fillPrivilege(nRow);
259 }
260 }
261 }
262 catch(SQLException& e)
263 {
264 bErg = false;
265 ::dbtools::showError(::dbtools::SQLExceptionInfo(e),VCLUnoHelper::GetInterface(GetParent()),m_xContext);
266 }
267 if(bErg && Controller().is())
268 Controller()->SaveValue();
269 if(!bErg)
270 UpdateTables();
271
272 return bErg;
273}
274
275OUString OTableGrantControl::GetCellText( sal_Int32 nRow, sal_uInt16 nColId ) const
276{
277 if(COL_TABLE_NAME == nColId)
278 return m_aTableNames[nRow];
279
280 sal_Int32 nPriv = 0;
281 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
282 if(aFind != m_aPrivMap.end())
283 nPriv = aFind->second.nRights;
284
285 return OUString::number(isAllowed(nColId,nPriv) ? 1 :0);
286}
287
288void OTableGrantControl::InitController( CellControllerRef& /*rController*/, sal_Int32 nRow, sal_uInt16 nColumnId )
289{
290 OUString sTablename = m_aTableNames[nRow];
291 // special case for tablename
292 if (nColumnId == COL_TABLE_NAME)
293 m_pEdit->get_widget().set_text(sTablename);
294 else
295 {
296 // get the privileges from the user
297 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
298 m_pCheckCell->GetBox().set_active(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nRights));
299 }
300}
301
302void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
303{
304
305 if ( !m_xUsers->hasByName(m_sUserName) )
306 return;
307
308 try
309 {
310 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
311 if ( xAuth.is() )
312 {
313 // get the privileges
314 TPrivileges nRights;
315 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
316 if(m_xGrantUser.is())
317 nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
318 else
319 nRights.nWithGrant = 0;
320
321 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
322 }
323 }
324 catch(SQLException& e)
325 {
326 ::dbtools::showError(::dbtools::SQLExceptionInfo(e),VCLUnoHelper::GetInterface(GetParent()),m_xContext);
327 }
328 catch(Exception& )
329 {
330 }
331}
332
333bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege)
334{
335 bool bAllowed = false;
336 switch (_nColumnId)
337 {
338 case COL_INSERT:
339 bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
340 break;
341 case COL_DELETE:
342 bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
343 break;
344 case COL_UPDATE:
345 bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
346 break;
347 case COL_ALTER:
348 bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
349 break;
350 case COL_SELECT:
351 bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
352 break;
353 case COL_REF:
354 bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
355 break;
356 case COL_DROP:
357 bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
358 break;
359 }
360 return bAllowed;
361}
362
363void OTableGrantControl::setUserName(const OUString& _sUserName)
364{
365 m_sUserName = _sUserName;
367}
368
370{
371 OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
372 m_xGrantUser = _xGrantUser;
373}
374
375CellController* OTableGrantControl::GetController( sal_Int32 nRow, sal_uInt16 nColumnId )
376{
377
378 CellController* pController = nullptr;
379 switch( nColumnId )
380 {
381 case COL_TABLE_NAME:
382 break;
383 case COL_INSERT:
384 case COL_DELETE:
385 case COL_UPDATE:
386 case COL_ALTER:
387 case COL_SELECT:
388 case COL_REF:
389 case COL_DROP:
390 {
391 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
392 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
393 pController = new CheckBoxCellController( m_pCheckCell );
394 }
395 break;
396 default:
397 ;
398 }
399 return pController;
400}
401
402bool OTableGrantControl::SeekRow( sal_Int32 nRow )
403{
404 m_nDataPos = nRow;
405
406 return (nRow <= m_aTableNames.getLength());
407}
408
409void OTableGrantControl::PaintCell( OutputDevice& rDev, const tools::Rectangle& rRect, sal_uInt16 nColumnId ) const
410{
411
412 if(nColumnId != COL_TABLE_NAME)
413 {
414 TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
415 if(aFind != m_aPrivMap.end())
416 PaintTristate(rRect, isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE,isAllowed(nColumnId,aFind->second.nWithGrant));
417 else
418 PaintTristate(rRect, TRISTATE_FALSE, false);
419 }
420 else
421 {
422 OUString aText(GetCellText( m_nDataPos, nColumnId ));
423 Point aPos( rRect.TopLeft() );
424 sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
425 sal_Int32 nHeight = GetDataWindow().GetTextHeight();
426
427 if( aPos.X() < rRect.Left() || aPos.X() + nWidth > rRect.Right() ||
428 aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
429 {
430 rDev.SetClipRegion(vcl::Region(rRect));
431 }
432
433 rDev.DrawText( aPos, aText );
434 }
435
436 if( rDev.IsClipRegion() )
437 rDev.SetClipRegion();
438}
439
441{
443 SaveModified();
444}
445
446OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
447{
448 TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
449 if(aFind == m_aPrivMap.end())
450 {
451 fillPrivilege(_nRow);
452 aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
453 }
454 return aFind;
455}
456
458{
459 sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
460 if(nColumnId != COL_TABLE_NAME)
461 {
462 TriState eState = TRISTATE_FALSE;
463 TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
464 if(aFind != m_aPrivMap.end())
465 {
466 eState = isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE;
467 }
468 else
469 eState = TRISTATE_FALSE;
470
471 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState );
472 }
473 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
474}
475
476/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_uInt16 COL_REF
const sal_uInt16 COL_TABLE_NAME
const sal_uInt16 COL_DELETE
const sal_uInt16 COL_UPDATE
const sal_uInt16 COL_DROP
const sal_uInt16 COL_ALTER
const sal_uInt16 COL_INSERT
const sal_uInt16 COL_SELECT
BrowserMode
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
sal_uInt16 GetColumnId(sal_uInt16 nPos) const
BrowserDataWin & GetDataWindow() const
void InsertDataColumn(sal_uInt16 nItemId, const OUString &rText, tools::Long nSize, HeaderBarItemBits nBits=HeaderBarItemBits::STDSTYLE, sal_uInt16 nPos=HEADERBAR_APPEND)
virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessibleCell(sal_Int32 nRow, sal_uInt16 nColumnPos) override
sal_Int32 GetCurRow() const
void FreezeColumn(sal_uInt16 nColumnId)
void SetMode(BrowserMode nMode)
void SetColumnWidth(sal_uInt16 nColumnId, sal_uLong nWidth)
sal_uInt16 GetCurColumnId() const
void RowInserted(sal_Int32 nRow, sal_Int32 nNumRows=1, bool bDoPaint=true, bool bKeepSelection=false)
virtual sal_Int32 GetRowCount() const override
NotifyEventType GetType() const
bool IsClipRegion() const
void SetClipRegion()
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, std::vector< tools::Rectangle > *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
constexpr tools::Long Y() const
constexpr tools::Long X() const
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static VclPtr< reference_type > Create(Arg &&... arg)
css::uno::Reference< css::sdbcx::XAuthorizable > m_xGrantUser
void fillPrivilege(sal_Int32 _nRow) const
virtual bool IsTabAllowed(bool bForward) const override
css::uno::Reference< css::container::XNameAccess > m_xTables
std::map< OUString, TPrivileges > TTablePrivilegeMap
void setGrantUser(const css::uno::Reference< css::sdbcx::XAuthorizable > &_xGrantUser)
void setComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
VclPtr<::svt::EditControl > m_pEdit
css::uno::Sequence< OUString > m_aTableNames
void setTablesSupplier(const css::uno::Reference< css::sdbcx::XTablesSupplier > &_xTablesSup)
virtual ~OTableGrantControl() override
static bool isAllowed(sal_uInt16 _nColumnId, sal_Int32 _nPrivilege)
TTablePrivilegeMap::const_iterator findPrivilege(sal_Int32 _nRow) const
ImplSVEvent * m_nDeactivateEvent
void setUserName(const OUString &_sUserName)
css::uno::Reference< css::container::XNameAccess > m_xUsers
virtual void Init() override
TTablePrivilegeMap m_aPrivMap
virtual bool SaveModified() override
virtual ::svt::CellController * GetController(sal_Int32 nRow, sal_uInt16 nCol) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual bool PreNotify(NotifyEvent &rNEvt) override
virtual OUString GetCellText(sal_Int32 nRow, sal_uInt16 nColId) const override
virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessibleCell(sal_Int32 nRow, sal_uInt16 nColumnId) override
Creates the accessible object of a data table cell.
VclPtr<::svt::CheckBoxControl > m_pCheckCell
virtual void InitController(::svt::CellControllerRef &rController, sal_Int32 nRow, sal_uInt16 nCol) override
virtual void dispose() override
virtual bool SeekRow(sal_Int32 nRow) override
virtual void CellModified() override
virtual void PaintCell(OutputDevice &rDev, const tools::Rectangle &rRect, sal_uInt16 nColId) const override
css::uno::Reference< css::accessibility::XAccessible > CreateAccessibleCheckBoxCell(sal_Int32 _nRow, sal_uInt16 _nColumnPos, const TriState &eState)
sal_uInt32 GetAutoColumnWidth(sal_uInt16 nColId)
virtual bool IsTabAllowed(bool bForward) const
const CellControllerRef & Controller() const
virtual void RemoveRows()
virtual bool PreNotify(NotifyEvent &rNEvt) override
void PaintTristate(const tools::Rectangle &rRect, const TriState &eState, bool _bEnabled=true) const
virtual void Init()
virtual void dispose() override
virtual void CellModified()
constexpr tools::Long Top() const
constexpr Point TopLeft() const
constexpr tools::Long Right() const
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
virtual void set_editable(bool bEditable)=0
virtual void set_sensitive(bool sensitive)=0
#define DBA_RES(id)
EditBrowseBoxFlags
TriState
TRISTATE_FALSE
TRISTATE_TRUE
@ Exception
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
int i
WinBits const WB_TABSTOP