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