1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TableGrantCtrl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_TABLEGRANTCONTROL_HXX
34 #include "TableGrantCtrl.hxx"
36 #ifndef _TOOLS_DEBUG_HXX
37 #include <tools/debug.hxx>
39 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
40 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42 #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGE_HPP_
43 #include <com/sun/star/sdbcx/Privilege.hpp>
45 #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGEOBJECT_HPP_
46 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
48 #ifndef _COM_SUN_STAR_SDBCX_XUSERSSUPPLIER_HPP_
49 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
51 #ifndef _COM_SUN_STAR_SDBCX_XAUTHORIZABLE_HPP_
52 #include <com/sun/star/sdbcx/XAuthorizable.hpp>
54 #ifndef _DBU_CONTROL_HRC_
55 #include "dbu_control.hrc"
57 #ifndef DBAUI_TOOLS_HXX
58 #include "UITools.hxx"
61 using namespace ::com::sun::star::accessibility
;
62 using namespace ::com::sun::star::container
;
63 using namespace ::com::sun::star::sdbcx
;
64 using namespace ::com::sun::star::sdbc
;
65 using namespace ::com::sun::star::uno
;
66 using namespace ::dbaui
;
67 using namespace ::svt
;
69 const USHORT COL_TABLE_NAME
= 1;
70 const USHORT COL_SELECT
= 2;
71 const USHORT COL_INSERT
= 3;
72 const USHORT COL_DELETE
= 4;
73 const USHORT COL_UPDATE
= 5;
74 const USHORT COL_ALTER
= 6;
75 const USHORT COL_REF
= 7;
76 const USHORT COL_DROP
= 8;
78 DBG_NAME(OTableGrantControl
)
80 //================================================================================
82 //================================================================================
83 OTableGrantControl::OTableGrantControl( Window
* pParent
,const ResId
& _RsId
)
84 :EditBrowseBox( pParent
,_RsId
, EBBF_SMART_TAB_TRAVEL
| EBBF_NOROWPICTURE
)
89 ,m_nDeactivateEvent(0)
91 DBG_CTOR(OTableGrantControl
,NULL
);
92 //////////////////////////////////////////////////////////////////////
95 InsertDataColumn( i
, String(ModuleRes(STR_TABLE_PRIV_NAME
) ), 75);
97 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_SELECT
)), 75);
98 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_INSERT
)), 75);
99 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_DELETE
)), 75);
100 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_UPDATE
)), 75);
101 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_ALTER
)), 75);
102 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE
)), 75);
103 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_DROP
)), 75);
106 SetColumnWidth(i
,GetAutoColumnWidth(i
));
109 //------------------------------------------------------------------------
110 OTableGrantControl::~OTableGrantControl()
112 DBG_DTOR(OTableGrantControl
,NULL
);
113 if (m_nDeactivateEvent
)
115 Application::RemoveUserEvent(m_nDeactivateEvent
);
116 m_nDeactivateEvent
= 0;
124 // -----------------------------------------------------------------------------
125 void OTableGrantControl::setTablesSupplier(const Reference
< XTablesSupplier
>& _xTablesSup
)
127 // first we need the users
128 Reference
< XUsersSupplier
> xUserSup(_xTablesSup
,UNO_QUERY
);
130 m_xUsers
= xUserSup
->getUsers();
132 // second we need the tables to determine which privileges the user has
134 m_xTables
= _xTablesSup
->getTables();
137 m_aTableNames
= m_xTables
->getElementNames();
139 OSL_ENSURE(m_xUsers
.is(),"No user access supported!");
140 OSL_ENSURE(m_xTables
.is(),"No tables supported!");
142 // -----------------------------------------------------------------------------
143 void OTableGrantControl::setORB(const Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _xORB
)
147 //------------------------------------------------------------------------
148 void OTableGrantControl::UpdateTables()
153 RowInserted(0, m_aTableNames
.getLength());
154 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
156 //------------------------------------------------------------------------
157 void OTableGrantControl::Init()
159 DBG_CHKTHIS(OTableGrantControl
,NULL
);
160 EditBrowseBox::Init();
162 //////////////////////////////////////////////////////////////////////
163 // ComboBox instanzieren
166 m_pCheckCell
= new CheckBoxControl( &GetDataWindow() );
167 m_pCheckCell
->GetBox().EnableTriState(FALSE
);
169 m_pEdit
= new Edit( &GetDataWindow() );
170 m_pEdit
->SetReadOnly();
171 m_pEdit
->Enable(FALSE
);
175 //////////////////////////////////////////////////////////////////////
176 // Browser Mode setzen
177 BrowserMode nMode
= BROWSER_COLUMNSELECTION
| BROWSER_HLINESFULL
| BROWSER_VLINESFULL
|
178 BROWSER_HIDECURSOR
| BROWSER_HIDESELECT
;
183 //------------------------------------------------------------------------------
184 void OTableGrantControl::Resize()
186 DBG_CHKTHIS(OTableGrantControl
,NULL
);
187 EditBrowseBox::Resize();
190 //------------------------------------------------------------------------------
191 long OTableGrantControl::PreNotify(NotifyEvent
& rNEvt
)
193 DBG_CHKTHIS(OTableGrantControl
,NULL
);
194 if (rNEvt
.GetType() == EVENT_LOSEFOCUS
)
195 if (!HasChildPathFocus())
197 if (m_nDeactivateEvent
)
198 Application::RemoveUserEvent(m_nDeactivateEvent
);
199 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchDeactivate
));
201 if (rNEvt
.GetType() == EVENT_GETFOCUS
)
203 if (m_nDeactivateEvent
)
204 Application::RemoveUserEvent(m_nDeactivateEvent
);
205 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchActivate
));
207 return EditBrowseBox::PreNotify(rNEvt
);
210 //------------------------------------------------------------------------------
211 IMPL_LINK(OTableGrantControl
, AsynchActivate
, void*, EMPTYARG
)
213 m_nDeactivateEvent
= 0;
218 //------------------------------------------------------------------------------
219 IMPL_LINK(OTableGrantControl
, AsynchDeactivate
, void*, EMPTYARG
)
221 m_nDeactivateEvent
= 0;
226 //------------------------------------------------------------------------------
227 BOOL
OTableGrantControl::IsTabAllowed(BOOL bForward
) const
229 DBG_CHKTHIS(OTableGrantControl
,NULL
);
230 long nRow
= GetCurRow();
231 USHORT nCol
= GetCurColumnId();
233 if (bForward
&& (nCol
== 2) && (nRow
== GetRowCount() - 1))
236 if (!bForward
&& (nCol
== 1) && (nRow
== 0))
239 return EditBrowseBox::IsTabAllowed(bForward
);
241 //------------------------------------------------------------------------------
242 #define GRANT_REVOKE_RIGHT(what) \
243 if(m_pCheckCell->GetBox().IsChecked()) \
244 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
246 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
248 //------------------------------------------------------------------------------
249 BOOL
OTableGrantControl::SaveModified()
251 DBG_CHKTHIS(OTableGrantControl
,NULL
);
253 sal_Int32 nRow
= GetCurRow();
254 if(nRow
== -1 || nRow
>= m_aTableNames
.getLength())
257 ::rtl::OUString sTableName
= m_aTableNames
[nRow
];
262 if ( m_xUsers
->hasByName(m_sUserName
) )
264 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
267 switch( GetCurColumnId() )
270 GRANT_REVOKE_RIGHT(Privilege::INSERT
);
273 GRANT_REVOKE_RIGHT(Privilege::DELETE
);
276 GRANT_REVOKE_RIGHT(Privilege::UPDATE
);
279 GRANT_REVOKE_RIGHT(Privilege::ALTER
);
282 GRANT_REVOKE_RIGHT(Privilege::SELECT
);
285 GRANT_REVOKE_RIGHT(Privilege::REFERENCE
);
288 GRANT_REVOKE_RIGHT(Privilege::DROP
);
295 catch(SQLException
& e
)
298 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xORB
);
300 if(bErg
&& Controller().Is())
301 Controller()->ClearModified();
308 //------------------------------------------------------------------------------
309 String
OTableGrantControl::GetCellText( long nRow
, USHORT nColId
) const
311 DBG_CHKTHIS(OTableGrantControl
,NULL
);
312 if(COL_TABLE_NAME
== nColId
)
313 return m_aTableNames
[nRow
];
316 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
317 if(aFind
!= m_aPrivMap
.end())
318 nPriv
= aFind
->second
.nRights
;
320 return String::CreateFromInt32(isAllowed(nColId
,nPriv
) ? 1 :0);
323 //------------------------------------------------------------------------------
324 void OTableGrantControl::InitController( CellControllerRef
& /*rController*/, long nRow
, USHORT nColumnId
)
326 DBG_CHKTHIS(OTableGrantControl
,NULL
);
327 String sTablename
= m_aTableNames
[nRow
];
328 // special case for tablename
329 if(nColumnId
== COL_TABLE_NAME
)
330 m_pEdit
->SetText(sTablename
);
333 // get the privileges from the user
334 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
335 m_pCheckCell
->GetBox().Check(aFind
!= m_aPrivMap
.end() ? isAllowed(nColumnId
,aFind
->second
.nRights
) : FALSE
);
338 // -----------------------------------------------------------------------------
339 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow
) const
342 if ( m_xUsers
->hasByName(m_sUserName
) )
346 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
349 // get the privileges
351 nRights
.nRights
= xAuth
->getPrivileges(m_aTableNames
[_nRow
],PrivilegeObject::TABLE
);
352 if(m_xGrantUser
.is())
353 nRights
.nWithGrant
= m_xGrantUser
->getGrantablePrivileges(m_aTableNames
[_nRow
],PrivilegeObject::TABLE
);
355 nRights
.nWithGrant
= 0;
357 m_aPrivMap
[m_aTableNames
[_nRow
]] = nRights
;
360 catch(SQLException
& e
)
362 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xORB
);
369 // -----------------------------------------------------------------------------
370 sal_Bool
OTableGrantControl::isAllowed(USHORT _nColumnId
,sal_Int32 _nPrivilege
) const
372 sal_Bool bAllowed
= sal_False
;
376 bAllowed
= (Privilege::INSERT
& _nPrivilege
) == Privilege::INSERT
;
379 bAllowed
= (Privilege::DELETE
& _nPrivilege
) == Privilege::DELETE
;
382 bAllowed
= (Privilege::UPDATE
& _nPrivilege
) == Privilege::UPDATE
;
385 bAllowed
= (Privilege::ALTER
& _nPrivilege
) == Privilege::ALTER
;
388 bAllowed
= (Privilege::SELECT
& _nPrivilege
) == Privilege::SELECT
;
391 bAllowed
= (Privilege::REFERENCE
& _nPrivilege
) == Privilege::REFERENCE
;
394 bAllowed
= (Privilege::DROP
& _nPrivilege
) == Privilege::DROP
;
399 // -----------------------------------------------------------------------------
400 void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName
)
402 m_sUserName
= _sUserName
;
403 m_aPrivMap
= TTablePrivilegeMap();
405 // -----------------------------------------------------------------------------
406 void OTableGrantControl::setGrantUser(const Reference
< XAuthorizable
>& _xGrantUser
)
408 OSL_ENSURE(_xGrantUser
.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
409 m_xGrantUser
= _xGrantUser
;
411 //------------------------------------------------------------------------------
412 CellController
* OTableGrantControl::GetController( long nRow
, USHORT nColumnId
)
414 DBG_CHKTHIS(OTableGrantControl
,NULL
);
416 CellController
* pController
= NULL
;
429 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
430 if(aFind
!= m_aPrivMap
.end() && isAllowed(nColumnId
,aFind
->second
.nWithGrant
))
431 pController
= new CheckBoxCellController( m_pCheckCell
);
439 //------------------------------------------------------------------------------
440 BOOL
OTableGrantControl::SeekRow( long nRow
)
442 DBG_CHKTHIS(OTableGrantControl
,NULL
);
445 return (nRow
<= m_aTableNames
.getLength());
448 //------------------------------------------------------------------------------
449 void OTableGrantControl::PaintCell( OutputDevice
& rDev
, const Rectangle
& rRect
, USHORT nColumnId
) const
451 DBG_CHKTHIS(OTableGrantControl
,NULL
);
453 if(nColumnId
!= COL_TABLE_NAME
)
455 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(m_nDataPos
);
456 if(aFind
!= m_aPrivMap
.end())
457 PaintTristate(rDev
, rRect
, isAllowed(nColumnId
,aFind
->second
.nRights
) ? STATE_CHECK
: STATE_NOCHECK
,isAllowed(nColumnId
,aFind
->second
.nWithGrant
));
459 PaintTristate(rDev
, rRect
, STATE_NOCHECK
,FALSE
);
463 String
aText(((OTableGrantControl
*)this)->GetCellText( m_nDataPos
, nColumnId
));
464 Point
aPos( rRect
.TopLeft() );
465 sal_Int32 nWidth
= GetDataWindow().GetTextWidth( aText
);
466 sal_Int32 nHeight
= GetDataWindow().GetTextHeight();
468 if( aPos
.X() < rRect
.Right() || aPos
.X() + nWidth
> rRect
.Right() ||
469 aPos
.Y() < rRect
.Top() || aPos
.Y() + nHeight
> rRect
.Bottom() )
470 rDev
.SetClipRegion( rRect
);
472 rDev
.DrawText( aPos
, aText
);
475 if( rDev
.IsClipRegion() )
476 rDev
.SetClipRegion();
479 //------------------------------------------------------------------------
480 void OTableGrantControl::CellModified()
482 DBG_CHKTHIS(OTableGrantControl
,NULL
);
483 EditBrowseBox::CellModified();
486 // -----------------------------------------------------------------------------
487 OTableGrantControl::TTablePrivilegeMap::const_iterator
OTableGrantControl::findPrivilege(sal_Int32 _nRow
) const
489 TTablePrivilegeMap::const_iterator aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
490 if(aFind
== m_aPrivMap
.end())
492 fillPrivilege(_nRow
);
493 aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
497 // -----------------------------------------------------------------------------
498 Reference
< XAccessible
> OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow
, sal_uInt16 _nColumnPos
)
500 USHORT nColumnId
= GetColumnId( _nColumnPos
);
501 if(nColumnId
!= COL_TABLE_NAME
)
503 TriState eState
= STATE_NOCHECK
;
504 BOOL bEnable
= FALSE
;
505 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(_nRow
);
506 if(aFind
!= m_aPrivMap
.end())
508 eState
= isAllowed(nColumnId
,aFind
->second
.nRights
) ? STATE_CHECK
: STATE_NOCHECK
;
509 bEnable
= isAllowed(nColumnId
,aFind
->second
.nWithGrant
);
512 eState
= STATE_NOCHECK
;
514 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow
, _nColumnPos
,eState
,bEnable
);
516 return EditBrowseBox::CreateAccessibleCell( _nRow
, _nColumnPos
);
518 // -----------------------------------------------------------------------------