RosterAccess.java 3.4 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
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.
Matt Tucker's avatar
Matt Tucker committed
19 20
 */

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

23
import java.util.Collection;
24

Matt Tucker's avatar
Matt Tucker committed
25 26 27
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
28 29
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.group.Group;
30 31
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
32
import org.jivesoftware.openfire.pubsub.Node;
33
import org.jivesoftware.openfire.roster.RosterManager;
34 35
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41 42 43 44 45
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;

/**
 * Anyone in the specified roster group(s) may subscribe and retrieve items.
 *
 * @author Matt Tucker
 */
public class RosterAccess extends AccessModel {

46 47
	private static final Logger Log = LoggerFactory.getLogger(RosterAccess.class);

Matt Tucker's avatar
Matt Tucker committed
48 49 50
    RosterAccess() {
    }

51
    @Override
52
	public boolean canSubscribe(Node node, JID owner, JID subscriber) {
53
        // Let node owners and sysadmins always subscribe to the node
54
        if (node.isAdmin(owner)) {
55 56
            return true;
        }
57
        for (JID nodeOwner : node.getOwners()) {
58
            if (nodeOwner.equals(owner)) {
59
                return true;
Matt Tucker's avatar
Matt Tucker committed
60
            }
61 62 63
        }
        // Check that the subscriber is a local user
        XMPPServer server = XMPPServer.getInstance();
64 65
        if (server.isLocal(owner)) {
            GroupManager gMgr = GroupManager.getInstance();
66
        	Collection<String> nodeGroups = node.getRosterGroupsAllowed();
67 68 69 70 71 72 73 74 75
        	for (String groupName : nodeGroups) {
        		try {
	        		Group group = gMgr.getGroup(groupName);
	        		// access allowed if the node group is visible to the subscriber
	        		if (server.getRosterManager().isGroupVisible(group, owner)) {
	        			return true;
	        		}
        		} catch (GroupNotFoundException gnfe){ 
        			// ignore
76 77 78 79 80 81 82
        		}
        	}
        }
        else {
            // Subscriber is a remote user. This should never happen.
            Log.warn("Node with access model Roster has a remote user as subscriber: " +
                    node.getNodeID());
Matt Tucker's avatar
Matt Tucker committed
83 84 85 86
        }
        return false;
    }

87 88
    @Override
	public boolean canAccessItems(Node node, JID owner, JID subscriber) {
Matt Tucker's avatar
Matt Tucker committed
89 90 91
        return canSubscribe(node, owner, subscriber);
    }

92 93
    @Override
	public String getName() {
Matt Tucker's avatar
Matt Tucker committed
94 95 96
        return "roster";
    }

97 98
    @Override
	public PacketError.Condition getSubsriptionError() {
Matt Tucker's avatar
Matt Tucker committed
99 100 101
        return PacketError.Condition.not_authorized;
    }

102 103
    @Override
	public Element getSubsriptionErrorDetail() {
Matt Tucker's avatar
Matt Tucker committed
104 105 106 107
        return DocumentHelper.createElement(
                QName.get("not-in-roster-group", "http://jabber.org/protocol/pubsub#errors"));
    }

108 109
    @Override
	public boolean isAuthorizationRequired() {
Matt Tucker's avatar
Matt Tucker committed
110 111 112
        return false;
    }
}