Commit e2e6e399 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Initial version. JM-404

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@5706 b35dd754-fafc-0310-a699-88a17e54d16e
parent 54f14f24
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.commands.admin;
import org.dom4j.Element;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.commands.AdHocCommand;
import org.jivesoftware.wildfire.commands.SessionData;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;
import java.util.Arrays;
import java.util.List;
/**
* Command that allows to change password of existing users.
*
* @author Gaston Dombiak
*
* TODO Use i18n
*/
public class ChangeUserPassword extends AdHocCommand {
public String getCode() {
return "http://jabber.org/protocol/admin#change-user-password";
}
public String getDefaultLabel() {
return "Change User Password";
}
public int getMaxStages(SessionData data) {
return 1;
}
public void execute(SessionData data, Element command) {
Element note = command.addElement("note");
// Check if groups cannot be modified (backend is read-only)
if (UserManager.getUserProvider().isReadOnly()) {
note.addAttribute("type", "error");
note.setText("Users are read only. Changing password is not allowed.");
return;
}
JID account = new JID(data.getData().get("accountjid").get(0));
String newPassword = data.getData().get("password").get(0);
if (!XMPPServer.getInstance().isLocal(account)) {
note.addAttribute("type", "error");
note.setText("Cannot change password of remote user.");
return;
}
// Get requested group
User user;
try {
user = UserManager.getInstance().getUser(account.getNode());
} catch (UserNotFoundException e) {
// Group not found
note.addAttribute("type", "error");
note.setText("User does not exists.");
return;
}
// Set the new passowrd of the user
user.setPassword(newPassword);
// Answer that the operation was successful
note.addAttribute("type", "info");
note.setText("Operation finished successfully");
}
protected void addStageInformation(SessionData data, Element command) {
DataForm form = new DataForm(DataForm.Type.form);
form.setTitle("Changing a User Password");
form.addInstruction("Fill out this form to change a user\u2019s password.");
FormField field = form.addField();
field.setType(FormField.Type.hidden);
field.setVariable("FORM_TYPE");
field.addValue("http://jabber.org/protocol/admin");
field = form.addField();
field.setType(FormField.Type.jid_single);
field.setLabel("The Jabber ID for this account");
field.setVariable("accountjid");
field.setRequired(true);
field = form.addField();
field.setType(FormField.Type.text_private);
field.setLabel("The password for this account");
field.setVariable("password");
field.setRequired(true);
// Add the form to the command
command.add(form.getElement());
}
protected List<Action> getActions(SessionData data) {
return Arrays.asList(AdHocCommand.Action.complete);
}
protected AdHocCommand.Action getExecuteAction(SessionData data) {
return AdHocCommand.Action.complete;
}
public boolean hasPermission(JID requester) {
return super.hasPermission(requester) && !UserManager.getUserProvider().isReadOnly();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment