1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <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>
28 #include <connectivity/dbtools.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <vcl/svapp.hxx>
31 #include <osl/diagnose.h>
32 #include <strings.hrc>
34 using namespace ::com::sun::star::accessibility
;
35 using namespace ::com::sun::star::container
;
36 using namespace ::com::sun::star::sdbcx
;
37 using namespace ::com::sun::star::sdbc
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::dbaui
;
40 using namespace ::svt
;
42 const sal_uInt16 COL_TABLE_NAME
= 1;
43 const sal_uInt16 COL_SELECT
= 2;
44 const sal_uInt16 COL_INSERT
= 3;
45 const sal_uInt16 COL_DELETE
= 4;
46 const sal_uInt16 COL_UPDATE
= 5;
47 const sal_uInt16 COL_ALTER
= 6;
48 const sal_uInt16 COL_REF
= 7;
49 const sal_uInt16 COL_DROP
= 8;
53 OTableGrantControl::OTableGrantControl(const css::uno::Reference
<css::awt::XWindow
> &rParent
)
54 :EditBrowseBox(VCLUnoHelper::GetWindow(rParent
), EditBrowseBoxFlags::SMART_TAB_TRAVEL
| EditBrowseBoxFlags::NO_HANDLE_COLUMN_CONTENT
, WB_TABSTOP
)
55 ,m_pCheckCell( nullptr )
58 ,m_nDeactivateEvent(nullptr)
62 InsertDataColumn( i
, DBA_RES(STR_TABLE_PRIV_NAME
), 75);
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);
73 SetColumnWidth(i
,GetAutoColumnWidth(i
));
76 OTableGrantControl::~OTableGrantControl()
81 void OTableGrantControl::dispose()
83 if (m_nDeactivateEvent
)
85 Application::RemoveUserEvent(m_nDeactivateEvent
);
86 m_nDeactivateEvent
= nullptr;
89 m_pCheckCell
.disposeAndClear();
90 m_pEdit
.disposeAndClear();
93 ::svt::EditBrowseBox::dispose();
96 void OTableGrantControl::setTablesSupplier(const Reference
< XTablesSupplier
>& _xTablesSup
)
98 // first we need the users
99 Reference
< XUsersSupplier
> xUserSup(_xTablesSup
,UNO_QUERY
);
101 m_xUsers
= xUserSup
->getUsers();
103 // second we need the tables to determine which privileges the user has
105 m_xTables
= _xTablesSup
->getTables();
108 m_aTableNames
= m_xTables
->getElementNames();
110 OSL_ENSURE(m_xUsers
.is(),"No user access supported!");
111 OSL_ENSURE(m_xTables
.is(),"No tables supported!");
114 void OTableGrantControl::setComponentContext(const Reference
< css::uno::XComponentContext
>& _rxContext
)
116 m_xContext
= _rxContext
;
119 void OTableGrantControl::UpdateTables()
124 RowInserted(0, m_aTableNames
.getLength());
125 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
128 void OTableGrantControl::Init()
130 EditBrowseBox::Init();
132 // instantiate ComboBox
135 m_pCheckCell
= VclPtr
<CheckBoxControl
>::Create( &GetDataWindow() );
136 m_pCheckCell
->EnableTriState(false);
138 m_pEdit
= VclPtr
<EditControl
>::Create(&GetDataWindow());
139 weld::Entry
& rEntry
= m_pEdit
->get_widget();
140 rEntry
.set_editable(false);
141 rEntry
.set_sensitive(false);
146 BrowserMode
const nMode
= BrowserMode::COLUMNSELECTION
| BrowserMode::HLINES
| BrowserMode::VLINES
|
147 BrowserMode::HIDECURSOR
| BrowserMode::HIDESELECT
;
152 bool OTableGrantControl::PreNotify(NotifyEvent
& rNEvt
)
154 if (rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
155 if (!HasChildPathFocus())
157 if (m_nDeactivateEvent
)
158 Application::RemoveUserEvent(m_nDeactivateEvent
);
159 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchDeactivate
), nullptr, true);
161 if (rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
163 if (m_nDeactivateEvent
)
164 Application::RemoveUserEvent(m_nDeactivateEvent
);
165 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchActivate
), nullptr, true);
167 return EditBrowseBox::PreNotify(rNEvt
);
170 IMPL_LINK_NOARG(OTableGrantControl
, AsynchActivate
, void*, void)
172 m_nDeactivateEvent
= nullptr;
176 IMPL_LINK_NOARG(OTableGrantControl
, AsynchDeactivate
, void*, void)
178 m_nDeactivateEvent
= nullptr;
182 bool OTableGrantControl::IsTabAllowed(bool bForward
) const
184 sal_Int32 nRow
= GetCurRow();
185 sal_uInt16 nCol
= GetCurColumnId();
187 if (bForward
&& (nCol
== 2) && (nRow
== GetRowCount() - 1))
190 if (!bForward
&& (nCol
== 1) && (nRow
== 0))
193 return EditBrowseBox::IsTabAllowed(bForward
);
196 bool OTableGrantControl::SaveModified()
199 sal_Int32 nRow
= GetCurRow();
200 if(nRow
== -1 || nRow
>= m_aTableNames
.getLength())
203 OUString sTableName
= m_aTableNames
[nRow
];
208 if ( m_xUsers
->hasByName(m_sUserName
) )
210 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
213 switch( GetCurColumnId() )
216 if (m_pCheckCell
->GetBox().get_active())
217 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::INSERT
);
219 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::INSERT
);
222 if (m_pCheckCell
->GetBox().get_active())
223 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::DELETE
);
225 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::DELETE
);
228 if (m_pCheckCell
->GetBox().get_active())
229 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::UPDATE
);
231 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::UPDATE
);
234 if (m_pCheckCell
->GetBox().get_active())
235 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::ALTER
);
237 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::ALTER
);
240 if (m_pCheckCell
->GetBox().get_active())
241 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::SELECT
);
243 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::SELECT
);
246 if (m_pCheckCell
->GetBox().get_active())
247 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::REFERENCE
);
249 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::REFERENCE
);
252 if (m_pCheckCell
->GetBox().get_active())
253 xAuth
->grantPrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::DROP
);
255 xAuth
->revokePrivileges(sTableName
,PrivilegeObject::TABLE
,Privilege::DROP
);
262 catch(SQLException
& e
)
265 ::dbtools::showError(::dbtools::SQLExceptionInfo(e
),VCLUnoHelper::GetInterface(GetParent()),m_xContext
);
267 if(bErg
&& Controller().is())
268 Controller()->SaveValue();
275 OUString
OTableGrantControl::GetCellText( sal_Int32 nRow
, sal_uInt16 nColId
) const
277 if(COL_TABLE_NAME
== nColId
)
278 return m_aTableNames
[nRow
];
281 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
282 if(aFind
!= m_aPrivMap
.end())
283 nPriv
= aFind
->second
.nRights
;
285 return OUString::number(isAllowed(nColId
,nPriv
) ? 1 :0);
288 void OTableGrantControl::InitController( CellControllerRef
& /*rController*/, sal_Int32 nRow
, sal_uInt16 nColumnId
)
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
);
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
));
302 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow
) const
305 if ( !m_xUsers
->hasByName(m_sUserName
) )
310 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
313 // get the privileges
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
);
319 nRights
.nWithGrant
= 0;
321 m_aPrivMap
[m_aTableNames
[_nRow
]] = nRights
;
324 catch(SQLException
& e
)
326 ::dbtools::showError(::dbtools::SQLExceptionInfo(e
),VCLUnoHelper::GetInterface(GetParent()),m_xContext
);
333 bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId
,sal_Int32 _nPrivilege
)
335 bool bAllowed
= false;
339 bAllowed
= (Privilege::INSERT
& _nPrivilege
) == Privilege::INSERT
;
342 bAllowed
= (Privilege::DELETE
& _nPrivilege
) == Privilege::DELETE
;
345 bAllowed
= (Privilege::UPDATE
& _nPrivilege
) == Privilege::UPDATE
;
348 bAllowed
= (Privilege::ALTER
& _nPrivilege
) == Privilege::ALTER
;
351 bAllowed
= (Privilege::SELECT
& _nPrivilege
) == Privilege::SELECT
;
354 bAllowed
= (Privilege::REFERENCE
& _nPrivilege
) == Privilege::REFERENCE
;
357 bAllowed
= (Privilege::DROP
& _nPrivilege
) == Privilege::DROP
;
363 void OTableGrantControl::setUserName(const OUString
& _sUserName
)
365 m_sUserName
= _sUserName
;
366 m_aPrivMap
= TTablePrivilegeMap();
369 void OTableGrantControl::setGrantUser(const Reference
< XAuthorizable
>& _xGrantUser
)
371 OSL_ENSURE(_xGrantUser
.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
372 m_xGrantUser
= _xGrantUser
;
375 CellController
* OTableGrantControl::GetController( sal_Int32 nRow
, sal_uInt16 nColumnId
)
378 CellController
* pController
= nullptr;
391 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
392 if(aFind
!= m_aPrivMap
.end() && isAllowed(nColumnId
,aFind
->second
.nWithGrant
))
393 pController
= new CheckBoxCellController( m_pCheckCell
);
402 bool OTableGrantControl::SeekRow( sal_Int32 nRow
)
406 return (nRow
<= m_aTableNames
.getLength());
409 void OTableGrantControl::PaintCell( OutputDevice
& rDev
, const tools::Rectangle
& rRect
, sal_uInt16 nColumnId
) const
412 if(nColumnId
!= COL_TABLE_NAME
)
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
));
418 PaintTristate(rRect
, TRISTATE_FALSE
, false);
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();
427 if( aPos
.X() < rRect
.Left() || aPos
.X() + nWidth
> rRect
.Right() ||
428 aPos
.Y() < rRect
.Top() || aPos
.Y() + nHeight
> rRect
.Bottom() )
430 rDev
.SetClipRegion(vcl::Region(rRect
));
433 rDev
.DrawText( aPos
, aText
);
436 if( rDev
.IsClipRegion() )
437 rDev
.SetClipRegion();
440 void OTableGrantControl::CellModified()
442 EditBrowseBox::CellModified();
446 OTableGrantControl::TTablePrivilegeMap::const_iterator
OTableGrantControl::findPrivilege(sal_Int32 _nRow
) const
448 TTablePrivilegeMap::const_iterator aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
449 if(aFind
== m_aPrivMap
.end())
451 fillPrivilege(_nRow
);
452 aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
457 Reference
< XAccessible
> OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow
, sal_uInt16 _nColumnPos
)
459 sal_uInt16 nColumnId
= GetColumnId( _nColumnPos
);
460 if(nColumnId
!= COL_TABLE_NAME
)
462 TriState eState
= TRISTATE_FALSE
;
463 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(_nRow
);
464 if(aFind
!= m_aPrivMap
.end())
466 eState
= isAllowed(nColumnId
,aFind
->second
.nRights
) ? TRISTATE_TRUE
: TRISTATE_FALSE
;
469 eState
= TRISTATE_FALSE
;
471 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow
, _nColumnPos
,eState
);
473 return EditBrowseBox::CreateAccessibleCell( _nRow
, _nColumnPos
);
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */