Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / dlg / UserAdmin.cxx
blob9fb27f8abe351cc72aff50ca6dcb7c4d1ef10398
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "UserAdmin.hxx"
31 #include "UserAdmin.hrc"
32 #include "UITools.hxx"
33 #include "dbu_dlg.hrc"
34 #include <comphelper/types.hxx>
35 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
36 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
37 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
38 #include <com/sun/star/sdbcx/XDrop.hpp>
39 #include <ucbhelper/interactionrequest.hxx>
40 #include <ucbhelper/simpleauthenticationrequest.hxx>
41 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/sdbcx/XUser.hpp>
44 #include <com/sun/star/sdbcx/XAppend.hpp>
45 #include "dbustrings.hrc"
46 #include <tools/debug.hxx>
47 #include "dbadmin.hxx"
48 #include "moduledbu.hxx"
49 #include <vcl/msgbox.hxx>
50 #include <sfx2/passwd.hxx>
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::sdbcx;
55 using namespace ::com::sun::star::sdbc;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::task;
58 using namespace dbaui;
59 using namespace ucbhelper;
60 using namespace comphelper;
63 class OPasswordDialog : public ModalDialog
65 FixedLine aFLUser;
66 FixedText aFTOldPassword;
67 Edit aEDOldPassword;
68 FixedText aFTPassword;
69 Edit aEDPassword;
70 FixedText aFTPasswordRepeat;
71 Edit aEDPasswordRepeat;
72 OKButton aOKBtn;
73 CancelButton aCancelBtn;
74 HelpButton aHelpBtn;
77 DECL_LINK( OKHdl_Impl, void * );
78 DECL_LINK( ModifiedHdl, Edit * );
80 public:
81 OPasswordDialog( Window* pParent,const String& _sUserName);
83 String GetOldPassword() const { return aEDOldPassword.GetText(); }
84 String GetNewPassword() const { return aEDPassword.GetText(); }
87 OPasswordDialog::OPasswordDialog(Window* _pParent,const String& _sUserName) :
89 ModalDialog( _pParent, ModuleRes( DLG_PASSWORD) ),
91 aFLUser ( this, ModuleRes( FL_USER ) ),
92 aFTOldPassword ( this, ModuleRes( FT_OLDPASSWORD ) ),
93 aEDOldPassword ( this, ModuleRes( ED_OLDPASSWORD ) ),
94 aFTPassword ( this, ModuleRes( FT_PASSWORD ) ),
95 aEDPassword ( this, ModuleRes( ED_PASSWORD ) ),
96 aFTPasswordRepeat ( this, ModuleRes( FT_PASSWORD_REPEAT ) ),
97 aEDPasswordRepeat ( this, ModuleRes( ED_PASSWORD_REPEAT ) ),
98 aOKBtn ( this, ModuleRes( BTN_PASSWORD_OK ) ),
99 aCancelBtn ( this, ModuleRes( BTN_PASSWORD_CANCEL ) ),
100 aHelpBtn ( this, ModuleRes( BTN_PASSWORD_HELP ) )
102 // hide until a help is avalable
103 aHelpBtn.Hide();
105 FreeResource();
106 String sUser = aFLUser.GetText();
107 sUser.SearchAndReplaceAscii("$name$: $",_sUserName);
108 aFLUser.SetText(sUser);
109 aOKBtn.Disable();
111 aOKBtn.SetClickHdl( LINK( this, OPasswordDialog, OKHdl_Impl ) );
112 aEDOldPassword.SetModifyHdl( LINK( this, OPasswordDialog, ModifiedHdl ) );
114 // -----------------------------------------------------------------------------
115 IMPL_LINK_NOARG(OPasswordDialog, OKHdl_Impl)
117 if( aEDPassword.GetText() == aEDPasswordRepeat.GetText() )
118 EndDialog( RET_OK );
119 else
121 String aErrorMsg( ModuleRes( STR_ERROR_PASSWORDS_NOT_IDENTICAL));
122 ErrorBox aErrorBox( this, WB_OK, aErrorMsg );
123 aErrorBox.Execute();
124 aEDPassword.SetText( String() );
125 aEDPasswordRepeat.SetText( String() );
126 aEDPassword.GrabFocus();
128 return 0;
130 // -----------------------------------------------------------------------------
131 IMPL_LINK( OPasswordDialog, ModifiedHdl, Edit *, pEdit )
133 aOKBtn.Enable(pEdit->GetText().Len() != 0);
134 return 0;
137 DBG_NAME(OUserAdmin);
138 //================================================================================
139 // OUserAdmin
140 //================================================================================
141 OUserAdmin::OUserAdmin(Window* pParent,const SfxItemSet& _rAttrSet)
142 : OGenericAdministrationPage( pParent, ModuleRes(TAB_PAGE_USERADMIN), _rAttrSet)
143 ,m_FL_USER( this , ModuleRes(FL_USER))
144 ,m_FT_USER( this , ModuleRes(FT_USER))
145 ,m_LB_USER( this , ModuleRes(LB_USER))
146 ,m_PB_NEWUSER( this , ModuleRes(PB_NEWUSER))
147 ,m_PB_CHANGEPWD( this , ModuleRes(PB_CHANGEPWD))
148 ,m_PB_DELETEUSER( this , ModuleRes(PB_DELETEUSER))
149 ,m_FL_TABLE_GRANTS( this , ModuleRes(FL_TABLE_GRANTS))
150 ,m_TableCtrl( this , ModuleRes(CTRL_TABLE_GRANTS))
152 DBG_CTOR(OUserAdmin,NULL);
153 m_LB_USER.SetSelectHdl(LINK(this, OUserAdmin, ListDblClickHdl));
155 m_PB_NEWUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
156 m_PB_CHANGEPWD.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
157 m_PB_DELETEUSER.SetClickHdl(LINK(this, OUserAdmin, UserHdl));
159 FreeResource();
161 // -----------------------------------------------------------------------
162 OUserAdmin::~OUserAdmin()
164 DBG_DTOR(OUserAdmin,NULL);
165 m_xConnection = NULL;
167 // -----------------------------------------------------------------------
168 void OUserAdmin::FillUserNames()
170 if(m_xConnection.is())
172 m_LB_USER.Clear();
174 Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
176 if ( xMetaData.is() )
178 m_UserName = xMetaData->getUserName();
180 // first we need the users
181 if ( m_xUsers.is() )
183 m_LB_USER.Clear();
185 m_aUserNames = m_xUsers->getElementNames();
186 const ::rtl::OUString* pBegin = m_aUserNames.getConstArray();
187 const ::rtl::OUString* pEnd = pBegin + m_aUserNames.getLength();
188 for(;pBegin != pEnd;++pBegin)
189 m_LB_USER.InsertEntry(*pBegin);
191 m_LB_USER.SelectEntryPos(0);
192 if(m_xUsers->hasByName(m_UserName))
194 Reference<XAuthorizable> xAuth;
195 m_xUsers->getByName(m_UserName) >>= xAuth;
196 m_TableCtrl.setGrantUser(xAuth);
199 m_TableCtrl.setUserName(GetUser());
200 m_TableCtrl.Init();
205 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
206 m_PB_NEWUSER.Enable(xAppend.is());
207 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
208 m_PB_DELETEUSER.Enable(xDrop.is());
210 m_PB_CHANGEPWD.Enable(m_xUsers.is());
211 m_TableCtrl.Enable(m_xUsers.is());
214 // -----------------------------------------------------------------------
215 SfxTabPage* OUserAdmin::Create( Window* pParent, const SfxItemSet& _rAttrSet )
217 return ( new OUserAdmin( pParent, _rAttrSet ) );
219 // -----------------------------------------------------------------------
220 IMPL_LINK( OUserAdmin, UserHdl, PushButton *, pButton )
224 if(pButton == &m_PB_NEWUSER)
226 SfxPasswordDialog aPwdDlg(this);
227 aPwdDlg.ShowExtras(SHOWEXTRAS_ALL);
228 if(aPwdDlg.Execute())
230 Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
231 Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
232 if(xNewUser.is())
234 xNewUser->setPropertyValue(PROPERTY_NAME,makeAny(rtl::OUString(aPwdDlg.GetUser())));
235 xNewUser->setPropertyValue(PROPERTY_PASSWORD,makeAny(rtl::OUString(aPwdDlg.GetPassword())));
236 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
237 if(xAppend.is())
238 xAppend->appendByDescriptor(xNewUser);
242 else if(pButton == &m_PB_CHANGEPWD)
244 String sName = GetUser();
246 if(m_xUsers->hasByName(sName))
248 Reference<XUser> xUser;
249 m_xUsers->getByName(sName) >>= xUser;
250 if(xUser.is())
252 ::rtl::OUString sNewPassword,sOldPassword;
253 OPasswordDialog aDlg(this,sName);
254 if(aDlg.Execute() == RET_OK)
256 sNewPassword = aDlg.GetNewPassword();
257 sOldPassword = aDlg.GetOldPassword();
259 if(!sNewPassword.isEmpty())
260 xUser->changePassword(sOldPassword,sNewPassword);
265 else
266 {// delete user
267 if(m_xUsers.is() && m_xUsers->hasByName(GetUser()))
269 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
270 if(xDrop.is())
272 QueryBox aQry(this, ModuleRes(QUERY_USERADMIN_DELETE_USER));
273 if(aQry.Execute() == RET_YES)
274 xDrop->dropByName(GetUser());
278 FillUserNames();
280 catch(const SQLException& e)
282 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
283 return 0;
285 catch(Exception& )
287 return 0;
290 return 0;
292 // -----------------------------------------------------------------------
293 IMPL_LINK( OUserAdmin, ListDblClickHdl, ListBox *, /*pListBox*/ )
295 m_TableCtrl.setUserName(GetUser());
296 m_TableCtrl.UpdateTables();
297 m_TableCtrl.DeactivateCell();
298 m_TableCtrl.ActivateCell(m_TableCtrl.GetCurRow(),m_TableCtrl.GetCurColumnId());
299 return 0;
302 // -----------------------------------------------------------------------
303 String OUserAdmin::GetUser()
305 return m_LB_USER.GetSelectEntry();
307 // -----------------------------------------------------------------------------
308 void OUserAdmin::fillControls(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
311 // -----------------------------------------------------------------------
312 void OUserAdmin::fillWindows(::std::vector< ISaveValueWrapper* >& /*_rControlList*/)
315 // -----------------------------------------------------------------------------
316 void OUserAdmin::implInitControls(const SfxItemSet& _rSet, sal_Bool _bSaveValue)
318 m_TableCtrl.setORB(m_xORB);
321 if ( !m_xConnection.is() && m_pAdminDialog )
323 m_xConnection = m_pAdminDialog->createConnection().first;
324 Reference< XTablesSupplier > xTablesSup(m_xConnection,UNO_QUERY);
325 Reference<XUsersSupplier> xUsersSup(xTablesSup,UNO_QUERY);
326 if ( !xUsersSup.is() )
328 Reference< XDataDefinitionSupplier > xDriver(m_pAdminDialog->getDriver(),UNO_QUERY);
329 if ( xDriver.is() )
331 xUsersSup.set(xDriver->getDataDefinitionByConnection(m_xConnection),UNO_QUERY);
332 xTablesSup.set(xUsersSup,UNO_QUERY);
335 if ( xUsersSup.is() )
337 m_TableCtrl.setTablesSupplier(xTablesSup);
338 m_xUsers = xUsersSup->getUsers();
341 FillUserNames();
343 catch(const SQLException& e)
345 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),this,m_xORB);
348 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */