1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* $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.openfire.pubsub;
import org.dom4j.Element;
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.util.LocaleUtils;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.JID;
import java.util.Arrays;
import java.util.List;
/**
* Ad-hoc command that sends pending subscriptions to node owners.
*
* @author Matt Tucker
*/
public class PendingSubscriptionsCommand extends AdHocCommand {
private PubSubService service;
public PendingSubscriptionsCommand(PubSubService service) {
this.service = service;
}
protected void addStageInformation(SessionData data, Element command) {
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());
}
public void execute(SessionData data, Element command) {
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"));
}
}
}
public String getCode() {
return "http://jabber.org/protocol/pubsub#get-pending";
}
public String getDefaultLabel() {
return LocaleUtils.getLocalizedString("pubsub.command.pending-subscriptions.label");
}
protected List<Action> getActions(SessionData data) {
return Arrays.asList(Action.complete);
}
protected Action getExecuteAction(SessionData data) {
return Action.complete;
}
public int getMaxStages(SessionData data) {
return 1;
}
public boolean hasPermission(JID requester) {
// 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;
}
}