1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "TableGrantCtrl.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 #include <com/sun/star/sdbcx/Privilege.hpp>
33 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
34 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
35 #include <com/sun/star/sdbcx/XAuthorizable.hpp>
36 #include "dbu_control.hrc"
37 #include "UITools.hxx"
39 using namespace ::com::sun::star::accessibility
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::sdbcx
;
42 using namespace ::com::sun::star::sdbc
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::dbaui
;
45 using namespace ::svt
;
47 const sal_uInt16 COL_TABLE_NAME
= 1;
48 const sal_uInt16 COL_SELECT
= 2;
49 const sal_uInt16 COL_INSERT
= 3;
50 const sal_uInt16 COL_DELETE
= 4;
51 const sal_uInt16 COL_UPDATE
= 5;
52 const sal_uInt16 COL_ALTER
= 6;
53 const sal_uInt16 COL_REF
= 7;
54 const sal_uInt16 COL_DROP
= 8;
56 DBG_NAME(OTableGrantControl
)
58 //================================================================================
60 //================================================================================
61 OTableGrantControl::OTableGrantControl( Window
* pParent
,const ResId
& _RsId
)
62 :EditBrowseBox( pParent
,_RsId
, EBBF_SMART_TAB_TRAVEL
| EBBF_NOROWPICTURE
)
67 ,m_nDeactivateEvent(0)
69 DBG_CTOR(OTableGrantControl
,NULL
);
70 //////////////////////////////////////////////////////////////////////
73 InsertDataColumn( i
, String(ModuleRes(STR_TABLE_PRIV_NAME
) ), 75);
75 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_SELECT
)), 75);
76 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_INSERT
)), 75);
77 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_DELETE
)), 75);
78 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_UPDATE
)), 75);
79 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_ALTER
)), 75);
80 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE
)), 75);
81 InsertDataColumn( i
++, String(ModuleRes(STR_TABLE_PRIV_DROP
)), 75);
84 SetColumnWidth(i
,GetAutoColumnWidth(i
));
87 //------------------------------------------------------------------------
88 OTableGrantControl::~OTableGrantControl()
90 DBG_DTOR(OTableGrantControl
,NULL
);
91 if (m_nDeactivateEvent
)
93 Application::RemoveUserEvent(m_nDeactivateEvent
);
94 m_nDeactivateEvent
= 0;
102 // -----------------------------------------------------------------------------
103 void OTableGrantControl::setTablesSupplier(const Reference
< XTablesSupplier
>& _xTablesSup
)
105 // first we need the users
106 Reference
< XUsersSupplier
> xUserSup(_xTablesSup
,UNO_QUERY
);
108 m_xUsers
= xUserSup
->getUsers();
110 // second we need the tables to determine which privileges the user has
112 m_xTables
= _xTablesSup
->getTables();
115 m_aTableNames
= m_xTables
->getElementNames();
117 OSL_ENSURE(m_xUsers
.is(),"No user access supported!");
118 OSL_ENSURE(m_xTables
.is(),"No tables supported!");
120 // -----------------------------------------------------------------------------
121 void OTableGrantControl::setORB(const Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _xORB
)
125 //------------------------------------------------------------------------
126 void OTableGrantControl::UpdateTables()
131 RowInserted(0, m_aTableNames
.getLength());
132 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
134 //------------------------------------------------------------------------
135 void OTableGrantControl::Init()
137 DBG_CHKTHIS(OTableGrantControl
,NULL
);
138 EditBrowseBox::Init();
140 //////////////////////////////////////////////////////////////////////
141 // ComboBox instanzieren
144 m_pCheckCell
= new CheckBoxControl( &GetDataWindow() );
145 m_pCheckCell
->GetBox().EnableTriState(sal_False
);
147 m_pEdit
= new Edit( &GetDataWindow() );
148 m_pEdit
->SetReadOnly();
149 m_pEdit
->Enable(sal_False
);
153 //////////////////////////////////////////////////////////////////////
154 // Browser Mode setzen
155 BrowserMode nMode
= BROWSER_COLUMNSELECTION
| BROWSER_HLINESFULL
| BROWSER_VLINESFULL
|
156 BROWSER_HIDECURSOR
| BROWSER_HIDESELECT
;
161 //------------------------------------------------------------------------------
162 void OTableGrantControl::Resize()
164 DBG_CHKTHIS(OTableGrantControl
,NULL
);
165 EditBrowseBox::Resize();
168 //------------------------------------------------------------------------------
169 long OTableGrantControl::PreNotify(NotifyEvent
& rNEvt
)
171 DBG_CHKTHIS(OTableGrantControl
,NULL
);
172 if (rNEvt
.GetType() == EVENT_LOSEFOCUS
)
173 if (!HasChildPathFocus())
175 if (m_nDeactivateEvent
)
176 Application::RemoveUserEvent(m_nDeactivateEvent
);
177 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchDeactivate
));
179 if (rNEvt
.GetType() == EVENT_GETFOCUS
)
181 if (m_nDeactivateEvent
)
182 Application::RemoveUserEvent(m_nDeactivateEvent
);
183 m_nDeactivateEvent
= Application::PostUserEvent(LINK(this, OTableGrantControl
, AsynchActivate
));
185 return EditBrowseBox::PreNotify(rNEvt
);
188 //------------------------------------------------------------------------------
189 IMPL_LINK_NOARG(OTableGrantControl
, AsynchActivate
)
191 m_nDeactivateEvent
= 0;
196 //------------------------------------------------------------------------------
197 IMPL_LINK_NOARG(OTableGrantControl
, AsynchDeactivate
)
199 m_nDeactivateEvent
= 0;
204 //------------------------------------------------------------------------------
205 sal_Bool
OTableGrantControl::IsTabAllowed(sal_Bool bForward
) const
207 DBG_CHKTHIS(OTableGrantControl
,NULL
);
208 long nRow
= GetCurRow();
209 sal_uInt16 nCol
= GetCurColumnId();
211 if (bForward
&& (nCol
== 2) && (nRow
== GetRowCount() - 1))
214 if (!bForward
&& (nCol
== 1) && (nRow
== 0))
217 return EditBrowseBox::IsTabAllowed(bForward
);
219 //------------------------------------------------------------------------------
220 #define GRANT_REVOKE_RIGHT(what) \
221 if(m_pCheckCell->GetBox().IsChecked()) \
222 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
224 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
226 //------------------------------------------------------------------------------
227 sal_Bool
OTableGrantControl::SaveModified()
229 DBG_CHKTHIS(OTableGrantControl
,NULL
);
231 sal_Int32 nRow
= GetCurRow();
232 if(nRow
== -1 || nRow
>= m_aTableNames
.getLength())
235 ::rtl::OUString sTableName
= m_aTableNames
[nRow
];
236 sal_Bool bErg
= sal_True
;
240 if ( m_xUsers
->hasByName(m_sUserName
) )
242 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
245 switch( GetCurColumnId() )
248 GRANT_REVOKE_RIGHT(Privilege::INSERT
);
251 GRANT_REVOKE_RIGHT(Privilege::DELETE
);
254 GRANT_REVOKE_RIGHT(Privilege::UPDATE
);
257 GRANT_REVOKE_RIGHT(Privilege::ALTER
);
260 GRANT_REVOKE_RIGHT(Privilege::SELECT
);
263 GRANT_REVOKE_RIGHT(Privilege::REFERENCE
);
266 GRANT_REVOKE_RIGHT(Privilege::DROP
);
273 catch(SQLException
& e
)
276 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xORB
);
278 if(bErg
&& Controller().Is())
279 Controller()->ClearModified();
286 //------------------------------------------------------------------------------
287 String
OTableGrantControl::GetCellText( long nRow
, sal_uInt16 nColId
) const
289 DBG_CHKTHIS(OTableGrantControl
,NULL
);
290 if(COL_TABLE_NAME
== nColId
)
291 return m_aTableNames
[nRow
];
294 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
295 if(aFind
!= m_aPrivMap
.end())
296 nPriv
= aFind
->second
.nRights
;
298 return String::CreateFromInt32(isAllowed(nColId
,nPriv
) ? 1 :0);
301 //------------------------------------------------------------------------------
302 void OTableGrantControl::InitController( CellControllerRef
& /*rController*/, long nRow
, sal_uInt16 nColumnId
)
304 DBG_CHKTHIS(OTableGrantControl
,NULL
);
305 String sTablename
= m_aTableNames
[nRow
];
306 // special case for tablename
307 if(nColumnId
== COL_TABLE_NAME
)
308 m_pEdit
->SetText(sTablename
);
311 // get the privileges from the user
312 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
313 m_pCheckCell
->GetBox().Check(aFind
!= m_aPrivMap
.end() ? isAllowed(nColumnId
,aFind
->second
.nRights
) : sal_False
);
316 // -----------------------------------------------------------------------------
317 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow
) const
320 if ( m_xUsers
->hasByName(m_sUserName
) )
324 Reference
<XAuthorizable
> xAuth(m_xUsers
->getByName(m_sUserName
),UNO_QUERY
);
327 // get the privileges
329 nRights
.nRights
= xAuth
->getPrivileges(m_aTableNames
[_nRow
],PrivilegeObject::TABLE
);
330 if(m_xGrantUser
.is())
331 nRights
.nWithGrant
= m_xGrantUser
->getGrantablePrivileges(m_aTableNames
[_nRow
],PrivilegeObject::TABLE
);
333 nRights
.nWithGrant
= 0;
335 m_aPrivMap
[m_aTableNames
[_nRow
]] = nRights
;
338 catch(SQLException
& e
)
340 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
),GetParent(),m_xORB
);
347 // -----------------------------------------------------------------------------
348 sal_Bool
OTableGrantControl::isAllowed(sal_uInt16 _nColumnId
,sal_Int32 _nPrivilege
) const
350 sal_Bool bAllowed
= sal_False
;
354 bAllowed
= (Privilege::INSERT
& _nPrivilege
) == Privilege::INSERT
;
357 bAllowed
= (Privilege::DELETE
& _nPrivilege
) == Privilege::DELETE
;
360 bAllowed
= (Privilege::UPDATE
& _nPrivilege
) == Privilege::UPDATE
;
363 bAllowed
= (Privilege::ALTER
& _nPrivilege
) == Privilege::ALTER
;
366 bAllowed
= (Privilege::SELECT
& _nPrivilege
) == Privilege::SELECT
;
369 bAllowed
= (Privilege::REFERENCE
& _nPrivilege
) == Privilege::REFERENCE
;
372 bAllowed
= (Privilege::DROP
& _nPrivilege
) == Privilege::DROP
;
377 // -----------------------------------------------------------------------------
378 void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName
)
380 m_sUserName
= _sUserName
;
381 m_aPrivMap
= TTablePrivilegeMap();
383 // -----------------------------------------------------------------------------
384 void OTableGrantControl::setGrantUser(const Reference
< XAuthorizable
>& _xGrantUser
)
386 OSL_ENSURE(_xGrantUser
.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
387 m_xGrantUser
= _xGrantUser
;
389 //------------------------------------------------------------------------------
390 CellController
* OTableGrantControl::GetController( long nRow
, sal_uInt16 nColumnId
)
392 DBG_CHKTHIS(OTableGrantControl
,NULL
);
394 CellController
* pController
= NULL
;
407 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(nRow
);
408 if(aFind
!= m_aPrivMap
.end() && isAllowed(nColumnId
,aFind
->second
.nWithGrant
))
409 pController
= new CheckBoxCellController( m_pCheckCell
);
417 //------------------------------------------------------------------------------
418 sal_Bool
OTableGrantControl::SeekRow( long nRow
)
420 DBG_CHKTHIS(OTableGrantControl
,NULL
);
423 return (nRow
<= m_aTableNames
.getLength());
426 //------------------------------------------------------------------------------
427 void OTableGrantControl::PaintCell( OutputDevice
& rDev
, const Rectangle
& rRect
, sal_uInt16 nColumnId
) const
429 DBG_CHKTHIS(OTableGrantControl
,NULL
);
431 if(nColumnId
!= COL_TABLE_NAME
)
433 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(m_nDataPos
);
434 if(aFind
!= m_aPrivMap
.end())
435 PaintTristate(rDev
, rRect
, isAllowed(nColumnId
,aFind
->second
.nRights
) ? STATE_CHECK
: STATE_NOCHECK
,isAllowed(nColumnId
,aFind
->second
.nWithGrant
));
437 PaintTristate(rDev
, rRect
, STATE_NOCHECK
,sal_False
);
441 String
aText(((OTableGrantControl
*)this)->GetCellText( m_nDataPos
, nColumnId
));
442 Point
aPos( rRect
.TopLeft() );
443 sal_Int32 nWidth
= GetDataWindow().GetTextWidth( aText
);
444 sal_Int32 nHeight
= GetDataWindow().GetTextHeight();
446 if( aPos
.X() < rRect
.Right() || aPos
.X() + nWidth
> rRect
.Right() ||
447 aPos
.Y() < rRect
.Top() || aPos
.Y() + nHeight
> rRect
.Bottom() )
448 rDev
.SetClipRegion( rRect
);
450 rDev
.DrawText( aPos
, aText
);
453 if( rDev
.IsClipRegion() )
454 rDev
.SetClipRegion();
457 //------------------------------------------------------------------------
458 void OTableGrantControl::CellModified()
460 DBG_CHKTHIS(OTableGrantControl
,NULL
);
461 EditBrowseBox::CellModified();
464 // -----------------------------------------------------------------------------
465 OTableGrantControl::TTablePrivilegeMap::const_iterator
OTableGrantControl::findPrivilege(sal_Int32 _nRow
) const
467 TTablePrivilegeMap::const_iterator aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
468 if(aFind
== m_aPrivMap
.end())
470 fillPrivilege(_nRow
);
471 aFind
= m_aPrivMap
.find(m_aTableNames
[_nRow
]);
475 // -----------------------------------------------------------------------------
476 Reference
< XAccessible
> OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow
, sal_uInt16 _nColumnPos
)
478 sal_uInt16 nColumnId
= GetColumnId( _nColumnPos
);
479 if(nColumnId
!= COL_TABLE_NAME
)
481 TriState eState
= STATE_NOCHECK
;
482 sal_Bool bEnable
= sal_False
;
483 TTablePrivilegeMap::const_iterator aFind
= findPrivilege(_nRow
);
484 if(aFind
!= m_aPrivMap
.end())
486 eState
= isAllowed(nColumnId
,aFind
->second
.nRights
) ? STATE_CHECK
: STATE_NOCHECK
;
487 bEnable
= isAllowed(nColumnId
,aFind
->second
.nWithGrant
);
490 eState
= STATE_NOCHECK
;
492 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow
, _nColumnPos
,eState
,bEnable
);
494 return EditBrowseBox::CreateAccessibleCell( _nRow
, _nColumnPos
);
496 // -----------------------------------------------------------------------------
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */