Search in sources :

Example 31 with Group

use of org.jivesoftware.openfire.group.Group in project Openfire by igniterealtime.

the class Roster method provideRosterItem.

/**
 * Generate a new RosterItem for use with createRosterItem.
 *
 * @param user       The roster jid address to create the roster item for.
 * @param nickname   The nickname to assign the item (or null for none).
 * @param groups     The groups the item belongs to (or null for none).
 * @param push       True if the new item must be push to the user.
 * @param persistent True if the new roster item should be persisted to the DB.
 * @return The newly created roster items ready to be stored by the Roster item's hash table
 * @throws UserAlreadyExistsException if the user is already in the roster
 * @throws SharedGroupException if the group is a shared group
 */
protected RosterItem provideRosterItem(JID user, String nickname, List<String> groups, boolean push, boolean persistent) throws UserAlreadyExistsException, SharedGroupException {
    if (groups != null && !groups.isEmpty()) {
        // Raise an error if the groups the item belongs to include a shared group
        for (String groupDisplayName : groups) {
            Collection<Group> groupsWithProp = GroupManager.getInstance().search("sharedRoster.displayName", groupDisplayName);
            for (Group group : groupsWithProp) {
                String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
                if (showInRoster != null && !showInRoster.equals("nobody")) {
                    throw new SharedGroupException("Cannot add an item to a shared group");
                }
            }
        }
    }
    org.xmpp.packet.Roster roster = new org.xmpp.packet.Roster();
    roster.setType(IQ.Type.set);
    org.xmpp.packet.Roster.Item item = roster.addItem(user, nickname, null, org.xmpp.packet.Roster.Subscription.none, groups);
    RosterItem rosterItem = new RosterItem(item);
    // Fire event indicating that a roster item is about to be added
    persistent = RosterEventDispatcher.addingContact(this, rosterItem, persistent);
    // Check if we need to make the new roster item persistent
    if (persistent) {
        rosterItem = RosterManager.getRosterItemProvider().createItem(username, rosterItem);
    }
    if (push) {
        // Broadcast the roster push to the user
        broadcast(roster);
    }
    rosterItems.put(user.toBareJID(), rosterItem);
    // Fire event indicating that a roster item has been added
    RosterEventDispatcher.contactAdded(this, rosterItem);
    return rosterItem;
}
Also used : Group(org.jivesoftware.openfire.group.Group)

Example 32 with Group

use of org.jivesoftware.openfire.group.Group in project Openfire by igniterealtime.

the class Roster method addSharedUser.

/**
 * Adds a new contact that belongs to a certain list of groups to the roster. Depending on
 * the contact's groups and this user's groups, the presence subscription of the roster item may
 * vary.
 *
 * @param addedUser the new contact to add to the roster
 * @param groups    the groups where the contact is a member
 */
