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 <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;
49 OTableGrantControl::OTableGrantControl( vcl::Window
* pParent
, WinBits nBits
)
50 :EditBrowseBox( pParent
, EditBrowseBoxFlags::SMART_TAB_TRAVEL
| EditBrowseBoxFlags::NO_HANDLE_COLUMN_CONTENT
, nBits
)
54 ,m_nDeactivateEvent(0)
58 InsertDataColumn( i
, OUString(ModuleRes(STR_TABLE_PRIV_NAME
) ), 75);
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);
69 SetColumnWidth(i
,GetAutoColumnWidth(i
));
72 OTableGrantControl::~OTableGrantControl()
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();
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
);
97 m_xUsers
= xUserSup
->getUsers();
99 // second we need the tables to determine which privileges the user has
101 m_xTables
= _xTablesSup
->getTables();
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()
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
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);
140 // Browser Mode setzen
141 BrowserMode nMode
= BrowserMode::COLUMNSELECTION
| BrowserMode::HLINES
| BrowserMode::VLINES
|
142 BrowserMode::HIDECURSOR
| BrowserMode::HIDESELECT
;
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;
177 IMPL_LINK_NOARG(OTableGrantControl
, AsynchDeactivate
)
179 m_nDeactivateEvent
= 0;
184 bool OTableGrantControl::IsTabAllowed(bool bForward
) const
186 long nRow
= GetCurRow();
187 sal_uInt16 nCol
= GetCurColumnId();
189 if (bForward
&& (nCol
== 2) && (nRow
== GetRowCount() - 1))
192 if (!bForward
&& (nCol
== 1) && (nRow
== 0))
195 return EditBrowseBox::IsTabAllowed(bForward
);
198 #define GRANT_REVOKE_RIGHT(what) \
199 if(m_pCheckCell->GetBox().IsChecked()) \
200 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
202 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
204 bool OTableGrantControl::SaveModified()
207 sal_Int32 nRow
= GetCurRow();
208 if(nRow
== -1 || nRow
>= m_aTableNames
.getLength())
211 OUString sTableName
= m_aTableNames
[nRow
];
216 if ( m_xUsers
->hasByName(m_sUserName
) )
218 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
221 switch( GetCurColumnId() )
224 GRANT_REVOKE_RIGHT(Privilege::INSERT
);
227 GRANT_REVOKE_RIGHT(Privilege::DELETE
);
230 GRANT_REVOKE_RIGHT(Privilege::UPDATE
);
233 GRANT_REVOKE_RIGHT(Privilege::ALTER
);
236 GRANT_REVOKE_RIGHT(Privilege::SELECT
);
239 GRANT_REVOKE_RIGHT(Privilege::REFERENCE
);
242 GRANT_REVOKE_RIGHT(Privilege::DROP
);
249 catch(SQLException
& e
)
252 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xContext
);
254 if(bErg
&& Controller().Is())
255 Controller()->ClearModified();
262 OUString
OTableGrantControl::GetCellText( long nRow
, sal_uInt16 nColId
) const
264 if(COL_TABLE_NAME
== nColId
)
265 return m_aTableNames
[nRow
];
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
);
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
);
299 // get the privileges
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
);
305 nRights
.nWithGrant
= 0;
307 m_aPrivMap
[m_aTableNames
[_nRow
]] = nRights
;
310 catch(SQLException
& e
)
312 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xContext
);
320 bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId
,sal_Int32 _nPrivilege
)
322 bool bAllowed
= false;
326 bAllowed
= (Privilege::INSERT
& _nPrivilege
) == Privilege::INSERT
;
329 bAllowed
= (Privilege::DELETE
& _nPrivilege
) == Privilege::DELETE
;
332 bAllowed
= (Privilege::UPDATE
& _nPrivilege
) == Privilege::UPDATE
;
335 bAllowed
= (Privilege::ALTER
& _nPrivilege
) == Privilege::ALTER
;
338 bAllowed
= (Privilege::SELECT
& _nPrivilege
) == Privilege::SELECT
;
341 bAllowed
= (Privilege::REFERENCE
& _nPrivilege
) == Privilege::REFERENCE
;
344 bAllowed
= (Privilege::DROP
& _nPrivilege
) == Privilege::DROP
;
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
;
378 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
379 if(aFind
!= m_aPrivMap
.end() && isAllowed(nColumnId
,aFind
->second
.nWithGrant
))
380 pController
= new CheckBoxCellController( m_pCheckCell
);
389 bool OTableGrantControl::SeekRow( long 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
));
405 PaintTristate(rDev
, rRect
, TRISTATE_FALSE
, false);
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();
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
]);
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
;
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: */