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;
}
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);
}
}
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;
}
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;
}
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);
}
}
Aggregations