UserProperties.java 4 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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.
Alex Wenckus's avatar
Alex Wenckus committed
15
 */
16
package org.jivesoftware.openfire.commands.admin.user;
Alex Wenckus's avatar
Alex Wenckus committed
17

18
import org.dom4j.Element;
19 20 21
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.user.User;
22
import org.jivesoftware.openfire.user.UserManager;
Alex Wenckus's avatar
Alex Wenckus committed
23 24 25 26
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

27
import java.util.Collections;
Alex Wenckus's avatar
Alex Wenckus committed
28 29 30
import java.util.List;

/**
31
 *  An adhoc command to retrieve the properties of the user.
Alex Wenckus's avatar
Alex Wenckus committed
32
 *
33
 * @author Alexander Wenckus
Alex Wenckus's avatar
Alex Wenckus committed
34 35
 */
public class UserProperties extends AdHocCommand {
36 37
    @Override
	public String getCode() {
Alex Wenckus's avatar
Alex Wenckus committed
38 39 40
        return "http://jabber.org/protocol/admin#get-user-properties";
    }

41 42
    @Override
	public String getDefaultLabel() {
Alex Wenckus's avatar
Alex Wenckus committed
43 44 45
        return "Get User Properties";
    }

46 47
    @Override
	public int getMaxStages(SessionData data) {
Alex Wenckus's avatar
Alex Wenckus committed
48 49 50
        return 1;
    }

51 52
    @Override
	public void execute(SessionData data, Element command) {
Alex Wenckus's avatar
Alex Wenckus committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        DataForm form = new DataForm(DataForm.Type.result);

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        List<String> accounts = data.getData().get("accountjids");

        if (accounts != null && accounts.size() > 0) {
            populateResponseFields(form, accounts);
        }

        command.add(form.getElement());
    }

    private void populateResponseFields(DataForm form, List<String> accounts) {
        FormField jidField = form.addField();
        jidField.setVariable("accountjids");

        FormField emailField = form.addField();
        emailField.setVariable("email");

        FormField nameField = form.addField();
        nameField.setVariable("name");

        UserManager manager = UserManager.getInstance();
        for(String account : accounts) {
            User user;
            try {
                JID jid = new JID(account);
                user = manager.getUser(jid.getNode());
            }
            catch (Exception ex) {
                continue;
            }

            jidField.addValue(account);
            emailField.addValue(user.getEmail());
            nameField.addValue(user.getName());
        }
    }

96 97
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Alex Wenckus's avatar
Alex Wenckus committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle("Retrieve Users' Information");
        form.addInstruction("Fill out this form to retrieve users' information.");

        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_multi);
        field.setLabel("The list of Jabber IDs to retrive the information");
        field.setVariable("accountjids");
        field.setRequired(true);

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

117 118
    @Override
	protected List<Action> getActions(SessionData data) {
119
        return Collections.singletonList(Action.complete);
Alex Wenckus's avatar
Alex Wenckus committed
120 121
    }

122 123
    @Override
	protected AdHocCommand.Action getExecuteAction(SessionData data) {
Alex Wenckus's avatar
Alex Wenckus committed
124 125 126
        return AdHocCommand.Action.complete;
    }
}