void addSharedUser(JID addedUser, Collection<Group> groups, Group addedGroup) {
    boolean newItem;
    RosterItem item;
    try {
        // Get the RosterItem for the *local* user to add
        item = getRosterItem(addedUser);
        newItem = false;
    } catch (UserNotFoundException e) {
        try {
            // Create a new RosterItem for this new user
            String nickname = UserNameManager.getUserName(addedUser);
            item = new RosterItem(addedUser, RosterItem.SUB_BOTH, RosterItem.ASK_NONE, RosterItem.RECV_NONE, nickname, null);
            // Add the new item to the list of items
            rosterItems.put(item.getJid().toBareJID(), item);
            newItem = true;
        } catch (UserNotFoundException ex) {
            Log.error("Couldn't find a user with username (" + addedUser + ")");
            return;
        }
    }
    // Update the subscription of the item **based on the item groups**
    Collection<Group> userGroups = GroupManager.getInstance().getGroups(getUserJID());
    // Set subscription type to BOTH if the roster user belongs to a shared group
    // that is mutually visible with a shared group of the new roster item
    final RosterManager rosterManager = XMPPServer.getInstance().getRosterManager();
    if (rosterManager.hasMutualVisibility(getUsername(), userGroups, addedUser, groups)) {
        item.setSubStatus(RosterItem.SUB_BOTH);
        for (Group group : groups) {
            if (rosterManager.isGroupVisible(group, getUserJID())) {
                // Add the shared group to the list of shared groups
                item.addSharedGroup(group);
            }
        }
        // BOTH subscription
        for (Group group : userGroups) {
            if (!group.isUser(addedUser) && rosterManager.isGroupVisible(group, addedUser)) {
                // Add the shared group to the list of invisible shared groups
                item.addInvisibleSharedGroup(group);
            }
        }
    } else {
        // If an item already exists then take note of the old subscription status
        RosterItem.SubType prevSubscription = null;
        if (!newItem) {
            prevSubscription = item.getSubStatus();
        }
        // Assume by default that the contact has subscribed from the presence of
        // this user
        item.setSubStatus(RosterItem.SUB_FROM);
        // Check if the user may see the new contact in a shared group
        for (Group group : groups) {
            if (rosterManager.isGroupVisible(group, getUserJID())) {
                // Add the shared group to the list of shared groups
                item.addSharedGroup(group);
                item.setSubStatus(RosterItem.SUB_TO);
            }
        }
        if (item.getSubStatus() == RosterItem.SUB_FROM) {
            item.addInvisibleSharedGroup(addedGroup);
        }
        // changed to BOTH based on the old and new subscription status
        if (prevSubscription != null) {
            if (prevSubscription == RosterItem.SUB_TO && item.getSubStatus() == RosterItem.SUB_FROM) {
                item.setSubStatus(RosterItem.SUB_BOTH);
            } else if (prevSubscription == RosterItem.SUB_FROM && item.getSubStatus() == RosterItem.SUB_TO) {
                item.setSubStatus(RosterItem.SUB_BOTH);
            }
        }
    }
    // Optimization: Check if we do not need to keep the item in memory
    if (item.isOnlyShared() && item.getSubStatus() == RosterItem.SUB_FROM) {
        // Remove from memory and do nothing else
        rosterItems.remove(item.getJid().toBareJID());
        // Cache information about shared contacts with subscription status FROM
        implicitFrom.put(item.getJid().toBareJID(), item.getInvisibleSharedGroupsNames());
    } else {
        // Remove from list of shared contacts with status FROM (if any)
        implicitFrom.remove(item.getJid().toBareJID());
        // Ensure that the item is an explicit roster item
        rosterItems.put(item.getJid().toBareJID(), item);
        // Brodcast to all the user resources of the updated roster item
        broadcast(item, true);
        // Probe the presence of the new group user
        if (item.getSubStatus() == RosterItem.SUB_BOTH || item.getSubStatus() == RosterItem.SUB_TO) {
            probePresence(item.getJid());
        }
    }
    if (newItem) {
        // Fire event indicating that a roster item has been added
        RosterEventDispatcher.contactAdded(this, item);
    } else {
        // Fire event indicating that a roster item has been updated
        RosterEventDispatcher.contactUpdated(this, item);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Group(org.jivesoftware.openfire.group.Group)

Example 33 with Group

use of org.jivesoftware.openfire.group.Group in project Openfire by igniterealtime.

the class RosterAccess method canSubscribe.

@Override
public boolean canSubscribe(Node node, JID owner, JID subscriber) {
    // Let node owners and sysadmins always subscribe to the node
    if (node.isAdmin(owner)) {
        return true;
    }
    for (JID nodeOwner : node.getOwners()) {
        if (nodeOwner.equals(owner)) {
            return true;
        }
    }
    // Check that the subscriber is a local user
    XMPPServer server = XMPPServer.getInstance();
    if (server.isLocal(owner)) {
        GroupManager gMgr = GroupManager.getInstance();
        Collection<String> nodeGroups = node.getRosterGroupsAllowed();
        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
            }
        }
    } else {
        // Subscriber is a remote user. This should never happen.
        Log.warn("Node with access model Roster has a remote user as subscriber: {}", node.getUniqueIdentifier());
    }
    return false;
}
Also used : Group(org.jivesoftware.openfire.group.Group) XMPPServer(org.jivesoftware.openfire.XMPPServer) JID(org.xmpp.packet.JID) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) GroupManager(org.jivesoftware.openfire.group.GroupManager)

