xref: /aoo41x/main/dbaccess/source/ui/dlg/UserAdmin.cxx (revision cdf0e10c)
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 
31 #ifndef DBAUI_USERADMIN_HXX
32 #include "UserAdmin.hxx"
33 #endif
34 #ifndef DBAUI_USERADMIN_HRC
35 #include "UserAdmin.hrc"
36 #endif
37 #ifndef DBAUI_TOOLS_HXX
38 #include "UITools.hxx"
39 #endif
40 #ifndef _DBU_DLG_HRC_
41 #include "dbu_dlg.hrc"
42 #endif
43 #ifndef _COMPHELPER_TYPES_HXX_
44 #include <comphelper/types.hxx>
45 #endif
46 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
47 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBCX_XDATADEFINITIONSUPPLIER_HPP_
50 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDBCX_XUSERSSUPPLIER_HPP_
53 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_SDBCX_XDROP_HPP_
56 #include <com/sun/star/sdbcx/XDrop.hpp>
57 #endif
58 #ifndef _UCBHELPER_INTERATIONREQUEST_HXX
59 #include <ucbhelper/interactionrequest.hxx>
60 #endif
61 #ifndef _UCBHELPER_SIMPLEAUTHENTICATIONREQUEST_HXX
62 #include <ucbhelper/simpleauthenticationrequest.hxx>
63 #endif
64 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
65 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
68 #include <com/sun/star/beans/XPropertySet.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_SDBCX_XUSER_HPP_
71 #include <com/sun/star/sdbcx/XUser.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
74 #include <com/sun/star/sdbcx/XAppend.hpp>
75 #endif
76 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
77 #include "dbustrings.hrc"
78 #endif
79 #ifndef _TOOLS_DEBUG_HXX
80 #include <tools/debug.hxx>
81 #endif
82 #ifndef _DBAUI_DBADMIN_HXX_
83 #include "dbadmin.hxx"
84 #endif
85 #ifndef _DBAUI_MODULE_DBU_HXX_
86 #include "moduledbu.hxx"
87 #endif
88 #ifndef _SV_MSGBOX_HXX
89 #include <vcl/msgbox.hxx>
90 #endif
91 #ifndef _SFX_PASSWD_HXX
92 #include <sfx2/passwd.hxx>
93 #endif
94 
95 using namespace ::com::sun::star::container;
96 using namespace ::com::sun::star::beans;
97 using namespace ::com::sun::star::sdbcx;
98 using namespace ::com::sun::star::sdbc;
99 using namespace ::com::sun::star::uno;
100 using namespace ::com::sun::star::task;
101 using namespace dbaui;
102 using namespace ucbhelper;
103 using namespace comphelper;
104 
105 
106 class OPasswordDialog : public ModalDialog
107 {
108 	FixedLine		aFLUser;
109 	FixedText		aFTOldPassword;
110 	Edit			aEDOldPassword;
111 	FixedText		aFTPassword;
112 	Edit			aEDPassword;
113 	FixedText		aFTPasswordRepeat;
114 	Edit			aEDPasswordRepeat;
115 	OKButton		aOKBtn;
116 	CancelButton	aCancelBtn;
117 	HelpButton		aHelpBtn;
118 
119 
120 	DECL_LINK( OKHdl_Impl, OKButton * );
121 	DECL_LINK( ModifiedHdl, Edit * );
122 
123 public:
124 	OPasswordDialog( Window* pParent,const String& _sUserName);
125 
126 	String			GetOldPassword() const { return aEDOldPassword.GetText(); }
127 	String			GetNewPassword() const { return aEDPassword.GetText(); }
128 };
129 
130 OPasswordDialog::OPasswordDialog(Window* _pParent,const String& _sUserName) :
131 
132 	ModalDialog( _pParent, ModuleRes( DLG_PASSWORD) ),
133 
134 	aFLUser				( this, ModuleRes( FL_USER ) ),
135 	aFTOldPassword		( this, ModuleRes( FT_OLDPASSWORD ) ),
136 	aEDOldPassword		( this, ModuleRes( ED_OLDPASSWORD ) ),
137 	aFTPassword			( this, ModuleRes( FT_PASSWORD ) ),
138 	aEDPassword			( this, ModuleRes( ED_PASSWORD ) ),
139 	aFTPasswordRepeat	( this, ModuleRes( FT_PASSWORD_REPEAT ) ),
140 	aEDPasswordRepeat	( this, ModuleRes( ED_PASSWORD_REPEAT ) ),
141 	aOKBtn				( this, ModuleRes( BTN_PASSWORD_OK ) ),
142 	aCancelBtn			( this, ModuleRes( BTN_PASSWORD_CANCEL ) ),
143 	aHelpBtn			( this, ModuleRes( BTN_PASSWORD_HELP ) )
144 {
145 	// hide until a help is avalable
146 	aHelpBtn.Hide();
147 
148 	FreeResource();
149 	String sUser = aFLUser.GetText();
150 	sUser.SearchAndReplaceAscii("$name$:  $",_sUserName);
151 	aFLUser.SetText(sUser);
152 	aOKBtn.Disable();
153 
154 	aOKBtn.SetClickHdl( LINK( this, OPasswordDialog, OKHdl_Impl ) );
155 	aEDOldPassword.SetModifyHdl( LINK( this, OPasswordDialog, ModifiedHdl ) );
156 }
157 // -----------------------------------------------------------------------------
158 IMPL_LINK( OPasswordDialog, OKHdl_Impl, OKButton *, EMPTYARG )
159 {
160 	if( aEDPassword.GetText() == aEDPasswordRepeat.GetText() )
161 		EndDialog( RET_OK );
162 	else
163 	{
164 		String aErrorMsg( ModuleRes( STR_ERROR_PASSWORDS_NOT_IDENTICAL));
165 		ErrorBox aErrorBox( this, WB_OK, aErrorMsg );
166 		aErrorBox.Execute();
167 		aEDPassword.SetText( String() );
168 		aEDPasswordRepeat.SetText( String() );
169 		aEDPassword.GrabFocus();
170 	}
171 	return 0;
172 }
173 // -----------------------------------------------------------------------------
174 IMPL_LINK( OPasswordDialog, ModifiedHdl, Edit *, pEdit )
175 {
176 	aOKBtn.Enable(pEdit->GetText().Len() != 0);
177 	return 0;
178 }
179 
180 DBG_NAME(OUserAdmin);
181 //================================================================================
182 // OUserAdmin
183 //================================================================================
184 OUserAdmin::OUserAdmin(Window* pParent,const SfxItemSet& _rAttrSet)
185 	: OGenericAdministrationPage( pParent, ModuleRes(TAB_PAGE_USERADMIN), _rAttrSet)
186 	,m_FL_USER(			this , ModuleRes(FL_USER))
187 	,m_FT_USER(			this , ModuleRes(FT_USER))
188 	,m_LB_USER(			this , ModuleRes(LB_USER))
189 	,m_PB_NEWUSER(		this , ModuleRes(PB_NEWUSER))
190 	,m_PB_CHANGEPWD(	this , ModuleRes(PB_CHANGEPWD))
191 	,m_PB_DELETEUSER(	this , ModuleRes(PB_DELETEUSER))
192 	,m_FL_TABLE_GRANTS( this , ModuleRes(FL_TABLE_GRANTS))
193 	,m_TableCtrl(		this , ModuleRes(CTRL_TABLE_GRANTS))
194 {
195 	DBG_CTOR(OUserAdmin,NULL);
196 	m_LB_USER.SetSelectHdl(LINK(this, OUserAdmin, ListDblClickHdl));
197 
198 	m_PB_NEWUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
199 	m_PB_CHANGEPWD.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
200 	m_PB_DELETEUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
201 
202 	FreeResource();
203 }
204 // -----------------------------------------------------------------------
205 OUserAdmin::~OUserAdmin()
206 {
207 	DBG_DTOR(OUserAdmin,NULL);
208 	m_xConnection = NULL;
209 }
210 // -----------------------------------------------------------------------
211 void OUserAdmin::FillUserNames()
212 {
213 	if(m_xConnection.is())
214 	{
215 		m_LB_USER.Clear();
216 
217 		Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
218 
219 		if ( xMetaData.is() )
220 		{
221 			m_UserName = xMetaData->getUserName();
222 
223 			// first we need the users
224 			if ( m_xUsers.is() )
225 			{
226 				m_LB_USER.Clear();
227 
228 				m_aUserNames = m_xUsers->getElementNames();
229 				const ::rtl::OUString* pBegin = m_aUserNames.getConstArray();
230 				const ::rtl::OUString* pEnd	  = pBegin + m_aUserNames.getLength();
231 				::rtl::OUString sUserName = m_UserName;
232 				for(;pBegin != pEnd;++pBegin)
233 					m_LB_USER.InsertEntry(*pBegin);
234 
235 				m_LB_USER.SelectEntryPos(0);
236 				if(m_xUsers->hasByName(m_UserName))
237 				{
238 					Reference<XAuthorizable> xAuth;
239 					m_xUsers->getByName(m_UserName) >>= xAuth;
240 					m_TableCtrl.setGrantUser(xAuth);
241 				}
242 
243 				m_TableCtrl.setUserName(GetUser());
244 				m_TableCtrl.Init();
245 			}
246 		}
247 	}
248 
249 	Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
250 	m_PB_NEWUSER.Enable(xAppend.is());
251 	Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
252 	m_PB_DELETEUSER.Enable(xDrop.is());
253 
254 	m_PB_CHANGEPWD.Enable(m_xUsers.is());
255 	m_TableCtrl.Enable(m_xUsers.is());
256 
257 }
258 // -----------------------------------------------------------------------
259 SfxTabPage*	OUserAdmin::Create( Window* pParent, const SfxItemSet& _rAttrSet )
260 {
261 	return ( new OUserAdmin( pParent, _rAttrSet ) );
262 }
263 // -----------------------------------------------------------------------
264 IMPL_LINK( OUserAdmin, UserHdl, PushButton *, pButton )
265 {
266 	try
267 	{
268 		if(pButton == &m_PB_NEWUSER)
269 		{
270 			SfxPasswordDialog aPwdDlg(this);
271 			aPwdDlg.ShowExtras(SHOWEXTRAS_ALL);
272 			if(aPwdDlg.Execute())
273 			{
274 				Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
275 				Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
276 				if(xNewUser.is())
277 				{
278 					xNewUser->setPropertyValue(PROPERTY_NAME,makeAny(rtl::OUString(aPwdDlg.GetUser())));
279 					xNewUser->setPropertyValue(PROPERTY_PASSWORD,makeAny(rtl::OUString(aPwdDlg.GetPassword())));
280 					Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
281 					if(xAppend.is())
282 						xAppend->appendByDescriptor(xNewUser);
283 				}
284 			}
285 		}
286 		else if(pButton == &m_PB_CHANGEPWD)
287 		{
288 			String sName = GetUser();
289 
290 			if(m_xUsers->hasByName(sName))
291 			{
292 				Reference<XUser> xUser;
293 				m_xUsers->getByName(sName) >>= xUser;
294 				if(xUser.is())
295 				{
296 					::rtl::OUString sNewPassword,sOldPassword;
297 					OPasswordDialog aDlg(this,sName);
298 					if(aDlg.Execute() == RET_OK)
299 					{
300 						sNewPassword = aDlg.GetNewPassword();
301 						sOldPassword = aDlg.GetOldPassword();
302 
303 						if(sNewPassword.getLength())
304 							xUser->changePassword(sOldPassword,sNewPassword);
305 					}
306 				}
307 			}
308 		}
309 		else
310 		{// delete user
311 			if(m_xUsers.is() && m_xUsers->hasByName(GetUser()))
312 			{
313 				Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
314 				if(xDrop.is())
315 				{
316 					QueryBox aQry(this, ModuleRes(QUERY_USERADMIN_DELETE_USER));
317 					if(aQry.Execute() == RET_YES)
318 						xDrop->dropByName(GetUser());
319 				}
320 			}
321 		}
322 		FillUserNames();
323 	}
324 	catch(SQLException& e)
325 	{
326 		::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
327 		return 0;
328 	}
329 	catch(Exception& )
330 	{
331 		return 0;
332 	}
333 
334 	return 0;
335 }
336 // -----------------------------------------------------------------------
337 IMPL_LINK( OUserAdmin, ListDblClickHdl, ListBox *, /*pListBox*/ )
338 {
339 	m_TableCtrl.setUserName(GetUser());
340 	m_TableCtrl.UpdateTables();
341 	m_TableCtrl.DeactivateCell();
342 	m_TableCtrl.ActivateCell(m_TableCtrl.GetCurRow(),m_TableCtrl.GetCurColumnId());
343 	return 0;
344 }
345 
346 // -----------------------------------------------------------------------
347 String OUserAdmin::GetUser()
348 {
349 	return m_LB_USER.GetSelectEntry();
350 }
351 // -----------------------------------------------------------------------------
352 void OUserAdmin::fillControls(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
353 {
354 }
355 // -----------------------------------------------------------------------
356 void OUserAdmin::fillWindows(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
357 {
358 }
359 // -----------------------------------------------------------------------------
360 void OUserAdmin::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
361 {
362 	m_TableCtrl.setORB(m_xORB);
363 	try
364 	{
365 		if ( !m_xConnection.is() && m_pAdminDialog )
366 		{
367 			m_xConnection = m_pAdminDialog->createConnection().first;
368 			Reference< XTablesSupplier > xTablesSup(m_xConnection,UNO_QUERY);
369 			Reference<XUsersSupplier> xUsersSup(xTablesSup,UNO_QUERY);
370 			if ( !xUsersSup.is() )
371 			{
372 				Reference< XDataDefinitionSupplier > xDriver(m_pAdminDialog->getDriver(),UNO_QUERY);
373 				if ( xDriver.is() )
374 				{
375 					xUsersSup.set(xDriver->getDataDefinitionByConnection(m_xConnection),UNO_QUERY);
376 					xTablesSup.set(xUsersSup,UNO_QUERY);
377 				}
378 			}
379 			if ( xUsersSup.is() )
380 			{
381 				m_TableCtrl.setTablesSupplier(xTablesSup);
382 				m_xUsers = xUsersSup->getUsers();
383 			}
384 		}
385 		FillUserNames();
386 	}
387 	catch(SQLException& e)
388 	{
389 		::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
390 	}
391 
392 	OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
393 }
394