xref: /trunk/main/dbaccess/source/ui/control/TableGrantCtrl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
30*cdf0e10cSrcweir #ifndef DBAUI_TABLEGRANTCONTROL_HXX
31*cdf0e10cSrcweir #include "TableGrantCtrl.hxx"
32*cdf0e10cSrcweir #endif
33*cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
37*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGE_HPP_
40*cdf0e10cSrcweir #include <com/sun/star/sdbcx/Privilege.hpp>
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGEOBJECT_HPP_
43*cdf0e10cSrcweir #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XUSERSSUPPLIER_HPP_
46*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XAUTHORIZABLE_HPP_
49*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAuthorizable.hpp>
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir #ifndef _DBU_CONTROL_HRC_
52*cdf0e10cSrcweir #include "dbu_control.hrc"
53*cdf0e10cSrcweir #endif
54*cdf0e10cSrcweir #ifndef DBAUI_TOOLS_HXX
55*cdf0e10cSrcweir #include "UITools.hxx"
56*cdf0e10cSrcweir #endif
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir using namespace ::com::sun::star::accessibility;
59*cdf0e10cSrcweir using namespace ::com::sun::star::container;
60*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
61*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
62*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
63*cdf0e10cSrcweir using namespace ::dbaui;
64*cdf0e10cSrcweir using namespace ::svt;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir const sal_uInt16 COL_TABLE_NAME = 1;
67*cdf0e10cSrcweir const sal_uInt16 COL_SELECT     = 2;
68*cdf0e10cSrcweir const sal_uInt16 COL_INSERT     = 3;
69*cdf0e10cSrcweir const sal_uInt16 COL_DELETE     = 4;
70*cdf0e10cSrcweir const sal_uInt16 COL_UPDATE     = 5;
71*cdf0e10cSrcweir const sal_uInt16 COL_ALTER      = 6;
72*cdf0e10cSrcweir const sal_uInt16 COL_REF        = 7;
73*cdf0e10cSrcweir const sal_uInt16 COL_DROP       = 8;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir DBG_NAME(OTableGrantControl)
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir //================================================================================
78*cdf0e10cSrcweir // OTableGrantControl
79*cdf0e10cSrcweir //================================================================================
80*cdf0e10cSrcweir OTableGrantControl::OTableGrantControl( Window* pParent,const ResId& _RsId)
81*cdf0e10cSrcweir     :EditBrowseBox( pParent,_RsId, EBBF_SMART_TAB_TRAVEL | EBBF_NOROWPICTURE )
82*cdf0e10cSrcweir     ,m_pCheckCell( NULL )
83*cdf0e10cSrcweir     ,m_pEdit( NULL )
84*cdf0e10cSrcweir     ,m_nDataPos( 0 )
85*cdf0e10cSrcweir     ,m_bEnable(sal_True)
86*cdf0e10cSrcweir     ,m_nDeactivateEvent(0)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     DBG_CTOR(OTableGrantControl,NULL);
89*cdf0e10cSrcweir     //////////////////////////////////////////////////////////////////////
90*cdf0e10cSrcweir     // Spalten einfuegen
91*cdf0e10cSrcweir     sal_uInt16 i=1;
92*cdf0e10cSrcweir     InsertDataColumn( i, String(ModuleRes(STR_TABLE_PRIV_NAME)  ), 75);
93*cdf0e10cSrcweir     FreezeColumn(i++);
94*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_SELECT)), 75);
95*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_INSERT)), 75);
96*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DELETE)), 75);
97*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_UPDATE)), 75);
98*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_ALTER)), 75);
99*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE)), 75);
100*cdf0e10cSrcweir     InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DROP)), 75);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     while(--i)
103*cdf0e10cSrcweir         SetColumnWidth(i,GetAutoColumnWidth(i));
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir //------------------------------------------------------------------------
107*cdf0e10cSrcweir OTableGrantControl::~OTableGrantControl()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     DBG_DTOR(OTableGrantControl,NULL);
110*cdf0e10cSrcweir     if (m_nDeactivateEvent)
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         Application::RemoveUserEvent(m_nDeactivateEvent);
113*cdf0e10cSrcweir         m_nDeactivateEvent = 0;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     delete m_pCheckCell;
117*cdf0e10cSrcweir     delete m_pEdit;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     m_xTables       = NULL;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir // -----------------------------------------------------------------------------
122*cdf0e10cSrcweir void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     // first we need the users
125*cdf0e10cSrcweir     Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
126*cdf0e10cSrcweir     if(xUserSup.is())
127*cdf0e10cSrcweir         m_xUsers = xUserSup->getUsers();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     // second we need the tables to determine which privileges the user has
130*cdf0e10cSrcweir     if(_xTablesSup.is())
131*cdf0e10cSrcweir         m_xTables = _xTablesSup->getTables();
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     if(m_xTables.is())
134*cdf0e10cSrcweir         m_aTableNames = m_xTables->getElementNames();
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     OSL_ENSURE(m_xUsers.is(),"No user access supported!");
137*cdf0e10cSrcweir     OSL_ENSURE(m_xTables.is(),"No tables supported!");
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir // -----------------------------------------------------------------------------
140*cdf0e10cSrcweir void OTableGrantControl::setORB(const Reference< ::com::sun::star::lang::XMultiServiceFactory>& _xORB)
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir     m_xORB = _xORB;
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir //------------------------------------------------------------------------
145*cdf0e10cSrcweir void OTableGrantControl::UpdateTables()
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir     RemoveRows();
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     if(m_xTables.is())
150*cdf0e10cSrcweir         RowInserted(0, m_aTableNames.getLength());
151*cdf0e10cSrcweir     //  m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir //------------------------------------------------------------------------
154*cdf0e10cSrcweir void OTableGrantControl::Init()
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
157*cdf0e10cSrcweir     EditBrowseBox::Init();
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     //////////////////////////////////////////////////////////////////////
160*cdf0e10cSrcweir     // ComboBox instanzieren
161*cdf0e10cSrcweir     if(!m_pCheckCell)
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         m_pCheckCell    = new CheckBoxControl( &GetDataWindow() );
164*cdf0e10cSrcweir         m_pCheckCell->GetBox().EnableTriState(sal_False);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         m_pEdit         = new Edit( &GetDataWindow() );
167*cdf0e10cSrcweir         m_pEdit->SetReadOnly();
168*cdf0e10cSrcweir         m_pEdit->Enable(sal_False);
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     UpdateTables();
172*cdf0e10cSrcweir     //////////////////////////////////////////////////////////////////////
173*cdf0e10cSrcweir     // Browser Mode setzen
174*cdf0e10cSrcweir     BrowserMode nMode = BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL |
175*cdf0e10cSrcweir                         BROWSER_HIDECURSOR      | BROWSER_HIDESELECT;
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     SetMode(nMode);
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir //------------------------------------------------------------------------------
181*cdf0e10cSrcweir void OTableGrantControl::Resize()
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
184*cdf0e10cSrcweir     EditBrowseBox::Resize();
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir //------------------------------------------------------------------------------
188*cdf0e10cSrcweir long OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
191*cdf0e10cSrcweir     if (rNEvt.GetType() == EVENT_LOSEFOCUS)
192*cdf0e10cSrcweir         if (!HasChildPathFocus())
193*cdf0e10cSrcweir         {
194*cdf0e10cSrcweir             if (m_nDeactivateEvent)
195*cdf0e10cSrcweir                 Application::RemoveUserEvent(m_nDeactivateEvent);
196*cdf0e10cSrcweir             m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate));
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir     if (rNEvt.GetType() == EVENT_GETFOCUS)
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         if (m_nDeactivateEvent)
201*cdf0e10cSrcweir             Application::RemoveUserEvent(m_nDeactivateEvent);
202*cdf0e10cSrcweir         m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate));
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir     return EditBrowseBox::PreNotify(rNEvt);
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir //------------------------------------------------------------------------------
208*cdf0e10cSrcweir IMPL_LINK(OTableGrantControl, AsynchActivate, void*, EMPTYARG)
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir     m_nDeactivateEvent = 0;
211*cdf0e10cSrcweir     ActivateCell();
212*cdf0e10cSrcweir     return 0L;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir //------------------------------------------------------------------------------
216*cdf0e10cSrcweir IMPL_LINK(OTableGrantControl, AsynchDeactivate, void*, EMPTYARG)
217*cdf0e10cSrcweir {
218*cdf0e10cSrcweir     m_nDeactivateEvent = 0;
219*cdf0e10cSrcweir     DeactivateCell();
220*cdf0e10cSrcweir     return 0L;
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir //------------------------------------------------------------------------------
224*cdf0e10cSrcweir sal_Bool OTableGrantControl::IsTabAllowed(sal_Bool bForward) const
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
227*cdf0e10cSrcweir     long nRow = GetCurRow();
228*cdf0e10cSrcweir     sal_uInt16 nCol = GetCurColumnId();
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
231*cdf0e10cSrcweir         return sal_False;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     if (!bForward && (nCol == 1) && (nRow == 0))
234*cdf0e10cSrcweir         return sal_False;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     return EditBrowseBox::IsTabAllowed(bForward);
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir //------------------------------------------------------------------------------
239*cdf0e10cSrcweir #define GRANT_REVOKE_RIGHT(what)                \
240*cdf0e10cSrcweir     if(m_pCheckCell->GetBox().IsChecked())      \
241*cdf0e10cSrcweir         xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
242*cdf0e10cSrcweir     else                                        \
243*cdf0e10cSrcweir         xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir //------------------------------------------------------------------------------
246*cdf0e10cSrcweir sal_Bool OTableGrantControl::SaveModified()
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     sal_Int32 nRow = GetCurRow();
251*cdf0e10cSrcweir     if(nRow == -1 || nRow >= m_aTableNames.getLength())
252*cdf0e10cSrcweir         return sal_False;
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     ::rtl::OUString sTableName = m_aTableNames[nRow];
255*cdf0e10cSrcweir     sal_Bool bErg = sal_True;
256*cdf0e10cSrcweir     try
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         if ( m_xUsers->hasByName(m_sUserName) )
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
262*cdf0e10cSrcweir             if ( xAuth.is() )
263*cdf0e10cSrcweir             {
264*cdf0e10cSrcweir                 switch( GetCurColumnId() )
265*cdf0e10cSrcweir                 {
266*cdf0e10cSrcweir                     case COL_INSERT:
267*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::INSERT);
268*cdf0e10cSrcweir                         break;
269*cdf0e10cSrcweir                     case COL_DELETE:
270*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::DELETE);
271*cdf0e10cSrcweir                         break;
272*cdf0e10cSrcweir                     case COL_UPDATE:
273*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::UPDATE);
274*cdf0e10cSrcweir                         break;
275*cdf0e10cSrcweir                     case COL_ALTER:
276*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::ALTER);
277*cdf0e10cSrcweir                         break;
278*cdf0e10cSrcweir                     case COL_SELECT:
279*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::SELECT);
280*cdf0e10cSrcweir                         break;
281*cdf0e10cSrcweir                     case COL_REF:
282*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::REFERENCE);
283*cdf0e10cSrcweir                         break;
284*cdf0e10cSrcweir                     case COL_DROP:
285*cdf0e10cSrcweir                         GRANT_REVOKE_RIGHT(Privilege::DROP);
286*cdf0e10cSrcweir                         break;
287*cdf0e10cSrcweir                 }
288*cdf0e10cSrcweir                 fillPrivilege(nRow);
289*cdf0e10cSrcweir             }
290*cdf0e10cSrcweir         }
291*cdf0e10cSrcweir     }
292*cdf0e10cSrcweir     catch(SQLException& e)
293*cdf0e10cSrcweir     {
294*cdf0e10cSrcweir         bErg = sal_False;
295*cdf0e10cSrcweir         ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir     if(bErg && Controller().Is())
298*cdf0e10cSrcweir         Controller()->ClearModified();
299*cdf0e10cSrcweir     if(!bErg)
300*cdf0e10cSrcweir         UpdateTables();
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     return bErg;
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir //------------------------------------------------------------------------------
306*cdf0e10cSrcweir String OTableGrantControl::GetCellText( long nRow, sal_uInt16 nColId ) const
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
309*cdf0e10cSrcweir     if(COL_TABLE_NAME == nColId)
310*cdf0e10cSrcweir         return m_aTableNames[nRow];
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     sal_Int32 nPriv = 0;
313*cdf0e10cSrcweir     TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
314*cdf0e10cSrcweir     if(aFind != m_aPrivMap.end())
315*cdf0e10cSrcweir         nPriv = aFind->second.nRights;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     return String::CreateFromInt32(isAllowed(nColId,nPriv) ? 1 :0);
318*cdf0e10cSrcweir }
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir //------------------------------------------------------------------------------
321*cdf0e10cSrcweir void OTableGrantControl::InitController( CellControllerRef& /*rController*/, long nRow, sal_uInt16 nColumnId )
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
324*cdf0e10cSrcweir     String sTablename = m_aTableNames[nRow];
325*cdf0e10cSrcweir     // special case for tablename
326*cdf0e10cSrcweir     if(nColumnId == COL_TABLE_NAME)
327*cdf0e10cSrcweir         m_pEdit->SetText(sTablename);
328*cdf0e10cSrcweir     else
329*cdf0e10cSrcweir     {
330*cdf0e10cSrcweir         // get the privileges from the user
331*cdf0e10cSrcweir         TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
332*cdf0e10cSrcweir         m_pCheckCell->GetBox().Check(aFind != m_aPrivMap.end() ? isAllowed(nColumnId,aFind->second.nRights) : sal_False);
333*cdf0e10cSrcweir     }
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir // -----------------------------------------------------------------------------
336*cdf0e10cSrcweir void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     if ( m_xUsers->hasByName(m_sUserName) )
340*cdf0e10cSrcweir     {
341*cdf0e10cSrcweir         try
342*cdf0e10cSrcweir         {
343*cdf0e10cSrcweir             Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
344*cdf0e10cSrcweir             if ( xAuth.is() )
345*cdf0e10cSrcweir             {
346*cdf0e10cSrcweir                 // get the privileges
347*cdf0e10cSrcweir                 TPrivileges nRights;
348*cdf0e10cSrcweir                 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
349*cdf0e10cSrcweir                 if(m_xGrantUser.is())
350*cdf0e10cSrcweir                     nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
351*cdf0e10cSrcweir                 else
352*cdf0e10cSrcweir                     nRights.nWithGrant = 0;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir                 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
355*cdf0e10cSrcweir             }
356*cdf0e10cSrcweir         }
357*cdf0e10cSrcweir         catch(SQLException& e)
358*cdf0e10cSrcweir         {
359*cdf0e10cSrcweir             ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
360*cdf0e10cSrcweir         }
361*cdf0e10cSrcweir         catch(Exception& )
362*cdf0e10cSrcweir         {
363*cdf0e10cSrcweir         }
364*cdf0e10cSrcweir     }
365*cdf0e10cSrcweir }
366*cdf0e10cSrcweir // -----------------------------------------------------------------------------
367*cdf0e10cSrcweir sal_Bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege) const
368*cdf0e10cSrcweir {
369*cdf0e10cSrcweir     sal_Bool bAllowed = sal_False;
370*cdf0e10cSrcweir     switch (_nColumnId)
371*cdf0e10cSrcweir     {
372*cdf0e10cSrcweir         case COL_INSERT:
373*cdf0e10cSrcweir             bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
374*cdf0e10cSrcweir             break;
375*cdf0e10cSrcweir         case COL_DELETE:
376*cdf0e10cSrcweir             bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
377*cdf0e10cSrcweir             break;
378*cdf0e10cSrcweir         case COL_UPDATE:
379*cdf0e10cSrcweir             bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
380*cdf0e10cSrcweir             break;
381*cdf0e10cSrcweir         case COL_ALTER:
382*cdf0e10cSrcweir             bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
383*cdf0e10cSrcweir             break;
384*cdf0e10cSrcweir         case COL_SELECT:
385*cdf0e10cSrcweir             bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
386*cdf0e10cSrcweir             break;
387*cdf0e10cSrcweir         case COL_REF:
388*cdf0e10cSrcweir             bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
389*cdf0e10cSrcweir             break;
390*cdf0e10cSrcweir         case COL_DROP:
391*cdf0e10cSrcweir             bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
392*cdf0e10cSrcweir             break;
393*cdf0e10cSrcweir     }
394*cdf0e10cSrcweir     return bAllowed;
395*cdf0e10cSrcweir }
396*cdf0e10cSrcweir // -----------------------------------------------------------------------------
397*cdf0e10cSrcweir void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName)
398*cdf0e10cSrcweir {
399*cdf0e10cSrcweir     m_sUserName = _sUserName;
400*cdf0e10cSrcweir     m_aPrivMap = TTablePrivilegeMap();
401*cdf0e10cSrcweir }
402*cdf0e10cSrcweir // -----------------------------------------------------------------------------
403*cdf0e10cSrcweir void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
404*cdf0e10cSrcweir {
405*cdf0e10cSrcweir     OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
406*cdf0e10cSrcweir     m_xGrantUser = _xGrantUser;
407*cdf0e10cSrcweir }
408*cdf0e10cSrcweir //------------------------------------------------------------------------------
409*cdf0e10cSrcweir CellController* OTableGrantControl::GetController( long nRow, sal_uInt16 nColumnId )
410*cdf0e10cSrcweir {
411*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir     CellController* pController = NULL;
414*cdf0e10cSrcweir     switch( nColumnId )
415*cdf0e10cSrcweir     {
416*cdf0e10cSrcweir         case COL_TABLE_NAME:
417*cdf0e10cSrcweir             break;
418*cdf0e10cSrcweir         case COL_INSERT:
419*cdf0e10cSrcweir         case COL_DELETE:
420*cdf0e10cSrcweir         case COL_UPDATE:
421*cdf0e10cSrcweir         case COL_ALTER:
422*cdf0e10cSrcweir         case COL_SELECT:
423*cdf0e10cSrcweir         case COL_REF:
424*cdf0e10cSrcweir         case COL_DROP:
425*cdf0e10cSrcweir             {
426*cdf0e10cSrcweir                 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
427*cdf0e10cSrcweir                 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
428*cdf0e10cSrcweir                     pController = new CheckBoxCellController( m_pCheckCell );
429*cdf0e10cSrcweir             }
430*cdf0e10cSrcweir             break;
431*cdf0e10cSrcweir         default:
432*cdf0e10cSrcweir             ;
433*cdf0e10cSrcweir     }
434*cdf0e10cSrcweir     return pController;
435*cdf0e10cSrcweir }
436*cdf0e10cSrcweir //------------------------------------------------------------------------------
437*cdf0e10cSrcweir sal_Bool OTableGrantControl::SeekRow( long nRow )
438*cdf0e10cSrcweir {
439*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
440*cdf0e10cSrcweir     m_nDataPos = nRow;
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir     return (nRow <= m_aTableNames.getLength());
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir //------------------------------------------------------------------------------
446*cdf0e10cSrcweir void OTableGrantControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, sal_uInt16 nColumnId ) const
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir     if(nColumnId != COL_TABLE_NAME)
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
453*cdf0e10cSrcweir         if(aFind != m_aPrivMap.end())
454*cdf0e10cSrcweir             PaintTristate(rDev, rRect, isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK,isAllowed(nColumnId,aFind->second.nWithGrant));
455*cdf0e10cSrcweir         else
456*cdf0e10cSrcweir             PaintTristate(rDev, rRect, STATE_NOCHECK,sal_False);
457*cdf0e10cSrcweir     }
458*cdf0e10cSrcweir     else
459*cdf0e10cSrcweir     {
460*cdf0e10cSrcweir         String aText(((OTableGrantControl*)this)->GetCellText( m_nDataPos, nColumnId ));
461*cdf0e10cSrcweir         Point aPos( rRect.TopLeft() );
462*cdf0e10cSrcweir         sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
463*cdf0e10cSrcweir         sal_Int32 nHeight = GetDataWindow().GetTextHeight();
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir         if( aPos.X() < rRect.Right() || aPos.X() + nWidth > rRect.Right() ||
466*cdf0e10cSrcweir             aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
467*cdf0e10cSrcweir             rDev.SetClipRegion( rRect );
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir         rDev.DrawText( aPos, aText );
470*cdf0e10cSrcweir     }
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir     if( rDev.IsClipRegion() )
473*cdf0e10cSrcweir         rDev.SetClipRegion();
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir //------------------------------------------------------------------------
477*cdf0e10cSrcweir void OTableGrantControl::CellModified()
478*cdf0e10cSrcweir {
479*cdf0e10cSrcweir     DBG_CHKTHIS(OTableGrantControl,NULL);
480*cdf0e10cSrcweir     EditBrowseBox::CellModified();
481*cdf0e10cSrcweir     SaveModified();
482*cdf0e10cSrcweir }
483*cdf0e10cSrcweir // -----------------------------------------------------------------------------
484*cdf0e10cSrcweir OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
485*cdf0e10cSrcweir {
486*cdf0e10cSrcweir     TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
487*cdf0e10cSrcweir     if(aFind == m_aPrivMap.end())
488*cdf0e10cSrcweir     {
489*cdf0e10cSrcweir         fillPrivilege(_nRow);
490*cdf0e10cSrcweir         aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
491*cdf0e10cSrcweir     }
492*cdf0e10cSrcweir     return aFind;
493*cdf0e10cSrcweir }
494*cdf0e10cSrcweir // -----------------------------------------------------------------------------
495*cdf0e10cSrcweir Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
496*cdf0e10cSrcweir {
497*cdf0e10cSrcweir     sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
498*cdf0e10cSrcweir     if(nColumnId != COL_TABLE_NAME)
499*cdf0e10cSrcweir     {
500*cdf0e10cSrcweir         TriState eState = STATE_NOCHECK;
501*cdf0e10cSrcweir         sal_Bool bEnable = sal_False;
502*cdf0e10cSrcweir         TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
503*cdf0e10cSrcweir         if(aFind != m_aPrivMap.end())
504*cdf0e10cSrcweir         {
505*cdf0e10cSrcweir             eState = isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK;
506*cdf0e10cSrcweir             bEnable = isAllowed(nColumnId,aFind->second.nWithGrant);
507*cdf0e10cSrcweir         }
508*cdf0e10cSrcweir         else
509*cdf0e10cSrcweir             eState = STATE_NOCHECK;
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir         return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState,bEnable );
512*cdf0e10cSrcweir     }
513*cdf0e10cSrcweir     return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
514*cdf0e10cSrcweir }
515*cdf0e10cSrcweir // -----------------------------------------------------------------------------
516*cdf0e10cSrcweir 
517