PendingSubscriptionsCommand.java 5.07 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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.
Matt Tucker's avatar
Matt Tucker committed
18 19
 */

20
package org.jivesoftware.openfire.pubsub;
Matt Tucker's avatar
Matt Tucker committed
21 22

import org.dom4j.Element;
23 24
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
25
import org.jivesoftware.util.LocaleUtils;
Matt Tucker's avatar
Matt Tucker committed
26 27 28 29
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;

30
import java.util.Collections;
Matt Tucker's avatar
Matt Tucker committed
31 32 33 34 35 36 37
import java.util.List;

/**
 * Ad-hoc command that sends pending subscriptions to node owners.
 *
 * @author Matt Tucker
 */
38
public class PendingSubscriptionsCommand extends AdHocCommand {
Matt Tucker's avatar
Matt Tucker committed
39 40 41

    private PubSubService service;

42
    public PendingSubscriptionsCommand(PubSubService service) {
Matt Tucker's avatar
Matt Tucker committed
43 44 45
        this.service = service;
    }

46 47
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Matt Tucker's avatar
Matt Tucker committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle(LocaleUtils.getLocalizedString("pubsub.command.pending-subscriptions.title"));
        form.addInstruction(
                LocaleUtils.getLocalizedString("pubsub.command.pending-subscriptions.instruction"));

        FormField formField = form.addField();
        formField.setVariable("pubsub#node");
        formField.setType(FormField.Type.list_single);
        formField.setLabel(
                LocaleUtils.getLocalizedString("pubsub.command.pending-subscriptions.node"));
        for (Node node : service.getNodes()) {
            if (!node.isCollectionNode() && node.isAdmin(data.getOwner())) {
                formField.addOption(null, node.getNodeID());
            }
        }
        // Add the form to the command
        command.add(form.getElement());
    }

67 68
    @Override
	public void execute(SessionData data, Element command) {
Matt Tucker's avatar
Matt Tucker committed
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 99 100 101 102 103 104 105 106 107 108 109 110
        Element note = command.addElement("note");
        List<String> nodeIDs = data.getData().get("pubsub#node");
        if (nodeIDs.isEmpty()) {
            // No nodeID was provided by the requester
            note.addAttribute("type", "error");
            note.setText(LocaleUtils.getLocalizedString(
                    "pubsub.command.pending-subscriptions.error.idrequired"));
        }
        else if (nodeIDs.size() > 1) {
            // More than one nodeID was provided by the requester
            note.addAttribute("type", "error");
            note.setText(LocaleUtils.getLocalizedString(
                    "pubsub.command.pending-subscriptions.error.manyIDs"));
        }
        else {
            Node node = service.getNode(nodeIDs.get(0));
            if (node != null) {
                if (node.isAdmin(data.getOwner())) {
                    note.addAttribute("type", "info");
                    note.setText(LocaleUtils.getLocalizedString(
                            "pubsub.command.pending-subscriptions.success"));

                    for (NodeSubscription subscription : node.getPendingSubscriptions()) {
                        subscription.sendAuthorizationRequest(data.getOwner());
                    }
                }
                else {
                    // Requester is not an admin of the specified node
                    note.addAttribute("type", "error");
                    note.setText(LocaleUtils.getLocalizedString(
                            "pubsub.command.pending-subscriptions.error.forbidden"));
                }
            }
            else {
                // Node with the specified nodeID was not found
                note.addAttribute("type", "error");
                note.setText(LocaleUtils.getLocalizedString(
                        "pubsub.command.pending-subscriptions.error.badid"));
            }
        }
    }

111 112
    @Override
	public String getCode() {
Matt Tucker's avatar
Matt Tucker committed
113 114 115
        return "http://jabber.org/protocol/pubsub#get-pending";
    }

116 117
    @Override
	public String getDefaultLabel() {
Matt Tucker's avatar
Matt Tucker committed
118 119 120
        return LocaleUtils.getLocalizedString("pubsub.command.pending-subscriptions.label");
    }

121 122
    @Override
	protected List<Action> getActions(SessionData data) {
123
        return Collections.singletonList(Action.complete);
Matt Tucker's avatar
Matt Tucker committed
124 125
    }

126 127
    @Override
	protected Action getExecuteAction(SessionData data) {
Matt Tucker's avatar
Matt Tucker committed
128 129 130
        return Action.complete;
    }

131 132
    @Override
	public int getMaxStages(SessionData data) {
Matt Tucker's avatar
Matt Tucker committed
133 134 135
        return 1;
    }

136 137
    @Override
	public boolean hasPermission(JID requester) {
Matt Tucker's avatar
Matt Tucker committed
138 139 140 141 142 143 144 145 146
        // User has permission if he is an owner of at least one node or is a sysadmin
        for (Node node : service.getNodes()) {
            if (!node.isCollectionNode() && node.isAdmin(requester)) {
                return true;
            }
        }
        return false;
    }
}