PresenceAccess.java 2.96 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
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
Matt Tucker's avatar
Matt Tucker committed
11 12
 */

13
package org.jivesoftware.openfire.pubsub.models;
Matt Tucker's avatar
Matt Tucker committed
14 15 16 17 18

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.util.Log;
19 20 21 22 23
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.pubsub.Node;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.user.UserNotFoundException;
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;

/**
 * Anyone with a presence subscription of both or from may subscribe and retrieve items.
 *
 * @author Matt Tucker
 */
public class PresenceAccess extends AccessModel {

    PresenceAccess() {
    }

    public boolean canSubscribe(Node node, JID owner, JID subscriber) {
38 39 40 41
        // Let node owners and sysadmins always subcribe to the node
        if (node.isAdmin(owner)) {
            return true;
        }
Matt Tucker's avatar
Matt Tucker committed
42 43
        // Get the only owner of the node
        JID nodeOwner = node.getOwners().iterator().next();
44 45 46 47
        // Give access to the owner of the roster :)
        if (nodeOwner.toBareJID().equals(owner.toBareJID())) {
            return true;
        }
Matt Tucker's avatar
Matt Tucker committed
48 49 50 51 52 53 54 55
        // 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
Gaston Dombiak's avatar
Gaston Dombiak committed
56 57
                return item != null && (RosterItem.SUB_BOTH == item.getSubStatus() ||
                        RosterItem.SUB_FROM == item.getSubStatus());
Matt Tucker's avatar
Matt Tucker committed
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
            }
            catch (UserNotFoundException e) {
                return false;
            }
        }
        else {
            // Owner of the node is a remote user. This should never happen.
            Log.warn("Node with access model Presence 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 "presence";
    }

    public PacketError.Condition getSubsriptionError() {
        return PacketError.Condition.not_authorized;
    }

    public Element getSubsriptionErrorDetail() {
        return DocumentHelper.createElement(QName.get("presence-subscription-required",
                "http://jabber.org/protocol/pubsub#errors"));
    }

    public boolean isAuthorizationRequired() {
        return false;
    }
}