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
/**
* $RCSfile: $
* $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.wildfire.pubsub.models;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.group.Group;
import org.jivesoftware.wildfire.pubsub.Node;
import org.jivesoftware.wildfire.roster.Roster;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import java.util.ArrayList;
import java.util.List;
/**
* Anyone in the specified roster group(s) may subscribe and retrieve items.
*
* @author Matt Tucker
*/
public class RosterAccess extends AccessModel {
RosterAccess() {
}
public boolean canSubscribe(Node node, JID owner, JID subscriber) {
// Let node owners and sysadmins always subcribe to the node
if (node.isAdmin(owner)) {
return true;
}
// Get the only owner of the node
JID nodeOwner = node.getOwners().iterator().next();
// Give access to the owner of the roster :)
if (nodeOwner.toBareJID().equals(owner.toBareJID())) {
return true;
}
// Get the roster of the node owner
XMPPServer server = XMPPServer.getInstance();
// Check that the node owner is a local user
if (server.isLocal(nodeOwner)) {
try {
Roster roster = server.getRosterManager().getRoster(nodeOwner.getNode());
RosterItem item = roster.getRosterItem(owner);
// Check that the subscriber is subscribe to the node owner's presence
boolean isSubscribed = item != null && (
RosterItem.SUB_BOTH == item.getSubStatus() ||
RosterItem.SUB_FROM == item.getSubStatus());
if (isSubscribed) {
// Get list of groups where the contact belongs
List<String> contactGroups = new ArrayList<String>(item.getGroups());
for (Group group : item.getSharedGroups()) {
contactGroups.add(group.getName());
}
for (Group group : item.getInvisibleSharedGroups()) {
contactGroups.add(group.getName());
}
// Check if subscriber is present in the allowed groups of the node
return contactGroups.removeAll(node.getRosterGroupsAllowed());
}
}
catch (UserNotFoundException e) {
// Do nothing
}
}
else {
// Owner of the node is a remote user. This should never happen.
Log.warn("Node with access model Roster has a remote user as owner: " +
node.getNodeID());
}
return false;
}
public boolean canAccessItems(Node node, JID owner, JID subscriber) {
return canSubscribe(node, owner, subscriber);
}
public String getName() {
return "roster";
}
public PacketError.Condition getSubsriptionError() {
return PacketError.Condition.not_authorized;
}
public Element getSubsriptionErrorDetail() {
return DocumentHelper.createElement(
QName.get("not-in-roster-group", "http://jabber.org/protocol/pubsub#errors"));
}
public boolean isAuthorizationRequired() {
return false;
}
}