Search in sources :

Example 51 with Group

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

the class UserServicePlugin method createUser.

public void createUser(String username, String password, String name, String email, String groupNames) throws UserAlreadyExistsException, GroupAlreadyExistsException, UserNotFoundException, GroupNotFoundException {
    userManager.createUser(username, password, name, email);
    userManager.getUser(username);
    if (groupNames != null) {
        Collection<Group> groups = new ArrayList<Group>();
        StringTokenizer tkn = new StringTokenizer(groupNames, ",");
        while (tkn.hasMoreTokens()) {
            String groupName = tkn.nextToken();
            Group group = null;
            try {
                group = GroupManager.getInstance().getGroup(groupName);
            } catch (GroupNotFoundException e) {
                // Create this group ;
                group = GroupManager.getInstance().createGroup(groupName);
                group.getProperties().put("sharedRoster.showInRoster", "onlyGroup");
                group.getProperties().put("sharedRoster.displayName", groupName);
                group.getProperties().put("sharedRoster.groupList", "");
            }
            groups.add(group);
        }
        for (Group group : groups) {
            group.getMembers().add(server.createJID(username, null));
        }
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 52 with Group

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

the class UserServicePlugin method getUserGroups.

/**
	 * Returns all group names or an empty collection for specific user
	 * 
	 */
public Collection<String> getUserGroups(String username) throws UserNotFoundException {
    User user = getUser(username);
    Collection<Group> groups = GroupManager.getInstance().getGroups(user);
    Collection<String> groupNames = new ArrayList<String>();
    for (Group group : groups) {
        groupNames.add(group.getName());
    }
    return groupNames;
}
Also used : Group(org.jivesoftware.openfire.group.Group) User(org.jivesoftware.openfire.user.User) ArrayList(java.util.ArrayList)

Example 53 with Group

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

the class UserServicePluginNG method deleteUserFromGroups.

/**
	 * Delete user from groups.
	 *
	 * @param username
	 *            the username
	 * @param userGroupsEntity
	 *            the user groups entity
	 * @throws ServiceException
	 *             the service exception
	 */
public void deleteUserFromGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
    if (userGroupsEntity != null) {
        for (String groupName : userGroupsEntity.getGroupNames()) {
            Group group = null;
            try {
                group = GroupManager.getInstance().getGroup(groupName);
            } catch (GroupNotFoundException e) {
                throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND, Response.Status.NOT_FOUND, e);
            }
            group.getMembers().remove(server.createJID(username, null));
        }
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) ServiceException(org.jivesoftware.openfire.exceptions.ServiceException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 54 with Group

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

the class UserServicePluginNG method addUserToGroups.

/**
	 * Adds the user to group.
	 *
	 * @param username
	 *            the username
	 * @param userGroupsEntity
	 *            the user groups entity
	 * @throws ServiceException
	 * @throws GroupAlreadyExistsException
	 *             the group already exists exception
	 */
public void addUserToGroups(String username, UserGroupsEntity userGroupsEntity) throws ServiceException {
    if (userGroupsEntity != null) {
        Collection<Group> groups = new ArrayList<Group>();
        for (String groupName : userGroupsEntity.getGroupNames()) {
            Group group = null;
            try {
                group = GroupManager.getInstance().getGroup(groupName);
            } catch (GroupNotFoundException e) {
                // Create this group
                group = createGroup(groupName);
            }
            groups.add(group);
        }
        for (Group group : groups) {
            group.getMembers().add(server.createJID(username, null));
        }
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) ArrayList(java.util.ArrayList) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Example 55 with Group

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

the class LocalMUCRoom method applyAffiliationChange.

/**
     * Evaluate the given JID to determine what the appropriate affiliation should be
     * after a change has been made. Each affected user will be granted the highest
     * affiliation they now possess, either explicitly or implicitly via membership
     * in one or more groups. If the JID is a user, the effective affiliation is
     * applied to each presence corresponding to that user. If the given JID is a group,
     * each user in the group is evaluated to determine what their new affiliations will
     * be. The returned presence updates will be broadcast to the occupants of the room.
     * 
     * @param senderRole Typically the room itself, or an owner/admin
     * @param affiliationJID The JID for the user or group that has been changed
     * @param reason An optional reason to explain why a user was kicked from the room
     * @return List of presence updates to be delivered to the room's occupants
     */
private List<Presence> applyAffiliationChange(MUCRole senderRole, final JID affiliationJID, String reason) {
    // Update the presence(s) for the new affiliation and inform all occupants
    List<JID> affectedOccupants = new ArrayList<>();
    // first, determine which actual (user) JIDs are affected by the affiliation change
    if (GroupJID.isGroup(affiliationJID)) {
        try {
            Group group = GroupManager.getInstance().getGroup(affiliationJID);
            // if so, calculate a new affiliation (if any) for the occupant(s)
            for (JID groupMember : group.getAll()) {
                if (occupantsByBareJID.containsKey(groupMember)) {
                    affectedOccupants.add(groupMember);
                }
            }
        } catch (GroupNotFoundException gnfe) {
            Log.error("Error updating group presences for " + affiliationJID, gnfe);
        }
    } else {
        if (occupantsByBareJID.containsKey(affiliationJID)) {
            affectedOccupants.add(affiliationJID);
        }
    }
    // now update each of the affected occupants with a new role/affiliation
    MUCRole.Role newRole;
    MUCRole.Affiliation newAffiliation;
    List<Presence> updatedPresences = new ArrayList<>();
    // new role/affiliation may be granted via group membership
    for (JID occupantJID : affectedOccupants) {
        Log.info("Applying affiliation change for " + occupantJID);
        boolean kickMember = false, isOutcast = false;
        if (owners.includes(occupantJID)) {
            newRole = MUCRole.Role.moderator;
            newAffiliation = MUCRole.Affiliation.owner;
        } else if (admins.includes(occupantJID)) {
            newRole = MUCRole.Role.moderator;
            newAffiliation = MUCRole.Affiliation.admin;
        } else // outcast trumps member when an affiliation is changed
        if (outcasts.includes(occupantJID)) {
            newAffiliation = MUCRole.Affiliation.outcast;
            newRole = MUCRole.Role.none;
            kickMember = true;
            isOutcast = true;
        } else if (members.includesKey(occupantJID)) {
            newRole = MUCRole.Role.participant;
            newAffiliation = MUCRole.Affiliation.member;
        } else if (isMembersOnly()) {
            newRole = MUCRole.Role.none;
            newAffiliation = MUCRole.Affiliation.none;
            kickMember = true;
        } else {
            newRole = isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant;
            newAffiliation = MUCRole.Affiliation.none;
        }
        Log.info("New affiliation: " + newAffiliation);
        try {
            List<Presence> thisOccupant = changeOccupantAffiliation(senderRole, occupantJID, newAffiliation, newRole);
            if (kickMember) {
                // a status code of 301 indicates the user was removed as an outcast
                for (Presence presence : thisOccupant) {
                    presence.setType(Presence.Type.unavailable);
                    presence.setStatus(null);
                    Element x = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
                    if (reason != null && reason.trim().length() > 0) {
                        x.element("item").addElement("reason").setText(reason);
                    }
                    x.addElement("status").addAttribute("code", isOutcast ? "301" : "321");
                    kickPresence(presence, senderRole.getUserAddress(), senderRole.getNickname());
                }
            }
            updatedPresences.addAll(thisOccupant);
        } catch (NotAllowedException e) {
            Log.error("Error updating presences for " + occupantJID, e);
        }
    }
    return updatedPresences;
}
Also used : Group(org.jivesoftware.openfire.group.Group) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) MUCRole(org.jivesoftware.openfire.muc.MUCRole) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Aggregations

Group (org.jivesoftware.openfire.group.Group)83 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)41 JID (org.xmpp.packet.JID)30 ArrayList (java.util.ArrayList)22 Element (org.dom4j.Element)20 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)10 List (java.util.List)9 User (org.jivesoftware.openfire.user.User)9 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)7 StringTokenizer (java.util.StringTokenizer)6 HashMap (java.util.HashMap)5 GroupManager (org.jivesoftware.openfire.group.GroupManager)5 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)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 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2