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