1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "UserAdmin.hxx"
22 #include "UserAdmin.hrc"
23 #include "UITools.hxx"
24 #include "dbu_dlg.hrc"
25 #include <comphelper/types.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
28 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
29 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
30 #include <com/sun/star/sdbcx/XDrop.hpp>
31 #include <ucbhelper/interactionrequest.hxx>
32 #include <ucbhelper/simpleauthenticationrequest.hxx>
33 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/sdbcx/XUser.hpp>
36 #include <com/sun/star/sdbcx/XAppend.hpp>
37 #include "dbustrings.hrc"
38 #include <tools/debug.hxx>
39 #include "dbadmin.hxx"
40 #include "moduledbu.hxx"
41 #include <vcl/msgbox.hxx>
42 #include <sfx2/passwd.hxx>
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::beans
;
46 using namespace ::com::sun::star::sdbcx
;
47 using namespace ::com::sun::star::sdbc
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::task
;
50 using namespace dbaui
;
51 using namespace ucbhelper
;
52 using namespace comphelper
;
55 class OPasswordDialog
: public ModalDialog
58 FixedText aFTOldPassword
;
60 FixedText aFTPassword
;
62 FixedText aFTPasswordRepeat
;
63 Edit aEDPasswordRepeat
;
65 CancelButton aCancelBtn
;
69 DECL_LINK( OKHdl_Impl
, void * );
70 DECL_LINK( ModifiedHdl
, Edit
* );
73 OPasswordDialog( Window
* pParent
,const String
& _sUserName
);
75 String
GetOldPassword() const { return aEDOldPassword
.GetText(); }
76 String
GetNewPassword() const { return aEDPassword
.GetText(); }
79 OPasswordDialog::OPasswordDialog(Window
* _pParent
,const String
& _sUserName
) :
81 ModalDialog( _pParent
, ModuleRes( DLG_PASSWORD
) ),
83 aFLUser ( this, ModuleRes( FL_USER
) ),
84 aFTOldPassword ( this, ModuleRes( FT_OLDPASSWORD
) ),
85 aEDOldPassword ( this, ModuleRes( ED_OLDPASSWORD
) ),
86 aFTPassword ( this, ModuleRes( FT_PASSWORD
) ),
87 aEDPassword ( this, ModuleRes( ED_PASSWORD
) ),
88 aFTPasswordRepeat ( this, ModuleRes( FT_PASSWORD_REPEAT
) ),
89 aEDPasswordRepeat ( this, ModuleRes( ED_PASSWORD_REPEAT
) ),
90 aOKBtn ( this, ModuleRes( BTN_PASSWORD_OK
) ),
91 aCancelBtn ( this, ModuleRes( BTN_PASSWORD_CANCEL
) ),
92 aHelpBtn ( this, ModuleRes( BTN_PASSWORD_HELP
) )
94 // hide until a help is avalable
98 String sUser
= aFLUser
.GetText();
99 sUser
.SearchAndReplaceAscii("$name$: $",_sUserName
);
100 aFLUser
.SetText(sUser
);
103 aOKBtn
.SetClickHdl( LINK( this, OPasswordDialog
, OKHdl_Impl
) );
104 aEDOldPassword
.SetModifyHdl( LINK( this, OPasswordDialog
, ModifiedHdl
) );
106 // -----------------------------------------------------------------------------
107 IMPL_LINK_NOARG(OPasswordDialog
, OKHdl_Impl
)
109 if( aEDPassword
.GetText() == aEDPasswordRepeat
.GetText() )
113 String
aErrorMsg( ModuleRes( STR_ERROR_PASSWORDS_NOT_IDENTICAL
));
114 ErrorBox
aErrorBox( this, WB_OK
, aErrorMsg
);
116 aEDPassword
.SetText( String() );
117 aEDPasswordRepeat
.SetText( String() );
118 aEDPassword
.GrabFocus();
122 // -----------------------------------------------------------------------------
123 IMPL_LINK( OPasswordDialog
, ModifiedHdl
, Edit
*, pEdit
)
125 aOKBtn
.Enable(!pEdit
->GetText().isEmpty());
129 DBG_NAME(OUserAdmin
);
130 //================================================================================
132 //================================================================================
133 OUserAdmin::OUserAdmin(Window
* pParent
,const SfxItemSet
& _rAttrSet
)
134 : OGenericAdministrationPage( pParent
, ModuleRes(TAB_PAGE_USERADMIN
), _rAttrSet
)
135 ,m_FL_USER( this , ModuleRes(FL_USER
))
136 ,m_FT_USER( this , ModuleRes(FT_USER
))
137 ,m_LB_USER( this , ModuleRes(LB_USER
))
138 ,m_PB_NEWUSER( this , ModuleRes(PB_NEWUSER
))
139 ,m_PB_CHANGEPWD( this , ModuleRes(PB_CHANGEPWD
))
140 ,m_PB_DELETEUSER( this , ModuleRes(PB_DELETEUSER
))
141 ,m_FL_TABLE_GRANTS( this , ModuleRes(FL_TABLE_GRANTS
))
142 ,m_TableCtrl( this , ModuleRes(CTRL_TABLE_GRANTS
))
144 DBG_CTOR(OUserAdmin
,NULL
);
145 m_LB_USER
.SetSelectHdl(LINK(this, OUserAdmin
, ListDblClickHdl
));
147 m_PB_NEWUSER
.SetClickHdl(LINK(this, OUserAdmin
, UserHdl
));
148 m_PB_CHANGEPWD
.SetClickHdl(LINK(this, OUserAdmin
, UserHdl
));
149 m_PB_DELETEUSER
.SetClickHdl(LINK(this, OUserAdmin
, UserHdl
));
153 // -----------------------------------------------------------------------
154 OUserAdmin::~OUserAdmin()
156 DBG_DTOR(OUserAdmin
,NULL
);
157 m_xConnection
= NULL
;
159 // -----------------------------------------------------------------------
160 void OUserAdmin::FillUserNames()
162 if(m_xConnection
.is())
166 Reference
<XDatabaseMetaData
> xMetaData
= m_xConnection
->getMetaData();
168 if ( xMetaData
.is() )
170 m_UserName
= xMetaData
->getUserName();
172 // first we need the users
177 m_aUserNames
= m_xUsers
->getElementNames();
178 const OUString
* pBegin
= m_aUserNames
.getConstArray();
179 const OUString
* pEnd
= pBegin
+ m_aUserNames
.getLength();
180 for(;pBegin
!= pEnd
;++pBegin
)
181 m_LB_USER
.InsertEntry(*pBegin
);
183 m_LB_USER
.SelectEntryPos(0);
184 if(m_xUsers
->hasByName(m_UserName
))
186 Reference
<XAuthorizable
> xAuth
;
187 m_xUsers
->getByName(m_UserName
) >>= xAuth
;
188 m_TableCtrl
.setGrantUser(xAuth
);
191 m_TableCtrl
.setUserName(GetUser());
197 Reference
<XAppend
> xAppend(m_xUsers
,UNO_QUERY
);
198 m_PB_NEWUSER
.Enable(xAppend
.is());
199 Reference
<XDrop
> xDrop(m_xUsers
,UNO_QUERY
);
200 m_PB_DELETEUSER
.Enable(xDrop
.is());
202 m_PB_CHANGEPWD
.Enable(m_xUsers
.is());
203 m_TableCtrl
.Enable(m_xUsers
.is());
206 // -----------------------------------------------------------------------
207 SfxTabPage
* OUserAdmin::Create( Window
* pParent
, const SfxItemSet
& _rAttrSet
)
209 return ( new OUserAdmin( pParent
, _rAttrSet
) );
211 // -----------------------------------------------------------------------
212 IMPL_LINK( OUserAdmin
, UserHdl
, PushButton
*, pButton
)
216 if(pButton
== &m_PB_NEWUSER
)
218 SfxPasswordDialog
aPwdDlg(this);
219 aPwdDlg
.ShowExtras(SHOWEXTRAS_ALL
);
220 if(aPwdDlg
.Execute())
222 Reference
<XDataDescriptorFactory
> xUserFactory(m_xUsers
,UNO_QUERY
);
223 Reference
<XPropertySet
> xNewUser
= xUserFactory
->createDataDescriptor();
226 xNewUser
->setPropertyValue(PROPERTY_NAME
,makeAny(OUString(aPwdDlg
.GetUser())));
227 xNewUser
->setPropertyValue(PROPERTY_PASSWORD
,makeAny(OUString(aPwdDlg
.GetPassword())));
228 Reference
<XAppend
> xAppend(m_xUsers
,UNO_QUERY
);
230 xAppend
->appendByDescriptor(xNewUser
);
234 else if(pButton
== &m_PB_CHANGEPWD
)
236 String sName
= GetUser();
238 if(m_xUsers
->hasByName(sName
))
240 Reference
<XUser
> xUser
;
241 m_xUsers
->getByName(sName
) >>= xUser
;
244 OUString sNewPassword
,sOldPassword
;
245 OPasswordDialog
aDlg(this,sName
);
246 if(aDlg
.Execute() == RET_OK
)
248 sNewPassword
= aDlg
.GetNewPassword();
249 sOldPassword
= aDlg
.GetOldPassword();
251 if(!sNewPassword
.isEmpty())
252 xUser
->changePassword(sOldPassword
,sNewPassword
);
259 if(m_xUsers
.is() && m_xUsers
->hasByName(GetUser()))
261 Reference
<XDrop
> xDrop(m_xUsers
,UNO_QUERY
);
264 QueryBox
aQry(this, ModuleRes(QUERY_USERADMIN_DELETE_USER
));
265 if(aQry
.Execute() == RET_YES
)
266 xDrop
->dropByName(GetUser());
272 catch(const SQLException
& e
)
274 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
), this, m_xORB
);
284 // -----------------------------------------------------------------------
285 IMPL_LINK( OUserAdmin
, ListDblClickHdl
, ListBox
*, /*pListBox*/ )
287 m_TableCtrl
.setUserName(GetUser());
288 m_TableCtrl
.UpdateTables();
289 m_TableCtrl
.DeactivateCell();
290 m_TableCtrl
.ActivateCell(m_TableCtrl
.GetCurRow(),m_TableCtrl
.GetCurColumnId());
294 // -----------------------------------------------------------------------
295 String
OUserAdmin::GetUser()
297 return m_LB_USER
.GetSelectEntry();
299 // -----------------------------------------------------------------------------
300 void OUserAdmin::fillControls(::std::vector
< ISaveValueWrapper
* >& /*_rControlList*/)
303 // -----------------------------------------------------------------------
304 void OUserAdmin::fillWindows(::std::vector
< ISaveValueWrapper
* >& /*_rControlList*/)
307 // -----------------------------------------------------------------------------
308 void OUserAdmin::implInitControls(const SfxItemSet
& _rSet
, sal_Bool _bSaveValue
)
310 m_TableCtrl
.setComponentContext(m_xORB
);
313 if ( !m_xConnection
.is() && m_pAdminDialog
)
315 m_xConnection
= m_pAdminDialog
->createConnection().first
;
316 Reference
< XTablesSupplier
> xTablesSup(m_xConnection
,UNO_QUERY
);
317 Reference
<XUsersSupplier
> xUsersSup(xTablesSup
,UNO_QUERY
);
318 if ( !xUsersSup
.is() )
320 Reference
< XDataDefinitionSupplier
> xDriver(m_pAdminDialog
->getDriver(),UNO_QUERY
);
323 xUsersSup
.set(xDriver
->getDataDefinitionByConnection(m_xConnection
),UNO_QUERY
);
324 xTablesSup
.set(xUsersSup
,UNO_QUERY
);
327 if ( xUsersSup
.is() )
329 m_TableCtrl
.setTablesSupplier(xTablesSup
);
330 m_xUsers
= xUsersSup
->getUsers();
335 catch(const SQLException
& e
)
337 ::dbaui::showError(::dbtools::SQLExceptionInfo(e
), this, m_xORB
);
340 OGenericAdministrationPage::implInitControls(_rSet
, _bSaveValue
);
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */