// Note: Only Sys Admins, Community Owner, and Community Moderators can remove users
boolean isOwnerOrModerator = SocialUtils.isOwnerOrModerator(communityIdStr, userIdStr);
boolean isSysAdmin = RESTTools.adminLookup(userIdStr);
boolean canRemove = (isOwnerOrModerator || isSysAdmin) ? true : false;
ResponsePojo rp = new ResponsePojo();
if (canRemove)
{
try
{
// Find person record to update
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(communityIdStr));
DBObject dbo = DbManager.getSocial().getCommunity().findOne(query);
if (dbo != null)
{
// Get GsonBuilder object with MongoDb de/serializers registered
CommunityPojo community = CommunityPojo.fromDb(dbo, CommunityPojo.class);
Boolean isMember = false;
Set<CommunityMemberPojo> cmps = null;
CommunityMemberPojo cmp = null;
if (community.getMembers() != null)
{
cmps = community.getMembers();
for (CommunityMemberPojo c : cmps)
{
if (c.get_id().toStringMongod().equals(personIdStr))
{
cmp = c;
isMember = true;
}
}
}
if (isMember)
{
cmps.remove(cmp);
community.setNumberOfMembers(community.getNumberOfMembers() - 1);
/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO (INF-1214): Make this code more robust to handle changes to the community that need to
// Caleb: this means change update to $set
/////////////////////////////////////////////////////////////////////////////////////////////////
DbManager.getSocial().getCommunity().update(query, community.toDb());
PersonHandler person = new PersonHandler();
person.removeCommunity(personIdStr, communityIdStr);
rp.setData(community, new CommunityPojoApiMap());
rp.setResponse(new ResponseObject("Remove member from community", true, "Member has been removed from community"));
}
else
{
rp.setResponse(new ResponseObject("Remove member from community",true,"Person is not a member of this community."));
}
}
else
{
rp.setResponse(new ResponseObject("Remove member from community", false, "Community not found."));
}
}
catch (Exception e)
{
// If an exception occurs log the error
logger.error("Exception Message: " + e.getMessage(), e);
rp.setResponse(new ResponseObject("Remove member from community", false,
"Error removing member from community " + e.getMessage()));
}
}
else
{
rp.setResponse(new ResponseObject("Remove member from community", false,
"The user does not have permissions to remove a user from the community."));
}
return rp;
}