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