AddUser.java 6.34 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1
/**
2 3 4
 * $RCSfile$
 * $Revision: 3144 $
 * $Date: 2005-12-01 14:20:11 -0300 (Thu, 01 Dec 2005) $
Alex Wenckus's avatar
Alex Wenckus committed
5
 *
6 7
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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
19
 */
20
package org.jivesoftware.openfire.commands.admin.user;
Alex Wenckus's avatar
Alex Wenckus committed
21

22 23
import org.dom4j.Element;
import org.jivesoftware.openfire.XMPPServer;
24 25
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
26
import org.jivesoftware.openfire.component.InternalComponentManager;
27
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
28
import org.jivesoftware.openfire.user.UserManager;
29
import org.jivesoftware.util.StringUtils;
Alex Wenckus's avatar
Alex Wenckus committed
30 31 32 33 34
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

import java.util.Arrays;
35
import java.util.List;
Alex Wenckus's avatar
Alex Wenckus committed
36 37 38
import java.util.Map;

/**
39
 * Adds a user to Openfire if the provider is not read-only. See
Alex Wenckus's avatar
Alex Wenckus committed
40 41 42 43 44 45 46 47 48 49 50 51 52 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 96 97 98
 * <a href="http://www.xmpp.org/extensions/xep-0133.html#add-user">Service Administration:
 * Add User</a>
 *
 * @author Alexander Wenckus
 */
public class AddUser extends AdHocCommand {
    public String getCode() {
        return "http://jabber.org/protocol/admin#add-user";
    }

    public String getDefaultLabel() {
        return "Add a User";
    }

    public int getMaxStages(SessionData data) {
        return 1;
    }

    public void execute(SessionData sessionData, 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("User provider is read only. New users cannot be created.");
            return;
        }
        Map<String, List<String>> data = sessionData.getData();

        // Let's create the jid and check that they are a local user
        JID account;
        try {
            account = new JID(get(data, "accountjid", 0));
        }
        catch (NullPointerException npe) {
            note.addAttribute("type", "error");
            note.setText("JID required parameter.");
            return;
        }
        if (!XMPPServer.getInstance().isLocal(account)) {
            note.addAttribute("type", "error");
            note.setText("Cannot create remote user.");
            return;
        }

        String password = get(data, "password", 0);
        String passwordRetry = get(data, "password-verify", 0);

        if (password == null || "".equals(password) || !password.equals(passwordRetry)) {
            note.addAttribute("type", "error");
            note.setText("Passwords do not match.");
            return;
        }

        String email = get(data, "email", 0);
        String givenName = get(data, "given_name", 0);
        String surName = get(data, "surname", 0);
        String name = (givenName == null ? "" : givenName) + (surName == null ? "" : surName);
        name = (name.equals("") ? null : name);

99 100 101 102 103 104 105
        // If provider requires email, validate
        if (UserManager.getUserProvider().isEmailRequired() && !StringUtils.isValidEmailAddress(email)) {
            note.addAttribute("type", "error");
            note.setText("No email was specified.");
            return;
        }

Alex Wenckus's avatar
Alex Wenckus committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        try {
            UserManager.getInstance().createUser(account.getNode(), password, name, email);
        }
        catch (UserAlreadyExistsException e) {
            note.addAttribute("type", "error");
            note.setText("User already exists.");
            return;
        }
        // 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("Adding a user");
Alex Wenckus's avatar
Alex Wenckus committed
122
        form.addInstruction("Fill out this form to add a user.");
Alex Wenckus's avatar
Alex Wenckus committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        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 the account to be added");
        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 = form.addField();
        field.setType(FormField.Type.text_private);
        field.setLabel("Retype password");
        field.setVariable("password-verify");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Email address");
        field.setVariable("email");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Given name");
        field.setVariable("given_name");

        field = form.addField();
        field.setType(FormField.Type.text_single);
        field.setLabel("Family name");
        field.setVariable("surname");

        // 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) {
173 174
        return (super.hasPermission(requester) || InternalComponentManager.getInstance().hasComponent(requester))
                && !UserManager.getUserProvider().isReadOnly();
Alex Wenckus's avatar
Alex Wenckus committed
175 176
    }
}