SystemAdminAdded.java 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (C) 2004-2009 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package org.jivesoftware.openfire.commands.clearspace;

import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.admin.AdminManager;
import org.jivesoftware.openfire.component.InternalComponentManager;
import org.dom4j.Element;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

/**
 * Notifies that a new system administrator has been added.
 *
 * @author Armando Jagucki
 */
public class SystemAdminAdded extends AdHocCommand {
38 39
    @Override
	public String getCode() {
40 41 42
        return "http://jabber.org/protocol/event#sys-admin-added";
    }

43 44
    @Override
	public String getDefaultLabel() {
45 46 47
        return "System administrator added";
    }

48 49
    @Override
	public int getMaxStages(SessionData data) {
50 51 52
        return 1;
    }

53 54
    @Override
	public void execute(SessionData sessionData, Element command) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        Element note = command.addElement("note");

        Map<String, List<String>> data = sessionData.getData();

        // Get the username of the new admin
        String adminUsername;
        try {
            adminUsername = get(data, "adminUsername", 0);
        }
        catch (NullPointerException npe) {
            note.addAttribute("type", "error");
            note.setText("Admin username required parameter.");
            return;
        }

        // Promotes the user to administrator, locally
        AdminManager.getInstance().addAdminAccount(adminUsername);

        // Answer that the operation was successful
        note.addAttribute("type", "info");
        note.setText("Operation finished successfully");
    }

78 79
    @Override
	protected void addStageInformation(SessionData data, Element command) {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Dispatching a system admin added event.");
        form.addInstruction("Fill out this form to dispatch a system admin added event.");

        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.text_single);
        field.setLabel("The username of the new system administrator.");
        field.setVariable("adminUsername");
        field.setRequired(true);

        // Add the form to the command
        command.add(form.getElement());
    }

99 100
    @Override
	protected List<Action> getActions(SessionData data) {
101 102 103
        return Arrays.asList(Action.complete);
    }

104 105
    @Override
	protected Action getExecuteAction(SessionData data) {
106 107 108 109 110 111 112 113
        return Action.complete;
    }

    @Override
    public boolean hasPermission(JID requester) {
        return super.hasPermission(requester) || InternalComponentManager.getInstance().hasComponent(requester);
    }
}