Example 34 with Group

use of org.jivesoftware.openfire.group.Group in project Openfire by igniterealtime.

the class CrowdAdminProvider method getAdmins.

@Override
public List<JID> getAdmins() {
    List<JID> results = new ArrayList<>();
    GroupProvider provider = GroupManager.getInstance().getProvider();
    String groups = JiveGlobals.getProperty(JIVE_AUTHORIZED_GROUPS);
    groups = (groups == null || groups.trim().length() == 0) ? "" : groups;
    // make sure the property is created
    JiveGlobals.setProperty(JIVE_AUTHORIZED_GROUPS, groups);
    StringTokenizer tokenizer = new StringTokenizer(groups, ",");
    while (tokenizer.hasMoreTokens()) {
        String groupName = tokenizer.nextToken().trim().toLowerCase();
        if (groupName != null && groupName.length() > 0) {
            try {
                LOG.info("Adding admin users from group: " + groupName);
                Group group = provider.getGroup(groupName);
                if (group != null) {
                    results.addAll(group.getMembers());
                }
            } catch (GroupNotFoundException gnfe) {
                LOG.error("Error when trying to load the members of group:" + String.valueOf(groupName), gnfe);
            }
        }
    }
    if (results.isEmpty()) {
        // Add default admin account when none was specified
        results.add(new JID("admin", XMPPServer.getInstance().getServerInfo().getXMPPDomain(), null, true));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("admin users: " + results.toString());
    }
    return results;
}
Also used : Group(org.jivesoftware.openfire.group.Group) StringTokenizer(java.util.StringTokenizer) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) GroupProvider(org.jivesoftware.openfire.group.GroupProvider)

Example 35 with Group

use of org.jivesoftware.openfire.group.Group in project Openfire by igniterealtime.

the class CrowdGroupProvider method getGroup.

@Override
public Group getGroup(String name) throws GroupNotFoundException {
    try {
        Cache<String, org.jivesoftware.openfire.crowd.jaxb.Group> groupCache = CacheFactory.createLocalCache(GROUP_CACHE_NAME);
        org.jivesoftware.openfire.crowd.jaxb.Group group = groupCache.get(name);
        if (group == null) {
            group = manager.getGroup(name);
            groupCache.put(name, group);
        }
        Collection<JID> members = getGroupMembers(name);
        Collection<JID> admins = Collections.emptyList();
        return new Group(name, group.description, members, admins);
    } catch (RemoteException re) {
        LOG.error("Failure to load group:" + String.valueOf(name), re);
        throw new GroupNotFoundException(re);
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) JID(org.xmpp.packet.JID) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) RemoteException(java.rmi.RemoteException)

Aggregations

Group (org.jivesoftware.openfire.group.Group)84 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)42 JID (org.xmpp.packet.JID)30 Element (org.dom4j.Element)20 ArrayList (java.util.ArrayList)19 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)11 User (org.jivesoftware.openfire.user.User)9 List (java.util.List)7 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)7 StringTokenizer (java.util.StringTokenizer)6 GroupManager (org.jivesoftware.openfire.group.GroupManager)5 HashMap (java.util.HashMap)4 GroupEntity (org.jivesoftware.openfire.plugin.rest.entity.GroupEntity)4 DataForm (org.xmpp.forms.DataForm)4 IQ (org.xmpp.packet.IQ)4 HashSet (java.util.HashSet)3 XMPPServer (org.jivesoftware.openfire.XMPPServer)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2