LibreOffice Module dbaccess (master) 1
UserAdmin.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
37#include <vcl/svapp.hxx>
38#include <vcl/weld.hxx>
39#include <sfx2/passwd.hxx>
40
41using namespace ::com::sun::star::container;
42using namespace ::com::sun::star::beans;
43using namespace ::com::sun::star::sdbcx;
44using namespace ::com::sun::star::sdbc;
45using namespace ::com::sun::star::uno;
46using namespace ::com::sun::star::task;
47using namespace dbaui;
48using namespace comphelper;
49
50namespace {
51
52#define MNI_ACTION_ADD_USER "add"
53#define MNI_ACTION_DEL_USER "delete"
54#define MNI_ACTION_CHANGE_PASSWORD "password"
55
56class OPasswordDialog : public weld::GenericDialogController
57{
58 std::unique_ptr<weld::Frame> m_xUser;
59 std::unique_ptr<weld::Entry> m_xEDOldPassword;
60 std::unique_ptr<weld::Entry> m_xEDPassword;
61 std::unique_ptr<weld::Entry> m_xEDPasswordRepeat;
62 std::unique_ptr<weld::Button> m_xOKBtn;
63
64 DECL_LINK(OKHdl_Impl, weld::Button&, void);
65 DECL_LINK(ModifiedHdl, weld::Entry&, void);
66
67public:
68 OPasswordDialog(weld::Window* pParent, std::u16string_view rUserName);
69
70 OUString GetOldPassword() const { return m_xEDOldPassword->get_text(); }
71 OUString GetNewPassword() const { return m_xEDPassword->get_text(); }
72};
73
74}
75
76OPasswordDialog::OPasswordDialog(weld::Window* _pParent, std::u16string_view rUserName)
77 : GenericDialogController(_pParent, "dbaccess/ui/password.ui", "PasswordDialog")
78 , m_xUser(m_xBuilder->weld_frame("userframe"))
79 , m_xEDOldPassword(m_xBuilder->weld_entry("oldpassword"))
80 , m_xEDPassword(m_xBuilder->weld_entry("newpassword"))
81 , m_xEDPasswordRepeat(m_xBuilder->weld_entry("confirmpassword"))
82 , m_xOKBtn(m_xBuilder->weld_button("ok"))
83{
84 OUString sUser = m_xUser->get_label();
85 sUser = sUser.replaceFirst("$name$: $", rUserName);
86 m_xUser->set_label(sUser);
87 m_xOKBtn->set_sensitive(false);
88
89 m_xOKBtn->connect_clicked( LINK( this, OPasswordDialog, OKHdl_Impl ) );
90 m_xEDOldPassword->connect_changed( LINK( this, OPasswordDialog, ModifiedHdl ) );
91}
92
93IMPL_LINK_NOARG(OPasswordDialog, OKHdl_Impl, weld::Button&, void)
94{
95 if (m_xEDPassword->get_text() == m_xEDPasswordRepeat->get_text())
96 m_xDialog->response(RET_OK);
97 else
98 {
99 OUString aErrorMsg( DBA_RES( STR_ERROR_PASSWORDS_NOT_IDENTICAL));
100 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
101 VclMessageType::Warning, VclButtonsType::Ok,
102 aErrorMsg));
103 xErrorBox->run();
104 m_xEDPassword->set_text(OUString());
105 m_xEDPasswordRepeat->set_text(OUString());
106 m_xEDPassword->grab_focus();
107 }
108}
109
110IMPL_LINK(OPasswordDialog, ModifiedHdl, weld::Entry&, rEdit, void)
111{
112 m_xOKBtn->set_sensitive(!rEdit.get_text().isEmpty());
113}
114
115// OUserAdmin
116OUserAdmin::OUserAdmin(weld::Container* pPage, weld::DialogController* pController,const SfxItemSet& _rAttrSet)
117 : OGenericAdministrationPage(pPage, pController, "dbaccess/ui/useradminpage.ui", "UserAdminPage", _rAttrSet)
118 , mxActionBar(m_xBuilder->weld_menu_button("action_menu"))
119 , m_xUSER(m_xBuilder->weld_combo_box("user"))
120 , m_xTable(m_xBuilder->weld_container("table"))
121 , m_xTableCtrlParent(m_xTable->CreateChildFrame())
122 , m_xTableCtrl(VclPtr<OTableGrantControl>::Create(m_xTableCtrlParent))
123{
124 mxActionBar->append_item(MNI_ACTION_ADD_USER, DBA_RES(STR_ADD_USER));
125 mxActionBar->append_item(MNI_ACTION_DEL_USER, DBA_RES(STR_DELETE_USER));
126 mxActionBar->append_item(MNI_ACTION_CHANGE_PASSWORD, DBA_RES(STR_CHANGE_PASSWORD));
127 mxActionBar->connect_selected(LINK(this,OUserAdmin,MenuSelectHdl));
128
129 m_xTableCtrl->Show();
130
131 m_xUSER->connect_changed(LINK(this, OUserAdmin, ListDblClickHdl));
132}
133
134IMPL_LINK(OUserAdmin, MenuSelectHdl, const OUString&, rIdent, void)
135{
136 try
137 {
138 if (rIdent == MNI_ACTION_ADD_USER) {
140 aPwdDlg.ShowExtras(SfxShowExtras::ALL);
141 if (aPwdDlg.run())
142 {
143 Reference<XDataDescriptorFactory> xUserFactory(m_xUsers,UNO_QUERY);
144 Reference<XPropertySet> xNewUser = xUserFactory->createDataDescriptor();
145 if(xNewUser.is())
146 {
147 xNewUser->setPropertyValue(PROPERTY_NAME,Any(aPwdDlg.GetUser()));
148 xNewUser->setPropertyValue(PROPERTY_PASSWORD,Any(aPwdDlg.GetPassword()));
149 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
150 if(xAppend.is())
151 xAppend->appendByDescriptor(xNewUser);
152 }
153 }
154 }
155 else if (rIdent == MNI_ACTION_DEL_USER) {
156 if (m_xUsers.is() && m_xUsers->hasByName(GetUser()))
157 {
158 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
159 if(xDrop.is())
160 {
161 std::unique_ptr<weld::MessageDialog> xQry(Application::CreateMessageDialog(GetFrameWeld(),
162 VclMessageType::Question, VclButtonsType::YesNo,
163 DBA_RES(STR_QUERY_USERADMIN_DELETE_USER)));
164 if (xQry->run() == RET_YES)
165 xDrop->dropByName(GetUser());
166 }
167 }
168 }
169 else if (rIdent == MNI_ACTION_CHANGE_PASSWORD) {
170 OUString sName = GetUser();
171 if(m_xUsers->hasByName(sName))
172 {
173 Reference<XUser> xUser;
174 m_xUsers->getByName(sName) >>= xUser;
175 if(xUser.is())
176 {
177 OPasswordDialog aDlg(GetFrameWeld(), sName);
178 if (aDlg.run() == RET_OK)
179 {
180 OUString sNewPassword,sOldPassword;
181 sNewPassword = aDlg.GetNewPassword();
182 sOldPassword = aDlg.GetOldPassword();
183
184 if(!sNewPassword.isEmpty())
185 xUser->changePassword(sOldPassword,sNewPassword);
186 }
187 }
188 }
189 }
190 FillUserNames();
191 }
192 catch(const SQLException& e)
193 {
194 ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
195 }
196 catch(Exception& )
197 {
198 }
199}
200
202{
203 m_xConnection = nullptr;
204 m_xTableCtrl.disposeAndClear();
205 m_xTableCtrlParent->dispose();
206 m_xTableCtrlParent.clear();
207}
208
210{
211 if(m_xConnection.is())
212 {
213 m_xUSER->clear();
214
215 Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
216
217 if ( xMetaData.is() )
218 {
219 m_UserName = xMetaData->getUserName();
220
221 // first we need the users
222 if ( m_xUsers.is() )
223 {
224 m_xUSER->clear();
225
226 m_aUserNames = m_xUsers->getElementNames();
227 const OUString* pBegin = m_aUserNames.getConstArray();
228 const OUString* pEnd = pBegin + m_aUserNames.getLength();
229 for(;pBegin != pEnd;++pBegin)
230 m_xUSER->append_text(*pBegin);
231
232 m_xUSER->set_active(0);
233 if(m_xUsers->hasByName(m_UserName))
234 {
236 m_xUsers->getByName(m_UserName) >>= xAuth;
237 m_xTableCtrl->setGrantUser(xAuth);
238 }
239
240 m_xTableCtrl->setUserName(GetUser());
241 m_xTableCtrl->Init();
242 }
243 }
244 }
245
246 Reference<XAppend> xAppend(m_xUsers,UNO_QUERY);
247 mxActionBar->set_item_sensitive(MNI_ACTION_ADD_USER, xAppend.is());
248 Reference<XDrop> xDrop(m_xUsers,UNO_QUERY);
249 mxActionBar->set_item_sensitive(MNI_ACTION_DEL_USER, xDrop.is());
250 mxActionBar->set_item_sensitive(MNI_ACTION_CHANGE_PASSWORD, m_xUsers.is());
251
252 m_xTableCtrl->Enable(m_xUsers.is());
253}
254
255std::unique_ptr<SfxTabPage> OUserAdmin::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* _rAttrSet )
256{
257 return std::make_unique<OUserAdmin>( pPage, pController, *_rAttrSet );
258}
259
261{
262 m_xTableCtrl->setUserName(GetUser());
263 m_xTableCtrl->UpdateTables();
264 m_xTableCtrl->DeactivateCell();
265 m_xTableCtrl->ActivateCell(m_xTableCtrl->GetCurRow(),m_xTableCtrl->GetCurColumnId());
266}
267
268OUString OUserAdmin::GetUser() const
269{
270 return m_xUSER->get_active_text();
271}
272
273void OUserAdmin::fillControls(std::vector< std::unique_ptr<ISaveValueWrapper> >& /*_rControlList*/)
274{
275}
276
277void OUserAdmin::fillWindows(std::vector< std::unique_ptr<ISaveValueWrapper> >& /*_rControlList*/)
278{
279}
280
281void OUserAdmin::implInitControls(const SfxItemSet& _rSet, bool _bSaveValue)
282{
283 m_xTableCtrl->setComponentContext(m_xORB);
284 try
285 {
286 if ( !m_xConnection.is() && m_pAdminDialog )
287 {
289 Reference< XTablesSupplier > xTablesSup(m_xConnection,UNO_QUERY);
290 Reference<XUsersSupplier> xUsersSup(xTablesSup,UNO_QUERY);
291 if ( !xUsersSup.is() )
292 {
294 if ( xDriver.is() )
295 {
296 xUsersSup.set(xDriver->getDataDefinitionByConnection(m_xConnection),UNO_QUERY);
297 xTablesSup.set(xUsersSup,UNO_QUERY);
298 }
299 }
300 if ( xUsersSup.is() )
301 {
302 m_xTableCtrl->setTablesSupplier(xTablesSup);
303 m_xUsers = xUsersSup->getUsers();
304 }
305 }
307 }
308 catch(const SQLException& e)
309 {
310 ::dbtools::showError(::dbtools::SQLExceptionInfo(e), GetDialogController()->getDialog()->GetXWindow(), m_xORB);
311 }
312
314}
315
316/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sName
#define MNI_ACTION_DEL_USER
Definition: UserAdmin.cxx:53
IMPL_LINK_NOARG(OPasswordDialog, OKHdl_Impl, weld::Button &, void)
Definition: UserAdmin.cxx:93
#define MNI_ACTION_CHANGE_PASSWORD
Definition: UserAdmin.cxx:54
IMPL_LINK(OPasswordDialog, ModifiedHdl, weld::Entry &, rEdit, void)
Definition: UserAdmin.cxx:110
#define MNI_ACTION_ADD_USER
Definition: UserAdmin.cxx:52
Reference< XExecutableDialog > m_xDialog
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
OUString GetPassword() const
virtual short run() override
OUString GetUser() const
void ShowExtras(SfxShowExtras nExtras)
SfxOkDialogController * GetDialogController() const
virtual css::uno::Reference< css::sdbc::XDriver > getDriver()=0
virtual std::pair< css::uno::Reference< css::sdbc::XConnection >, bool > createConnection()=0
IDatabaseSettingsDialog * m_pAdminDialog
Definition: adminpages.hxx:94
css::uno::Reference< css::uno::XComponentContext > m_xORB
Definition: adminpages.hxx:98
virtual void implInitControls(const SfxItemSet &_rSet, bool _bSaveValue)
called from within Reset and ActivatePage, use to initialize the controls with the items from the giv...
Definition: adminpages.cxx:150
virtual void fillControls(std::vector< std::unique_ptr< ISaveValueWrapper > > &_rControlList) override
will be called inside <method>implInitControls</method> to save the value if necessary
Definition: UserAdmin.cxx:273
virtual void implInitControls(const SfxItemSet &_rSet, bool _bSaveValue) override
called from within Reset and ActivatePage, use to initialize the controls with the items from the giv...
Definition: UserAdmin.cxx:281
virtual ~OUserAdmin() override
Definition: UserAdmin.cxx:201
std::unique_ptr< weld::MenuButton > mxActionBar
Definition: UserAdmin.hxx:35
std::unique_ptr< weld::ComboBox > m_xUSER
Definition: UserAdmin.hxx:36
css::uno::Reference< css::awt::XWindow > m_xTableCtrlParent
Definition: UserAdmin.hxx:38
OUString m_UserName
Definition: UserAdmin.hxx:45
css::uno::Reference< css::container::XNameAccess > m_xUsers
Definition: UserAdmin.hxx:42
virtual void fillWindows(std::vector< std::unique_ptr< ISaveValueWrapper > > &_rControlList) override
will be called inside <method>implInitControls</method> to disable if necessary
Definition: UserAdmin.cxx:277
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: UserAdmin.cxx:255
css::uno::Sequence< OUString > m_aUserNames
Definition: UserAdmin.hxx:43
VclPtr< OTableGrantControl > m_xTableCtrl
Definition: UserAdmin.hxx:39
css::uno::Reference< css::sdbc::XConnection > m_xConnection
Definition: UserAdmin.hxx:41
OUString GetUser() const
Definition: UserAdmin.cxx:268
#define DBA_RES(id)
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
@ Exception
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
IMPL_LINK(OApplicationController, OnSelectContainer, void *, _pType, void)
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
constexpr OUStringLiteral PROPERTY_PASSWORD(u"Password")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
RET_OK
RET_YES