Update ooo320-m1
[ooovba.git] / dbaccess / source / ui / dlg / UserAdmin.cxx
blobc8a86a7b179584a4dee5b9ebabc73e403f147639
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UserAdmin.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBAUI_USERADMIN_HXX
35 #include "UserAdmin.hxx"
36 #endif
37 #ifndef DBAUI_USERADMIN_HRC
38 #include "UserAdmin.hrc"
39 #endif
40 #ifndef DBAUI_TOOLS_HXX
41 #include "UITools.hxx"
42 #endif
43 #ifndef _DBU_DLG_HRC_
44 #include "dbu_dlg.hrc"
45 #endif
46 #ifndef _COMPHELPER_TYPES_HXX_
47 #include <comphelper/types.hxx>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
50 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDBCX_XDATADEFINITIONSUPPLIER_HPP_
53 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_SDBCX_XUSERSSUPPLIER_HPP_
56 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_SDBCX_XDROP_HPP_
59 #include <com/sun/star/sdbcx/XDrop.hpp>
60 #endif
61 #ifndef _UCBHELPER_INTERATIONREQUEST_HXX
62 #include <ucbhelper/interactionrequest.hxx>
63 #endif
64 #ifndef _UCBHELPER_SIMPLEAUTHENTICATIONREQUEST_HXX
65 #include <ucbhelper/simpleauthenticationrequest.hxx>
66 #endif
67 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
68 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
71 #include <com/sun/star/beans/XPropertySet.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_SDBCX_XUSER_HPP_
74 #include <com/sun/star/sdbcx/XUser.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
77 #include <com/sun/star/sdbcx/XAppend.hpp>
78 #endif
79 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
80 #include "dbustrings.hrc"
81 #endif
82 #ifndef _TOOLS_DEBUG_HXX
83 #include <tools/debug.hxx>
84 #endif
85 #ifndef _DBAUI_DBADMIN_HXX_
86 #include "dbadmin.hxx"
87 #endif
88 #ifndef _DBAUI_MODULE_DBU_HXX_
89 #include "moduledbu.hxx"
90 #endif
91 #ifndef _SV_MSGBOX_HXX
92 #include <vcl/msgbox.hxx>
93 #endif
94 #ifndef _SFX_PASSWD_HXX
95 #include <sfx2/passwd.hxx>
96 #endif
98 using namespace ::com::sun::star::container;
99 using namespace ::com::sun::star::beans;
100 using namespace ::com::sun::star::sdbcx;
101 using namespace ::com::sun::star::sdbc;
102 using namespace ::com::sun::star::uno;
103 using namespace ::com::sun::star::task;
104 using namespace dbaui;
105 using namespace ucbhelper;
106 using namespace comphelper;
109 class OPasswordDialog : public ModalDialog
111 FixedLine aFLUser;
112 FixedText aFTOldPassword;
113 Edit aEDOldPassword;
114 FixedText aFTPassword;
115 Edit aEDPassword;
116 FixedText aFTPasswordRepeat;
117 Edit aEDPasswordRepeat;
118 OKButton aOKBtn;
119 CancelButton aCancelBtn;
120 HelpButton aHelpBtn;
123 DECL_LINK( OKHdl_Impl, OKButton * );
124 DECL_LINK( ModifiedHdl, Edit * );
126 public:
127 OPasswordDialog( Window* pParent,const String& _sUserName);
129 String GetOldPassword() const { return aEDOldPassword.GetText(); }
130 String GetNewPassword() const { return aEDPassword.GetText(); }
133 OPasswordDialog::OPasswordDialog(Window* _pParent,const String& _sUserName) :
135 ModalDialog( _pParent, ModuleRes( DLG_PASSWORD) ),
137 aFLUser ( this, ModuleRes( FL_USER ) ),
138 aFTOldPassword ( this, ModuleRes( FT_OLDPASSWORD ) ),
139 aEDOldPassword ( this, ModuleRes( ED_OLDPASSWORD ) ),
140 aFTPassword ( this, ModuleRes( FT_PASSWORD ) ),
141 aEDPassword ( this, ModuleRes( ED_PASSWORD ) ),
142 aFTPasswordRepeat ( this, ModuleRes( FT_PASSWORD_REPEAT ) ),
143 aEDPasswordRepeat ( this, ModuleRes( ED_PASSWORD_REPEAT ) ),
144 aOKBtn ( this, ModuleRes( BTN_PASSWORD_OK ) ),
145 aCancelBtn ( this, ModuleRes( BTN_PASSWORD_CANCEL ) ),
146 aHelpBtn ( this, ModuleRes( BTN_PASSWORD_HELP ) )
148 // hide until a help is avalable
149 aHelpBtn.Hide();
151 FreeResource();
152 String sUser = aFLUser.GetText();
153 sUser.SearchAndReplaceAscii("$name$: $",_sUserName);
154 aFLUser.SetText(sUser);
155 aOKBtn.Disable();
157 aOKBtn.SetClickHdl( LINK( this, OPasswordDialog, OKHdl_Impl ) );
158 aEDOldPassword.SetModifyHdl( LINK( this, OPasswordDialog, ModifiedHdl ) );
160 // -----------------------------------------------------------------------------
161 IMPL_LINK( OPasswordDialog, OKHdl_Impl, OKButton *, EMPTYARG )
163 if( aEDPassword.GetText() == aEDPasswordRepeat.GetText() )
164 EndDialog( RET_OK );
165 else
167 String aErrorMsg( ModuleRes( STR_ERROR_PASSWORDS_NOT_IDENTICAL));
168 ErrorBox aErrorBox( this, WB_OK, aErrorMsg );
169 aErrorBox.Execute();
170 aEDPassword.SetText( String() );
171 aEDPasswordRepeat.SetText( String() );
172 aEDPassword.GrabFocus();
174 return 0;
176 // -----------------------------------------------------------------------------
177 IMPL_LINK( OPasswordDialog, ModifiedHdl, Edit *, pEdit )
179 aOKBtn.Enable(pEdit->GetText().Len() != 0);
180 return 0;
183 DBG_NAME(OUserAdmin);
184 //================================================================================
185 // OUserAdmin
186 //================================================================================
187 OUserAdmin::OUserAdmin(Window* pParent,const SfxItemSet& _rAttrSet)
188 : OGenericAdministrationPage( pParent, ModuleRes(TAB_PAGE_USERADMIN), _rAttrSet)
189 ,m_FL_USER( this , ModuleRes(FL_USER))
190 ,m_FT_USER( this , ModuleRes(FT_USER))
191 ,m_LB_USER( this , ModuleRes(LB_USER))
192 ,m_PB_NEWUSER( this , ModuleRes(PB_NEWUSER))
193 ,m_PB_CHANGEPWD( this , ModuleRes(PB_CHANGEPWD))
194 ,m_PB_DELETEUSER( this , ModuleRes(PB_DELETEUSER))
195 ,m_FL_TABLE_GRANTS( this , ModuleRes(FL_TABLE_GRANTS))
196 ,m_TableCtrl( this , ModuleRes(CTRL_TABLE_GRANTS))
198 DBG_CTOR(OUserAdmin,NULL);
199 m_LB_USER.SetSelectHdl(LINK(this, OUserAdmin, ListDblClickHdl));
201 m_PB_NEWUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
202 m_PB_CHANGEPWD.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
203 m_PB_DELETEUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
205 FreeResource();
207 // -----------------------------------------------------------------------
208 OUserAdmin::~OUserAdmin()
210 DBG_DTOR(OUserAdmin,NULL);
211 m_xConnection = NULL;
213 // -----------------------------------------------------------------------
214 void OUserAdmin::FillUserNames()
216 if(m_xConnection.is())
218 m_LB_USER.Clear();
220 Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
222 if ( xMetaData.is() )
224 m_UserName = xMetaData->getUserName();
226 // first we need the users
227 if ( m_xUsers.is() )
229 m_LB_USER.Clear();
231 m_aUserNames = m_xUsers->getElementNames();
232 const ::rtl::OUString* pBegin = m_aUserNames.getConstArray();
233 const ::rtl::OUString* pEnd = pBegin + m_aUserNames.getLength();
234 ::rtl::OUString sUserName = m_UserName;
235 for(;pBegin != pEnd;++pBegin)
236 m_LB_USER.InsertEntry(*pBegin);
238 m_LB_USER.SelectEntryPos(0);
239 if(m_xUsers->hasByName(m_UserName))
241 Reference<XAuthorizable> xAuth;
242 m_xUsers->getByName(m_UserName) >>= xAuth;
243 m_TableCtrl.setGrantUser(xAuth);
246 m_TableCtrl.setUserName(GetUser());
247 m_TableCtrl.Init();
252 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
253 m_PB_NEWUSER.Enable(xAppend.is());
254 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
255 m_PB_DELETEUSER.Enable(xDrop.is());
257 m_PB_CHANGEPWD.Enable(m_xUsers.is());
258 m_TableCtrl.Enable(m_xUsers.is());
261 // -----------------------------------------------------------------------
262 SfxTabPage* OUserAdmin::Create( Window* pParent, const SfxItemSet& _rAttrSet )
264 return ( new OUserAdmin( pParent, _rAttrSet ) );
266 // -----------------------------------------------------------------------
267 IMPL_LINK( OUserAdmin, UserHdl, PushButton *, pButton )
271 if(pButton == &m_PB_NEWUSER)
273 SfxPasswordDialog aPwdDlg(this);
274 aPwdDlg.ShowExtras(SHOWEXTRAS_ALL);
275 if(aPwdDlg.Execute())
277 Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
278 Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
279 if(xNewUser.is())
281 xNewUser->setPropertyValue(PROPERTY_NAME,makeAny(rtl::OUString(aPwdDlg.GetUser())));
282 xNewUser->setPropertyValue(PROPERTY_PASSWORD,makeAny(rtl::OUString(aPwdDlg.GetPassword())));
283 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
284 if(xAppend.is())
285 xAppend->appendByDescriptor(xNewUser);
289 else if(pButton == &m_PB_CHANGEPWD)
291 String sName = GetUser();
293 if(m_xUsers->hasByName(sName))
295 Reference<XUser> xUser;
296 m_xUsers->getByName(sName) >>= xUser;
297 if(xUser.is())
299 ::rtl::OUString sNewPassword,sOldPassword;
300 OPasswordDialog aDlg(this,sName);
301 if(aDlg.Execute() == RET_OK)
303 sNewPassword = aDlg.GetNewPassword();
304 sOldPassword = aDlg.GetOldPassword();
306 if(sNewPassword.getLength())
307 xUser->changePassword(sOldPassword,sNewPassword);
312 else
313 {// delete user
314 if(m_xUsers.is() && m_xUsers->hasByName(GetUser()))
316 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
317 if(xDrop.is())
319 QueryBox aQry(this, ModuleRes(QUERY_USERADMIN_DELETE_USER));
320 if(aQry.Execute() == RET_YES)
321 xDrop->dropByName(GetUser());
325 FillUserNames();
327 catch(SQLException& e)
329 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
330 return 0;
332 catch(Exception& )
334 return 0;
337 return 0;
339 // -----------------------------------------------------------------------
340 IMPL_LINK( OUserAdmin, ListDblClickHdl, ListBox *, /*pListBox*/ )
342 m_TableCtrl.setUserName(GetUser());
343 m_TableCtrl.UpdateTables();
344 m_TableCtrl.DeactivateCell();
345 m_TableCtrl.ActivateCell(m_TableCtrl.GetCurRow(),m_TableCtrl.GetCurColumnId());
346 return 0;
349 // -----------------------------------------------------------------------
350 String OUserAdmin::GetUser()
352 return m_LB_USER.GetSelectEntry();
354 // -----------------------------------------------------------------------------
355 void OUserAdmin::fillControls(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
358 // -----------------------------------------------------------------------
359 void OUserAdmin::fillWindows(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
362 // -----------------------------------------------------------------------------
363 void OUserAdmin::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
365 m_TableCtrl.setORB(m_xORB);
368 if ( !m_xConnection.is() && m_pAdminDialog )
370 m_xConnection = m_pAdminDialog->createConnection().first;
371 Reference< XTablesSupplier > xTablesSup(m_xConnection,UNO_QUERY);
372 Reference<XUsersSupplier> xUsersSup(xTablesSup,UNO_QUERY);
373 if ( !xUsersSup.is() )
375 Reference< XDataDefinitionSupplier > xDriver(m_pAdminDialog->getDriver(),UNO_QUERY);
376 if ( xDriver.is() )
378 xUsersSup.set(xDriver->getDataDefinitionByConnection(m_xConnection),UNO_QUERY);
379 xTablesSup.set(xUsersSup,UNO_QUERY);
382 if ( xUsersSup.is() )
384 m_TableCtrl.setTablesSupplier(xTablesSup);
385 m_xUsers = xUsersSup->getUsers();
388 FillUserNames();
390 catch(SQLException& e)
392 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
395 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);