Commit 0dc6204d authored by Holger Bergunde's avatar Holger Bergunde Committed by holger.bergunde

Added new Openfire plugin "Just married". Allows to change username or create...

Added new Openfire plugin "Just married". Allows to change username or create a copy of an existing user. 

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13205 b35dd754-fafc-0310-a699-88a17e54d16e
parent 1d43dab7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>GoJara Plugin Changelog</title>
<style type="text/css">
BODY {
font-size : 100%;
}
BODY, TD, TH {
font-family : tahoma, verdana, arial, helvetica, sans-serif;
font-size : 0.8em;
}
H2 {
font-size : 10pt;
font-weight : bold;
padding-left : 1em;
}
A:hover {
text-decoration : none;
}
H1 {
font-family : tahoma, arial, helvetica, sans-serif;
font-size : 1.4em;
font-weight: bold;
border-bottom : 1px #ccc solid;
padding-bottom : 2px;
}
TT {
font-family : courier new;
font-weight : bold;
color : #060;
}
PRE {
font-family : courier new;
font-size : 100%;
}
</style>
</head>
<body>
<h1>
Just married Plugin Changelog
</h1>
<p><b>1.0.0 </b> -- July 30, 2012</p>
<ul>
<li>Initial release. </li>
</ul>
</body>
</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.married.JustMarriedPlugin</class>
<name>Just married</name>
<description>Allows admins to rename or copy users</description>
<author>Holger Bergunde</author>
<version>1.0.0 </version>
<date>7/30/2012</date>
<minServerVersion>3.3.0</minServerVersion>
<adminconsole>
<tab id="tab-users">
<sidebar id="sidebar-users">
<item id="justmarried" name="Just married"
url="married.jsp"
description="Just married" />
</sidebar>
</tab>
</adminconsole>
</plugin>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Just married Plugin Readme</title>
<style type="text/css">
BODY {
font-size: 100%;
}
BODY,TD,TH {
font-family: tahoma, verdana, arial, helvetica, sans-serif;
font-size: 0.8em;
}
H2 {
font-size: 10pt;
font-weight: bold;
}
A:hover {
text-decoration: none;
}
H1 {
font-family: tahoma, arial, helvetica, sans-serif;
font-size: 1.4em;
font-weight: bold;
border-bottom: 1px #ccc solid;
padding-bottom: 2px;
}
TT {
font-family: courier new;
font-weight: bold;
color: #060;
}
PRE {
font-family: courier new;
font-size: 100%;
}
#datatable TH {
color: #fff;
background-color: #2A448C;
text-align: left;
}
#datatable TD {
background-color: #FAF6EF;
}
#datatable .name {
background-color: #DCE2F5;
}
</style>
</head>
<body>
<h1>Just married Plugin Readme</h1>
<h2>Overview</h2>
<p>This plugins allows admins to rename or copy users on the local
server. By default Openfire does not allow to rename users because the
username is the primary key in database. This plugin creates a new
contact and tries to copy roster entries, groups (including shared
groups), properties and vcard from the old contact to the new renamed
one. If you want you could keep the old user as well.</p>
<h2>Installation</h2>
<p>Copy justmarried.jar into the plugins directory of your Openfire
installation. The plugin will then be automatically deployed. To
upgrade to a new version, copy the new remoteRoster.jar file over the
existing file.</p>
<h2>Configuration</h2>
<p>The Just married plugin can be configured under
"User/Groups"-"Users"-"Just married".</p>
</body>
</html>
\ No newline at end of file
package org.jivesoftware.openfire.plugin.married;
import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.AuthFactory;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.openfire.vcard.VCardManager;
import org.xmpp.packet.StreamError;
public class JustMarriedPlugin implements Plugin {
private static Logger Log = Logger.getLogger(JustMarriedPlugin.class);
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
}
public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser) {
UserManager userManager = UserManager.getInstance();
try {
User currentUser = userManager.getUser(currentUserName);
// Old user found, create new one
String password = AuthFactory.getPassword(currentUserName);
User newUser = userManager.createUser(newUserName, password, currentUser.getName(), currentUser.getEmail());
newUser.setNameVisible(currentUser.isNameVisible());
newUser.setEmailVisible(currentUser.isEmailVisible());
newUser.setCreationDate(currentUser.getCreationDate());
copyRoster(currentUser, newUser, currentUserName);
copyProperties(currentUser, newUser);
copyToGroups(currentUserName, newUserName);
copyVCard(currentUserName, newUserName);
if (deleteOldUser) {
deleteUser(currentUser);
}
} catch (UserNotFoundException e) {
Log.error("Could not find user " + currentUserName, e);
return false;
} catch (UserAlreadyExistsException e) {
Log.error("Could not create user " + newUserName, e);
return false;
}
return true;
}
private static void copyVCard(String currentUserName, String newUserName) {
VCardManager vcardManager = VCardManager.getInstance();
Element vcard = vcardManager.getVCard(currentUserName);
if (vcard != null) {
try {
vcardManager.setVCard(newUserName, vcard);
} catch (Exception e) {
Log.error("Could not copy vcard to " + newUserName, e);
}
}
}
private static void copyToGroups(String currentUser, String newUser) {
GroupManager groupManager = GroupManager.getInstance();
for (Group group : groupManager.getGroups()) {
if (group.isUser(currentUser)) {
group.getMembers().add(XMPPServer.getInstance().createJID(newUser, null));
}
}
}
private static void deleteUser(User oldUser) {
UserManager.getInstance().deleteUser(oldUser);
final StreamError error = new StreamError(StreamError.Condition.not_authorized);
for (ClientSession sess : SessionManager.getInstance().getSessions(oldUser.getUsername())) {
sess.deliverRawText(error.toXML());
sess.close();
}
}
private static void copyProperties(User currentUser, User newUser) {
for (String key : currentUser.getProperties().keySet()) {
newUser.getProperties().put(key, User.getPropertyValue(currentUser.getUsername(), key));
}
}
private static void copyRoster(User currentUser, User newUser, String currentUserName) {
Roster newRoster = newUser.getRoster();
Roster currentRoster = currentUser.getRoster();
for (RosterItem item : currentRoster.getRosterItems()) {
try {
List<String> groups = item.getGroups();
RosterItem justCreated = newRoster.createRosterItem(item.getJid(), item.getNickname(), groups, true,
true);
justCreated.setAskStatus(item.getAskStatus());
justCreated.setRecvStatus(item.getRecvStatus());
justCreated.setSubStatus(item.getSubStatus());
for (Group gr : item.getSharedGroups()) {
justCreated.addSharedGroup(gr);
}
for (Group gr : item.getInvisibleSharedGroups()) {
justCreated.addInvisibleSharedGroup(gr);
}
addNewUserToOthersRoster(newUser, item, currentUserName);
} catch (UserAlreadyExistsException e) {
Log.error("Could not create roster item for user " + item.getJid(), e);
} catch (SharedGroupException e) {
Log.error("Could not create roster item for user " + item.getJid()
+ " because it is a contact from a shared group", e);
}
}
}
private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) {
otherItem.getJid();
UserManager userManager = UserManager.getInstance();
// Is this user registered with our OF server?
String username = otherItem.getJid().getNode();
if (username != null && username.length() > 0 && userManager.isRegisteredUser(username)
&& XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
try {
User otherUser = userManager.getUser(username);
Roster otherRoster = otherUser.getRoster();
RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(
currentUser, null));
try {
if (!oldUserOnOthersRoster.isOnlyShared()) {
RosterItem justCreated = otherRoster.createRosterItem(
XMPPServer.getInstance().createJID(newUser.getUsername(), null),
oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
}
} catch (UserAlreadyExistsException e) {
Log.error("Could not create roster item for user " + newUser.getUsername(), e);
} catch (SharedGroupException e) {
Log.error(e);
}
} catch (UserNotFoundException e) {
Log.error("Could not create roster item for user " + newUser.getUsername()
+ " because it is a contact from a shared group", e);
}
}
}
@Override
public void destroyPlugin() {
}
}
This diff is collapsed.
This diff is collapsed.
<%@ page
import="org.jivesoftware.openfire.plugin.married.JustMarriedPlugin"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<%
webManager.init(request, response, session, application, out);
String oldName = request.getParameter("oldName");
String newName = request.getParameter("newName");
String keepCopy = request.getParameter("copy");
%>
<html>
<head>
<title>Just married - name changer</title>
<meta name="pageID" content="justmarried" />
<meta name="helpPage" content="" />
<script src="./js/bootstrap.min.js" type="text/javascript"></script>
<link href="./css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="jive-contentBoxHeader">Just married</div>
<div class="jive-contentBox">
<%
if (oldName != null && newName != null && oldName.trim().length() > 0 && newName.trim().length() > 0) {
boolean success = JustMarriedPlugin.changeName(oldName, newName, keepCopy == null ? true : false);
if (success) {
out.write("<div class=\"success\">Sucessfully renamed user " + oldName + " to " + newName
+ "!</div>");
} else {
out.write("<div class=\"error\">Something went wrong :-/. Please have a closer look to the error log!</div>");
}
} else {
%>
<form class="form-horizontal">
<fieldset>
<legend>Change the name here</legend>
<label class="control-label" for="input01">Current username</label>
<div
<%out.write(oldName != null && oldName.length() == 0 ? "class=\"control-group error\""
: "class=\"controls\"");%>>
<input type="text" name="oldName" class="input-xlarge"
<%out.write(oldName != null && oldName.length() == 0 ? "id=\"inputError\"" : "id=\"input01\"");%>>
<p class="help-block">The current username e.g user.name
(without server)</p>
</div>
<label class="control-label" for="input01">New username</label>
<div
<%out.write(newName != null && newName.length() == 0 ? "class=\"control-group error\""
: "class=\"controls\"");%>>
<input type="text" name="newName" class="input-xlarge"
<%out.write(newName != null && newName.length() == 0 ? "id=\"inputError\"" : "id=\"input01\"");%>>
<p class="help-block">The new username e.g user.newname
(without server)</p>
</div>
<div class="control-group">
<label class="checkbox"> <input type="checkbox"
id="optionsCheckbox2" name="copy" value="keepCopy"> Keep a
copy of the old username
</label>
</div>
<div class="control-group">
<button type="submit" class="btn btn-primary">Rename user</button>
</div>
</fieldset>
</form>
<%
}
%>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment