openfire修改程序,使得在同一个分组里的用户,自动和该组admin互为好友,另外当把用户从组里删除时,自动删除和admin之间的好友列表
在group-edit.jsp中,修改程序,标记为黑体为新添加的,
注意在create和update之间需要sleep一段实践,不然可能会造成客户端(spark)处理会有问题,因为spark是并发处理消息的,
<% // Get parameters
boolean add = request.getParameter("add") != null;
boolean delete = request.getParameter("remove") != null;
boolean updateMember = request.getParameter("updateMember") != null;
boolean update = request.getParameter("save") != null;
boolean cancel = request.getParameter("cancel") != null;
String username = ParamUtils.getParameter(request, "username");
String [] adminIDs = ParamUtils.getParameters(request, "admin");
String [] deleteMembers = ParamUtils.getParameters(request, "delete");
String groupName = ParamUtils.getParameter(request, "group");
GroupManager groupManager = webManager.getGroupManager();
boolean groupInfoChanged = ParamUtils.getBooleanParameter(request, "groupChanged", false);
Map
// Get the presence manager
PresenceManager presenceManager = webManager.getPresenceManager();
UserManager userManager = webManager.getUserManager();
boolean enableRosterGroups = ParamUtils.getBooleanParameter(request,"enableRosterGroups");
boolean shareAdditional = ParamUtils.getParameter(request, "shareContactList") != null;
String groupDisplayName = ParamUtils.getParameter(request,"groupDisplayName");
String showGroup = ParamUtils.getParameter(request,"showGroup");
String[] groupNames = ParamUtils.getParameters(request, "groupNames");
Group group = groupManager.getGroup(groupName);
boolean success;
StringBuffer errorBuf = new StringBuffer();
if (cancel) {
response.sendRedirect("group-summary.jsp");
return;
}
if (update) {
if (enableRosterGroups && (groupDisplayName == null || groupDisplayName.trim().length() == 0)) {
errors.put("groupDisplayName", "");
}
if (errors.isEmpty()) {
if (enableRosterGroups) {
if (showGroup == null || !shareAdditional) {
showGroup = "onlyGroup";
}
if ("spefgroups".equals(showGroup)) {
showGroup = "onlyGroup";
}
else {
groupNames = new String[] {};
}
group.getProperties().put("sharedRoster.showInRoster", showGroup);
if (groupDisplayName != null) {
group.getProperties().put("sharedRoster.displayName", groupDisplayName);
}
group.getProperties().put("sharedRoster.groupList", toList(groupNames, "UTF-8"));
if (!SecurityAuditManager.getSecurityAuditProvider().blockGroupEvents()) {
// Log the event
webManager.logEvent("enabled roster groups for "+groupName, "showinroster = "+showGroup+"\ndisplayname = "+groupDisplayName+"\ngrouplist = "+toList(groupNames, "UTF-8"));
}
}
else {
group.getProperties().put("sharedRoster.showInRoster", "nobody");
group.getProperties().put("sharedRoster.displayName", "");
group.getProperties().put("sharedRoster.groupList", "");
if (!SecurityAuditManager.getSecurityAuditProvider().blockGroupEvents()) {
// Log the event
webManager.logEvent("disabled roster groups for "+groupName, null);
}
}
// Get admin list and compare it the admin posted list.
response.sendRedirect("group-edit.jsp?group=" + URLEncoder.encode(groupName, "UTF-8") + "&groupChanged=true");
return;
}
else {
// Continue editing since there are some errors
updateMember = false;
}
}
if (updateMember) {
Set
for (String adminID : adminIDs) {
JID newAdmin = new JID(adminID);
adminIDSet.add(newAdmin);
boolean isAlreadyAdmin = group.getAdmins().contains(newAdmin);
if (!isAlreadyAdmin) {
// Add new admin
group.getAdmins().add(newAdmin);
Collection
for(JID member:members){
Roster roster = webManager.getRosterManager().getRoster(member.getNode());
List
groups.add("\u6211\u7684\u597d\u53cb");
String nickName = UserManager.getInstance().getUser(newAdmin.getNode()).getName();
roster.createRosterItem(newAdmin, nickName, groups, true, true);
Roster roster1 = webManager.getRosterManager().getRoster(newAdmin.getNode());
String nickName1 = UserManager.getInstance().getUser(member.getNode()).getName();
roster1.createRosterItem(member, nickName1, groups, true, true);
try{
Thread.sleep(1000);
}catch(Exception e){
}
RosterItem item = roster.getRosterItem(newAdmin);
item.setNickname(nickName);
item.setSubStatus(RosterItem.SUB_BOTH);
roster.updateRosterItem(item);
RosterItem item1 = roster1.getRosterItem(member);
item1.setNickname(nickName1);
item1.setSubStatus(RosterItem.SUB_BOTH);
roster1.updateRosterItem(item1);
}
}
}
Collection
Set
for (JID admin : admins) {
if (!adminIDSet.contains(admin)) {
removeList.add(admin);
}
}
Collection
for (JID member : removeList) {
group.getMembers().add(member);
for(JID jid:members){
Roster roster = webManager.getRosterManager().getRoster(jid.getNode());
roster.deleteRosterItem(member,false);
Roster roster1 = webManager.getRosterManager().getRoster(member.getNode());
roster1.deleteRosterItem(jid,false);
}
}
if (!SecurityAuditManager.getSecurityAuditProvider().blockGroupEvents()) {
// Log the event
// TODO: Should log more here later
webManager.logEvent("updated group membership for "+groupName, null);
}
// Get admin list and compare it the admin posted list.
response.sendRedirect("group-edit.jsp?group=" + URLEncoder.encode(groupName, "UTF-8") + "&updatesuccess=true");
return;
}
else if (add && username != null) {
int count = 0;
username = username.trim();
username = username.toLowerCase();
if (username.indexOf('@') != -1) {
try {
UserManager.getInstance().getUser(JID.escapeNode(username));
// That means that this user has an email address as their node.
username = JID.escapeNode(username);
}
catch (UserNotFoundException e) {
}
}
// Add to group as member by default.
try {
boolean added;
if (username.indexOf('@') == -1) {
// No @ was found so assume this is a JID of a local user
username = JID.escapeNode(username);
username = Stringprep.nodeprep(username);
User user = UserManager.getInstance().getUser(username);
JID newUser = webManager.getXMPPServer().createJID(username, null);
added = group.getMembers().add(newUser);
Collection
for(JID admin:admins){
Roster roster = webManager.getRosterManager().getRoster(admin.getNode());
List
groups.add("\u6211\u7684\u597d\u53cb");
roster.createRosterItem(newUser, user.getName(), groups, true, true);
Roster roster1 = webManager.getRosterManager().getRoster(newUser.getNode());
String nickName1 = UserManager.getInstance().getUser(admin.getNode()).getName();
roster1.createRosterItem(admin, nickName1, groups, true, true);
try{
Thread.sleep(1000);
}catch(Exception e){
}
RosterItem item = roster.getRosterItem(newUser);
item.setNickname(user.getName());
item.setSubStatus(RosterItem.SUB_BOTH);
roster.updateRosterItem(item);
RosterItem item1 = roster1.getRosterItem(admin);
item1.setNickname(nickName1);
item1.setSubStatus(RosterItem.SUB_BOTH);
roster1.updateRosterItem(item1);
}
}
else {
// Admin entered a JID. Add the JID directly to the list of group members
JID newUser = new JID(username);
added = group.getMembers().add(newUser);
Collection
for(JID admin:admins){
Roster roster = webManager.getRosterManager().getRoster(admin.getNode());
List
groups.add("\u6211\u7684\u597d\u53cb");
String nickName = UserManager.getInstance().getUser(username).getName();
roster.createRosterItem(newUser, nickName, groups, true, true);
Roster roster1 = webManager.getRosterManager().getRoster(newUser.getNode());
String nickName1 = UserManager.getInstance().getUser(admin.getNode()).getName();
roster1.createRosterItem(admin, nickName1, groups, true, true);
try{
Thread.sleep(1000);
}catch(Exception e){
}
RosterItem item = roster.getRosterItem(newUser);
item.setNickname(nickName);
item.setSubStatus(RosterItem.SUB_BOTH);
roster.updateRosterItem(item);
RosterItem item1 = roster1.getRosterItem(admin);
item1.setNickname(nickName1);
item1.setSubStatus(RosterItem.SUB_BOTH);
roster1.updateRosterItem(item1);
}
if (!SecurityAuditManager.getSecurityAuditProvider().blockGroupEvents()) {
// Log the event
webManager.logEvent("added group member to "+groupName, "username = "+username);
}
}
if (added) {
count++;
}
else {
errorBuf.append("
").append(
LocaleUtils.getLocalizedString("group.edit.already_user", Arrays.asList(username)));
}
}
catch (Exception e) {
Log.warn("Problem adding new user to existing group", e);
errorBuf.append("
").append(
LocaleUtils.getLocalizedString("group.edit.inexistent_user", Arrays.asList(username)));
}
if (count > 0) {
response.sendRedirect("group-edit.jsp?group=" +
URLEncoder.encode(groupName, "UTF-8") + "&success=true");
return;
}
else {
success = false;
add = true;
}
}
else if(add && username == null){
add = false;
}
else if (delete) {
for (String deleteMember : deleteMembers) {
JID member = new JID(deleteMember);
if(group.getAdmins().contains(member)){
for(JID jid:group.getMembers()){
Roster roster = webManager.getRosterManager().getRoster(jid.getNode());
roster.deleteRosterItem(member,false);
Roster roster1 = webManager.getRosterManager().getRoster(member.getNode());
roster1.deleteRosterItem(jid,false);
}
}else{
for(JID jid:group.getAdmins()){
Roster roster = webManager.getRosterManager().getRoster(jid.getNode());
roster.deleteRosterItem(member,false);
Roster roster1 = webManager.getRosterManager().getRoster(member.getNode());
roster1.deleteRosterItem(jid,false);
}
}
group.getMembers().remove(member);
group.getAdmins().remove(member);
}
response.sendRedirect("group-edit.jsp?group=" + URLEncoder.encode(groupName, "UTF-8") + "&deletesuccess=true");
return;
}
success = groupInfoChanged || "true".equals(request.getParameter("success")) ||
"true".equals(request.getParameter("deletesuccess")) ||
"true".equals(request.getParameter("updatesuccess")) ||
"true".equals(request.getParameter("creategroupsuccess"));
if (errors.size() == 0) {
showGroup = group.getProperties().get("sharedRoster.showInRoster");
enableRosterGroups = !"nobody".equals(showGroup);
shareAdditional = "everybody".equals(showGroup);
if ("onlyGroup".equals(showGroup)) {
String glist = group.getProperties().get("sharedRoster.groupList");
List
if (glist != null) {
StringTokenizer tokenizer = new StringTokenizer(glist,",\t\n\r\f");
while (tokenizer.hasMoreTokens()) {
String tok = tokenizer.nextToken().trim();
l.add(tok.trim());
}
if (!l.isEmpty()) {
shareAdditional = true;
}
}
groupNames = l.toArray(new String[]{});
}
groupDisplayName = group.getProperties().get("sharedRoster.displayName");
}
%>