tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / dbaccess / source / ui / dlg / UserAdmin.cxx
blob21e293d75f0702e6da44bc1e5d316d25c221edfd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "UserAdmin.hxx"
21 #include <com/sun/star/sdbc/SQLException.hpp>
22 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
23 #include <com/sun/star/sdbc/XDriver.hpp>
24 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
25 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
26 #include <com/sun/star/sdbcx/XDrop.hpp>
27 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/sdbcx/XUser.hpp>
30 #include <com/sun/star/sdbcx/XAppend.hpp>
31 #include <IItemSetHelper.hxx>
32 #include <strings.hrc>
33 #include <strings.hxx>
34 #include <core_resource.hxx>
35 #include <connectivity/dbexception.hxx>
36 #include <connectivity/dbtools.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/weld.hxx>
39 #include <sfx2/passwd.hxx>
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::sdbcx;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::task;
47 using namespace dbaui;
49 namespace {
51 #define MNI_ACTION_ADD_USER "add"
52 #define MNI_ACTION_DEL_USER "delete"
53 #define MNI_ACTION_CHANGE_PASSWORD "password"
55 class OPasswordDialog : public weld::GenericDialogController
57 std::unique_ptr<weld::Frame> m_xUser;
58 std::unique_ptr<weld::Entry> m_xEDOldPassword;
59 std::unique_ptr<weld::Entry> m_xEDPassword;
60 std::unique_ptr<weld::Entry> m_xEDPasswordRepeat;
61 std::unique_ptr<weld::Button> m_xOKBtn;
63 DECL_LINK(OKHdl_Impl, weld::Button&, void);
64 DECL_LINK(ModifiedHdl, weld::Entry&, void);
66 public:
67 OPasswordDialog(weld::Window* pParent, std::u16string_view rUserName);
69 OUString GetOldPassword() const { return m_xEDOldPassword->get_text(); }
70 OUString GetNewPassword() const { return m_xEDPassword->get_text(); }
75 OPasswordDialog::OPasswordDialog(weld::Window* _pParent, std::u16string_view rUserName)
76 : GenericDialogController(_pParent, u"dbaccess/ui/password.ui"_ustr, u"PasswordDialog"_ustr)
77 , m_xUser(m_xBuilder->weld_frame(u"userframe"_ustr))
78 , m_xEDOldPassword(m_xBuilder->weld_entry(u"oldpassword"_ustr))
79 , m_xEDPassword(m_xBuilder->weld_entry(u"newpassword"_ustr))
80 , m_xEDPasswordRepeat(m_xBuilder->weld_entry(u"confirmpassword"_ustr))
81 , m_xOKBtn(m_xBuilder->weld_button(u"ok"_ustr))
83 OUString sUser = m_xUser->get_label();
84 sUser = sUser.replaceFirst("$name$: $", rUserName);
85 m_xUser->set_label(sUser);
86 m_xOKBtn->set_sensitive(false);
88 m_xOKBtn->connect_clicked( LINK( this, OPasswordDialog, OKHdl_Impl ) );
89 m_xEDOldPassword->connect_changed( LINK( this, OPasswordDialog, ModifiedHdl ) );
92 IMPL_LINK_NOARG(OPasswordDialog, OKHdl_Impl, weld::Button&, void)
94 if (m_xEDPassword->get_text() == m_xEDPasswordRepeat->get_text())
95 m_xDialog->response(RET_OK);
96 else
98 OUString aErrorMsg( DBA_RES( STR_ERROR_PASSWORDS_NOT_IDENTICAL));
99 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
100 VclMessageType::Warning, VclButtonsType::Ok,
101 aErrorMsg));
102 xErrorBox->run();
103 m_xEDPassword->set_text(OUString());
104 m_xEDPasswordRepeat->set_text(OUString());
105 m_xEDPassword->grab_focus();
109 IMPL_LINK(OPasswordDialog, ModifiedHdl, weld::Entry&, rEdit, void)
111 m_xOKBtn->set_sensitive(!rEdit.get_text().isEmpty());
114 // OUserAdmin
115 OUserAdmin::OUserAdmin(weld::Container* pPage, weld::DialogController* pController,const SfxItemSet& _rAttrSet)
116 : OGenericAdministrationPage(pPage, pController, u"dbaccess/ui/useradminpage.ui"_ustr, u"UserAdminPage"_ustr, _rAttrSet)
117 , mxActionBar(m_xBuilder->weld_menu_button(u"action_menu"_ustr))
118 , m_xUSER(m_xBuilder->weld_combo_box(u"user"_ustr))
119 , m_xTable(m_xBuilder->weld_container(u"table"_ustr))
120 , m_xTableCtrlParent(m_xTable->CreateChildFrame())
121 , m_xTableCtrl(VclPtr<OTableGrantControl>::Create(m_xTableCtrlParent))
123 mxActionBar->append_item(u"" MNI_ACTION_ADD_USER ""_ustr, DBA_RES(STR_ADD_USER));
124 mxActionBar->append_item(u"" MNI_ACTION_DEL_USER ""_ustr, DBA_RES(STR_DELETE_USER));
125 mxActionBar->append_item(u"" MNI_ACTION_CHANGE_PASSWORD ""_ustr, DBA_RES(STR_CHANGE_PASSWORD));
126 mxActionBar->connect_selected(LINK(this,OUserAdmin,MenuSelectHdl));
128 m_xTableCtrl->Show();
130 m_xUSER->connect_changed(LINK(this, OUserAdmin, ListDblClickHdl));
133 IMPL_LINK(OUserAdmin, MenuSelectHdl, const OUString&, rIdent, void)
137 if (rIdent == MNI_ACTION_ADD_USER) {
138 SfxPasswordDialog aPwdDlg(GetFrameWeld());
139 aPwdDlg.ShowExtras(SfxShowExtras::ALL);
140 if (aPwdDlg.run())
142 Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
143 Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
144 if(xNewUser.is())
146 xNewUser->setPropertyValue(PROPERTY_NAME,Any(aPwdDlg.GetUser()));
147 xNewUser->setPropertyValue(PROPERTY_PASSWORD,Any(aPwdDlg.GetPassword()));
148 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
149 if(xAppend.is())
150 xAppend->appendByDescriptor(xNewUser);
154 else if (rIdent == MNI_ACTION_DEL_USER) {
155 if (m_xUsers.is() && m_xUsers->hasByName(GetUser()))
157 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
158 if(xDrop.is())
160 std::unique_ptr<weld::MessageDialog> xQry(Application::CreateMessageDialog(GetFrameWeld(),
161 VclMessageType::Question, VclButtonsType::YesNo,
162 DBA_RES(STR_QUERY_USERADMIN_DELETE_USER)));
163 if (xQry->run() == RET_YES)
164 xDrop->dropByName(GetUser());
168 else if (rIdent == MNI_ACTION_CHANGE_PASSWORD) {
169 OUString sName = GetUser();
170 if(m_xUsers->hasByName(sName))
172 Reference<XUser> xUser;
173 m_xUsers->getByName(sName) >>= xUser;
174 if(xUser.is())
176 OPasswordDialog aDlg(GetFrameWeld(), sName);
177 if (aDlg.run() == RET_OK)
179 OUString sNewPassword,sOldPassword;
180 sNewPassword = aDlg.GetNewPassword();
181 sOldPassword = aDlg.GetOldPassword();
183 if(!sNewPassword.isEmpty())
184 xUser->changePassword(sOldPassword,sNewPassword);
189 FillUserNames();
191 catch(const SQLException& e)
193 ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
195 catch(Exception& )
200 OUserAdmin::~OUserAdmin()
202 m_xConnection = nullptr;
203 m_xTableCtrl.disposeAndClear();
204 m_xTableCtrlParent->dispose();
205 m_xTableCtrlParent.clear();
208 void OUserAdmin::FillUserNames()
210 if(m_xConnection.is())
212 m_xUSER->clear();
214 Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
216 if ( xMetaData.is() )
218 m_UserName = xMetaData->getUserName();
220 // first we need the users
221 if ( m_xUsers.is() )
223 m_xUSER->clear();
225 for (auto& name : m_xUsers->getElementNames())
226 m_xUSER->append_text(name);
228 m_xUSER->set_active(0);
229 if(m_xUsers->hasByName(m_UserName))
231 Reference<XAuthorizable> xAuth;
232 m_xUsers->getByName(m_UserName) >>= xAuth;
233 m_xTableCtrl->setGrantUser(xAuth);
236 m_xTableCtrl->setUserName(GetUser());
237 m_xTableCtrl->Init();
242 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
243 mxActionBar->set_item_sensitive(u"" MNI_ACTION_ADD_USER ""_ustr, xAppend.is());
244 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
245 mxActionBar->set_item_sensitive(u"" MNI_ACTION_DEL_USER ""_ustr, xDrop.is());
246 mxActionBar->set_item_sensitive(u"" MNI_ACTION_CHANGE_PASSWORD ""_ustr, m_xUsers.is());
248 m_xTableCtrl->Enable(m_xUsers.is());
251 std::unique_ptr<SfxTabPage> OUserAdmin::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* _rAttrSet )
253 return std::make_unique<OUserAdmin>( pPage, pController, *_rAttrSet );
256 IMPL_LINK_NOARG(OUserAdmin, ListDblClickHdl, weld::ComboBox&, void)
258 m_xTableCtrl->setUserName(GetUser());
259 m_xTableCtrl->UpdateTables();
260 m_xTableCtrl->DeactivateCell();
261 m_xTableCtrl->ActivateCell(m_xTableCtrl->GetCurRow(),m_xTableCtrl->GetCurColumnId());
264 OUString OUserAdmin::GetUser() const
266 return m_xUSER->get_active_text();
269 void OUserAdmin::fillControls(std::vector< std::unique_ptr<ISaveValueWrapper> >& /*_rControlList*/)
273 void OUserAdmin::fillWindows(std::vector< std::unique_ptr<ISaveValueWrapper> >& /*_rControlList*/)
277 void OUserAdmin::implInitControls(const SfxItemSet& _rSet, bool _bSaveValue)
279 m_xTableCtrl->setComponentContext(m_xORB);
282 if ( !m_xConnection.is() && m_pAdminDialog )
284 m_xConnection = m_pAdminDialog->createConnection().first;
285 Reference< XTablesSupplier > xTablesSup(m_xConnection,UNO_QUERY);
286 Reference<XUsersSupplier> xUsersSup(xTablesSup,UNO_QUERY);
287 if ( !xUsersSup.is() )
289 Reference< XDataDefinitionSupplier > xDriver(m_pAdminDialog->getDriver(),UNO_QUERY);
290 if ( xDriver.is() )
292 xUsersSup.set(xDriver->getDataDefinitionByConnection(m_xConnection),UNO_QUERY);
293 xTablesSup.set(xUsersSup,UNO_QUERY);
296 if ( xUsersSup.is() )
298 m_xTableCtrl->setTablesSupplier(xTablesSup);
299 m_xUsers = xUsersSup->getUsers();
302 FillUserNames();
304 catch(const SQLException& e)
306 ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
309 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */