r1284 - in xwiki/trunk/src/main/java/com/xpn/xwiki/plugin: . userdirectory usertools

Jeremi Joslin jeremi at users.forge.objectweb.org
Tue Sep 5 16:32:13 CEST 2006


Author: jeremi
Date: 2006-09-05 16:32:13 +0200 (Tue, 05 Sep 2006)
New Revision: 1284

Added:
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/Group.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPlugin.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPluginAPI.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementTools.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsAPI.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsImpl.java
Log:
* Add UserDirectory Plugin
* Add some user management tools as a plugin

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/Group.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/Group.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/Group.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,403 @@
+package com.xpn.xwiki.plugin.userdirectory;
+
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.objects.BaseObject;
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.XWiki;
+import com.xpn.xwiki.api.Document;
+import com.xpn.xwiki.api.Object;
+import com.xpn.xwiki.plugin.PluginException;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Vector;
+import java.util.Iterator;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: jeremi
+ * Date: Aug 16, 2006
+ * Time: 2:06:34 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class Group {
+    private Document        doc;
+    private Object          objDirectoryGroup;
+    private boolean         isNew = false;
+
+    public static final int ERROR_USERDIRECTORYPLUGIN_GROUP_UNKNOWN = 0;
+    public static final int ERROR_USERDIRECTORYPLUGIN_GROUP_DOESNT_EXIST = 1;
+
+    public Group(XWikiDocument doc, XWikiContext context) throws XWikiException {
+        this(new Document(doc, context), context);
+    }
+
+    public Group(Document doc, XWikiContext context) throws XWikiException {
+        reload(doc, context);
+    }
+
+    public void reload(XWikiContext context) throws XWikiException {
+        reload(null, context);
+    }
+
+    public void reload(Document doc, XWikiContext context) throws XWikiException {
+        if (doc == null)
+            doc = new Document(context.getWiki().getDocument(this.doc.getFullName(), context), context);
+        this.doc = doc;
+
+        BaseClass dirGrpClass = getXWikiDirectoryGroupClass(context);
+        Object obj;
+
+        if ((obj = doc.getObject(dirGrpClass.getName())) == null){
+            doc.createNewObject(dirGrpClass.getName());
+            obj = doc.getObject(dirGrpClass.getName());
+            this.isNew = true;
+        }
+        this.objDirectoryGroup = obj;
+    }
+
+    public boolean isNew() {
+        return isNew;
+    }
+
+    public void setName(String name){
+        objDirectoryGroup.set("name", name);
+    }
+
+    public String getName(){
+        return (String) objDirectoryGroup.get("name");
+    }
+
+    public String getPageName(){
+        return doc.getFullName();
+    }
+
+    public void set(String key, java.lang.Object value, XWikiContext context){
+        objDirectoryGroup.set(key, value);
+    }
+
+    public java.lang.Object get(String key, XWikiContext context){
+        return objDirectoryGroup.get(key);
+    }
+
+    public List getUsersPageName(XWikiContext context) throws XWikiException {
+        List usersPageName = new ArrayList();
+        List objs = doc.getObjects(getXWikiGroupsClass(context).getName());
+        Iterator it = objs.iterator();
+        while(it.hasNext())
+        {
+            usersPageName.add(((Object)it.next()).get("member"));
+        }
+        return usersPageName;
+    }
+
+    /**
+     *  Add a parent to this group
+     * @param name the name of the page of the parent group
+     * @param context
+     * @return false if the group is already parent of this one
+     * @throws XWikiException if the parent group doesn't exist
+     */
+    public boolean addParentName(String name, UserDirectoryPlugin userDirPlugin, XWikiContext context) throws XWikiException {
+        if (isParentPage(name, context))
+            return false;
+        Group parentGroup = getGroup(name, context);
+        if (parentGroup.isNew())
+            throw new PluginException(UserDirectoryPlugin.class, ERROR_USERDIRECTORYPLUGIN_GROUP_DOESNT_EXIST, "This group doesn't exist");
+        addParentName(name, context);
+        return true;
+    }
+
+    public boolean isParentPage(String name, XWikiContext context) throws XWikiException {
+        List parentsPage = getParentPages(context);
+        Iterator it = parentsPage.iterator();
+        while(it.hasNext())
+        {
+            String pageName = (String) it.next();
+            if (pageName.equals(name))
+                return true;
+        }
+        return false;
+    }
+
+    public boolean removeParent(String name, UserDirectoryPlugin userDirPlugin, XWikiContext context) throws XWikiException {
+        if (!isParentPage(name, context))
+            return false;
+        removeParent(name, context);
+        return true;
+    }
+
+    private void removeParent(String name, XWikiContext context) throws XWikiException {
+        String className = getXWikiGroupRelationClass(context).getName();
+        Vector objs = doc.getObjects(className);
+        Iterator it = objs.iterator();
+
+        while(it.hasNext()){
+            Object obj = (Object) it.next();
+            if (obj.get("parentpage").equals(name)){
+                doc.removeObject(obj);
+                return;
+            }
+        }
+    }
+
+
+    private void addParentName(String name, XWikiContext context) throws XWikiException {
+        String className = getXWikiGroupRelationClass(context).getName();
+        int nb = doc.createNewObject(className);
+
+        Object obj = doc.getObject(className, nb);
+        obj.set("parentpage", name);
+    }
+
+    /**
+     *
+     * @param context
+     * @return the list of parent group's page
+     * @throws XWikiException
+     */
+    public List getParentPages(XWikiContext context) throws XWikiException {
+        ArrayList parents = new ArrayList();
+
+        Vector objs = doc.getObjects(getXWikiGroupRelationClass(context).getName());
+        if (objs == null)
+            return parents;
+
+        Iterator it = objs.iterator();
+        while (it.hasNext())
+        {
+            Object obj = (Object) it.next();
+            String parentsPage = (String) obj.get("parentpage");
+            if (parentsPage != null && parentsPage.length() > 0)
+                parents.add(parentsPage);
+        }
+        return parents;
+    }
+
+    public List getParents(XWikiContext context) throws XWikiException {
+        List parentsPage = getParentPages(context);
+        List res = new ArrayList();
+        Iterator it = parentsPage.iterator();
+        while(it.hasNext()){
+            String str = (String) it.next();
+            res.add(getGroup(str, context));
+        }
+        return res;
+    }
+
+    /**
+     *
+     * @param name
+     * @param context
+     * @return
+     * @throws XWikiException
+     */
+    public boolean addUser(String name, XWikiContext context) throws XWikiException {
+        if (isMember(name, context))
+            return false;
+        String className = getXWikiGroupsClass(context).getName();
+        int nb = doc.createNewObject(className);
+        Object obj = doc.getObject(className, nb);
+        obj.set("member", name);
+        return true;
+    }
+
+    public boolean isMember(String docName, XWikiContext context) throws XWikiException {
+        List users = getMembers(context);
+        return users.contains(docName);
+    }
+
+    public List getUnactivatedMembers(XWikiContext context) throws XWikiException {
+        List unactivatedMembers = new ArrayList();
+        List members = getMembers(context);
+        Iterator it = members.iterator();
+        while(it.hasNext()){
+            String userPage = (String) it.next();
+            Document doc = new Document(context.getWiki().getDocument(userPage, context), context);
+            doc.use("XWiki.XWikiUsers");
+            Integer active = (Integer) doc.getValue("active");
+            if (active.intValue() == 0){
+                unactivatedMembers.add(userPage);
+            }
+        }
+        return unactivatedMembers;
+    }
+
+    public List getActivatedMembers(XWikiContext context) throws XWikiException {
+        List unactivatedMembers = new ArrayList();
+        List members = getMembers(context);
+        Iterator it = members.iterator();
+        while(it.hasNext()){
+            String userPage = (String) it.next();
+            Document doc = new Document(context.getWiki().getDocument(userPage, context), context);
+            doc.use("XWiki.XWikiUsers");
+            String active = (String) doc.getValue("active");
+            if (active.equals("0")){
+                unactivatedMembers.add(userPage);
+            }
+        }
+        return unactivatedMembers;
+    }
+
+    public List getMembers(XWikiContext context) throws XWikiException {
+        List objs = doc.getObjects(Group.getXWikiGroupsClass(context).getName());
+        Iterator it = objs.iterator();
+        List members = new ArrayList();
+        while(it.hasNext())
+        {
+            members.add(((Object) it.next()).get("member"));
+        }
+        return members;
+    }
+
+    public boolean removeUser(String docName, XWikiContext context) throws XWikiException {
+        if (!isMember(docName, context))
+            return false;
+        String className = getXWikiGroupsClass(context).getName();
+        Vector  objs = doc.getObjects(className);
+
+        Object obj;
+        Iterator it = objs.iterator();
+
+        while(it.hasNext())
+        {
+            obj = (Object) it.next();
+            if (obj.get("member").equals(docName)) {
+                doc.removeObject(obj);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public void save(XWikiContext context) throws XWikiException {
+        doc.saveWithProgrammingRights();
+    }
+
+    protected static BaseClass getXWikiGroupsClass(XWikiContext context) throws XWikiException {
+        XWikiDocument doc;
+        XWiki xwiki = context.getWiki();
+        boolean needsUpdate = false;
+
+        try {
+            doc = xwiki.getDocument("XWiki.XWikiGroups", context);
+        } catch (Exception e) {
+            doc = new XWikiDocument();
+            doc.setWeb("XWiki");
+            doc.setName("XWikiGroups");
+            needsUpdate = true;
+        }
+
+        BaseClass bclass = doc.getxWikiClass();
+        bclass.setName("XWiki.XWikiGroups");
+        needsUpdate |= bclass.addTextField("member", "Member", 30);
+        needsUpdate |= bclass.addTextField("role", "Role", 30);
+        needsUpdate |= bclass.addTextAreaField("description", "Description", 40, 5);
+
+        String content = doc.getContent();
+        if ((content==null)||(content.equals(""))) {
+            needsUpdate = true;
+            doc.setContent("1 XWikiGroups");
+        }
+
+        if (needsUpdate)
+            xwiki.saveDocument(doc, context);
+        return bclass;
+    }
+
+    protected static BaseClass getXWikiDirectoryGroupClass(XWikiContext context) throws XWikiException {
+        XWikiDocument doc;
+        XWiki xwiki = context.getWiki();
+        boolean needsUpdate = false;
+
+        try {
+            doc = xwiki.getDocument("XWiki.DirectoryGroupClass", context);
+        } catch (Exception e) {
+            doc = new XWikiDocument();
+            doc.setWeb("XWiki");
+            doc.setName("DirectoryGroupClass");
+            needsUpdate = true;
+        }
+
+        BaseClass bclass = doc.getxWikiClass();
+        bclass.setName("XWiki.DirectoryGroupClass");
+        needsUpdate |= bclass.addTextField("name", "Name", 30);
+        needsUpdate |= bclass.addTextAreaField("description", "Description", 40, 5);
+
+        String content = doc.getContent();
+        if ((content==null)||(content.equals(""))) {
+            needsUpdate = true;
+            doc.setContent("1 DirectoryGroupClass");
+        }
+
+        if (needsUpdate)
+            xwiki.saveDocument(doc, context);
+        return bclass;
+    }
+
+    protected static BaseClass getXWikiGroupRelationClass(XWikiContext context) throws XWikiException {
+        XWikiDocument doc;
+        XWiki xwiki = context.getWiki();
+        boolean needsUpdate = false;
+
+        try {
+            doc = xwiki.getDocument("XWiki.GroupRelationClass", context);
+        } catch (Exception e) {
+            doc = new XWikiDocument();
+            doc.setWeb("XWiki");
+            doc.setName("GroupRelationClass");
+            needsUpdate = true;
+        }
+
+        BaseClass bclass = doc.getxWikiClass();
+        bclass.setName("XWiki.GroupRelationClass");
+        needsUpdate |= bclass.addTextField("name", "Name", 30);
+        needsUpdate |= bclass.addTextField("parentpage", "Parent", 30);
+        needsUpdate |= bclass.addTextAreaField("description", "Description", 40, 5);
+
+        String content = doc.getContent();
+        if ((content==null)||(content.equals(""))) {
+            needsUpdate = true;
+            doc.setContent("1 XWikiGroup");
+        }
+
+        if (needsUpdate)
+            xwiki.saveDocument(doc, context);
+        return bclass;
+    }
+
+    public static List getAllGroupsPageName(XWikiContext context) throws XWikiException {
+        String className = getXWikiDirectoryGroupClass(context).getName();
+        String hql = ", BaseObject as obj where obj.name=doc.fullName"
+                        + " and obj.className='" + className + "'";
+        return context.getWiki().getStore().searchDocumentsNames(hql, context);
+    }
+
+
+    public static boolean isValidGroup(String grpName, XWikiContext context) throws XWikiException {
+        Document doc = new Document(context.getWiki().getDocument(grpName, context), context);
+        if (doc.isNew())
+            return false;
+        return (doc.getObjects(getXWikiDirectoryGroupClass(context).getName()).size() > 0);
+    }
+
+    public static Group getGroup(String space, String name, XWikiContext context) throws XWikiException {
+        XWikiDocument doc = context.getWiki().getDocument(space, name, context);
+        return (new Group(doc, context));
+    }
+
+    public Group getGroup(String name, XWikiContext context) throws XWikiException {
+        if (name.contains("."))
+        {
+            String[] grp = name.split("\\.");
+            return getGroup(grp[0], grp[1], context);
+        }
+        else
+            return getGroup(UserDirectoryPlugin.DEFAULT_PLUGIN_SPACE, name, context);
+    }
+
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPlugin.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPlugin.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPlugin.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,294 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
+package com.xpn.xwiki.plugin.userdirectory;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Document;
+import com.xpn.xwiki.api.Api;
+import com.xpn.xwiki.web.XWikiRequest;
+import com.xpn.xwiki.plugin.usertools.XWikiUserManagementTools;
+import com.xpn.xwiki.user.api.XWikiGroupService;
+import com.xpn.xwiki.plugin.PluginException;
+import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
+import com.xpn.xwiki.plugin.XWikiPluginInterface;
+import com.xpn.xwiki.doc.XWikiDocument;
+
+import java.util.*;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.VelocityContext;
+
+
+public class UserDirectoryPlugin  extends XWikiDefaultPlugin implements XWikiPluginInterface {
+    private static Log mLogger =
+            LogFactory.getFactory().getInstance(UserDirectoryPlugin.class);
+
+    public static final String DEFAULT_PLUGIN_SPACE = "Directory";
+    public static final String DEFAULT_GROUP_TEMPLATE= "XWiki.DirectoryGroupTemplate";
+    public static final String DEFAULT_DEACTIVATION_MESSAGE_PAGE = "XWiki.DeactivationMessage";
+    public static final int ERROR_USERDIRECTORYPLUGIN_UNKNOWN = 0;
+    public static final int ERROR_USERDIRECTORYPLUGIN_ALREADYEXIST = 1;
+    public static final int ERROR_USERDIRECTORYPLUGIN_GRPDOESNTEXIST = 2;
+    private static final int ERROR_XWIKI_EMAIL_CANNOT_PREPARE_VALIDATION_EMAIL = 3;
+    private static final int ERROR_XWIKI_EMAIL_CANNOT_GET_VALIDATION_CONFIG = 4;
+
+    public UserDirectoryPlugin(String name, String className, XWikiContext context) {
+        super(name, className, context);
+    }
+
+    public String getName() {
+        return "userdirectory";
+    }
+
+    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
+        return new UserDirectoryPluginAPI((UserDirectoryPlugin) plugin, context);
+    }
+
+
+    /**
+     * Add a group from the request
+     * @param context
+     * @return
+     */
+    public Group addGroup(XWikiContext context) throws XWikiException {
+        XWikiRequest req = context.getRequest();
+        String name = req.get("XWiki.DirectoryGroupClass_0_name");
+        name = context.getWiki().clearName(name, context);
+        XWikiDocument tmpDoc = context.getWiki().getDocument(DEFAULT_PLUGIN_SPACE, name, context);
+        if (!tmpDoc.isNew())
+            throw new PluginException(UserDirectoryPlugin.class, ERROR_USERDIRECTORYPLUGIN_ALREADYEXIST, "This document already exist, try another name");
+        Document doc = new Document(tmpDoc, context);
+        doc.addObjectFromRequest("XWiki.DirectoryGroupClass");
+        doc.setContent(getTemplate(context));
+        doc.saveWithProgrammingRights();
+        return new Group(doc, context);
+    }
+
+    public String getTemplate(XWikiContext context) throws XWikiException {
+        return context.getWiki().getDocument(DEFAULT_GROUP_TEMPLATE, context).getContent();
+    }
+
+    public void updateGroup(XWikiContext context) throws XWikiException {
+        XWikiRequest req = context.getRequest();
+        String pageName = req.get("pageName");
+        XWikiDocument tmpDoc = context.getWiki().getDocument(pageName, context);
+        Document doc = new Document(tmpDoc, context);
+        doc.updateObjectFromRequest("XWiki.DirectoryGroupClass");
+        doc.save();
+    }
+
+    public boolean groupExist(String name, XWikiContext context) throws XWikiException {
+        return getGroup(name, context).isNew();
+    }
+
+     public Group getGroup(String space, String name, XWikiContext context) throws XWikiException {
+        return Group.getGroup(space, name, context);
+    }
+
+    public Group getGroup(String name, XWikiContext context) throws XWikiException {
+        return getGroup(DEFAULT_PLUGIN_SPACE, name, context);
+    }
+
+    public List getAllGroupsPageName(XWikiContext context) throws XWikiException {
+        return Group.getAllGroupsPageName(context);
+    }
+
+    public List getAllGroups(XWikiContext context) throws XWikiException {
+        List allGroupsPageName = getAllGroupsPageName(context);
+        List groups = new ArrayList();
+        if (allGroupsPageName == null)
+            return groups;
+        Iterator it = allGroupsPageName.iterator();
+        while (it.hasNext())
+            groups.add(getGroup((String) it.next(), context));
+        return groups;
+
+    }
+
+    public List getMembers(String grpPage, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpPage, context);
+        return grp.getMembers(context);
+    }
+
+    public List getUnactivatedMembers(String grpPage, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpPage, context);
+        return grp.getUnactivatedMembers(context);
+    }
+
+    public boolean removeGroup(String name, XWikiContext context){
+        return false;
+    }
+
+    public boolean addParentGroup(String childGroupName, String parentGroupName, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(childGroupName, context);
+        boolean res = grp.addParentName(parentGroupName, this, context);
+        if (res){
+            grp.save(context);
+            return true;
+        }
+        return false;
+    }
+
+    public boolean removeParentGroup(String childGroupName, String parentGroupName, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(childGroupName, context);
+        boolean res =  grp.removeParent(parentGroupName, this, context);
+        if (res){
+            grp.save(context);
+            return true;
+        }
+        return false;
+    }
+
+    public List getParentGroups(String grpName, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpName, context);
+        return grp.getParents(context);
+    }
+
+    public String inviteToGroup(String name, String firstName, String email, String group, XWikiContext context) throws XWikiException {
+        String pageName = context.getWiki().convertUsername(email, context);
+        XWikiUserManagementTools userTools = (XWikiUserManagementTools) context.getWiki().getPluginApi("usermanagementtools", context);
+        XWikiDocument doc = context.getWiki().getDocument(userTools.getUserSpace(context) + "." + pageName, context);
+        if (doc.isNew()) {
+            String userDocName = userTools.inviteUser(name, email, context);
+            Document userDoc = new Document(context.getWiki().getDocument(userDocName,context), context);
+            userDoc.use("XWiki.XWikiUsers");
+            userDoc.set("first_name", firstName);
+            userDoc.saveWithProgrammingRights();
+        }
+        addUserToGroup(doc.getFullName(), group, context);
+        return doc.getFullName();
+    }
+
+    public void addUserToGroup(String userPage, String group, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(group, context);
+        if (grp == null)
+            throw new PluginException(UserDirectoryPlugin.class, ERROR_USERDIRECTORYPLUGIN_GRPDOESNTEXIST, "This group doesn't exist");
+        if (grp.addUser(userPage, context))
+            grp.save(context);
+    }
+
+    public String getUserName(String userPage, XWikiContext context) throws XWikiException {
+        XWikiUserManagementTools userTools = (XWikiUserManagementTools) context.getWiki().getPluginApi("usermanagementtools", context);
+        return userTools.getUserName(userPage, context);
+    }
+
+    public String getUserEmail(String userPage, XWikiContext context) throws XWikiException {
+        XWikiUserManagementTools userTools = (XWikiUserManagementTools) context.getWiki().getPluginApi("usermanagementtools", context);
+        return userTools.getEmail(userPage, context);
+    }
+
+    public List getUsersDocumentName(String grpPage, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpPage, context);
+        return grp.getUsersPageName(context);
+    }
+
+    public List getUsersDocument(String grpPage, XWikiContext context) throws XWikiException {
+        List users = getUsersDocumentName(grpPage, context);
+        List usersDoc = new ArrayList();
+        Iterator it = users.iterator();
+        while(it.hasNext()){
+            usersDoc.add(new Document(context.getWiki().getDocument((String) it.next(), context), context));
+        }
+        return usersDoc;
+    }
+
+    public boolean removeMemberships(String userPage, String grpPage, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpPage, context);
+        grp.removeUser(userPage, context);
+        grp.save(context);
+        return true;
+    }
+
+    public void sendDeactivationEMail(String userPage, String grpPage, XWikiContext context) throws XWikiException {
+        Group grp = getGroup(grpPage, context);
+        String userName = getUserName(userPage, context);
+        String email = getUserEmail(userPage, context);
+        String message = prepareDeactivationMessage(userName, email, grp.getName(), context);
+        String sender;
+        try {
+            sender = context.getWiki().getXWikiPreference("admin_email", context);
+        } catch (Exception e) {
+            throw new PluginException(getName(), ERROR_XWIKI_EMAIL_CANNOT_GET_VALIDATION_CONFIG,
+                    "Exception while reading the validation email config", e, null);
+
+        }
+        context.getWiki().sendMessage(sender, email, message, context);
+    }
+
+    private String prepareDeactivationMessage(String name,String email, String grp, XWikiContext context) throws XWikiException {
+        XWikiDocument doc = context.getWiki().getDocument(getDeactivationMessageDocument(context), context);
+
+        String content = doc.getContent();
+
+        try {
+            VelocityContext vcontext = (VelocityContext) context.get("vcontext");
+            vcontext.put("name", name);
+            vcontext.put("group", grp);
+            vcontext.put("email", email);
+            content = context.getWiki().parseContent(content, context);
+        } catch (Exception e) {
+            throw new PluginException(getName(), ERROR_XWIKI_EMAIL_CANNOT_PREPARE_VALIDATION_EMAIL,
+                    "Exception while preparing the validation email", e, null);
+
+        }
+        return content;
+    }
+
+    protected String getDeactivationMessageDocument(XWikiContext context)
+    {
+        return DEFAULT_DEACTIVATION_MESSAGE_PAGE;
+    }
+
+    public List getUserMemberships(String userPage, XWikiContext context) throws XWikiException {
+        XWikiGroupService groupService = context.getWiki().getGroupService(context);
+        Collection groups = groupService.listGroupsForUser(userPage, context);
+        List userGrps = new ArrayList();
+        Iterator it = groups.iterator();
+        while(it.hasNext()){
+            String grpName = (String) it.next();
+            if (Group.isValidGroup(grpName, context))
+                userGrps.add(grpName);
+        }
+        return userGrps;
+    }
+
+    public boolean deactivateAccount(String userPage, XWikiContext context) throws XWikiException {
+        Document doc = new Document(context.getWiki().getDocument(userPage, context), context);
+        doc.use("XWiki.XWikiUsers");
+        doc.set("active", "0");
+        String validkey = context.getWiki().generateValidationKey(16);
+        doc.set("validkey", validkey);
+        doc.saveWithProgrammingRights();
+        return true;
+    }
+
+    public void resendInvitation(String userPage, XWikiContext context) throws XWikiException {
+        XWikiUserManagementTools userTools = (XWikiUserManagementTools) context.getWiki().getPluginApi("usermanagementtools", context);
+
+        userTools.resendInvitation(userTools.getEmail(userPage, context), context);
+    }
+
+
+
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPluginAPI.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPluginAPI.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/userdirectory/UserDirectoryPluginAPI.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
+package com.xpn.xwiki.plugin.userdirectory;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Api;
+
+import java.util.List;
+
+public class UserDirectoryPluginAPI extends Api {
+    UserDirectoryPlugin userDir;
+
+    public UserDirectoryPluginAPI(UserDirectoryPlugin userDirectory, XWikiContext context) {
+        super(context);
+        this.userDir = userDirectory;
+    }
+
+    public Group addGroup(XWikiContext context) throws XWikiException {
+        return userDir.addGroup(context);
+    }
+
+    public void updateGroup(XWikiContext context) throws XWikiException {
+        userDir.updateGroup(context);
+    }
+
+    public boolean groupExist(String name, XWikiContext context) throws XWikiException {
+        return userDir.groupExist(name, context);
+    }
+
+    public Group getGroup(String space, String name, XWikiContext context) throws XWikiException {
+        return userDir.getGroup(space, name, context);
+    }
+
+    public Group getGroup(String name, XWikiContext context) throws XWikiException {
+        return userDir.getGroup(name, context);
+    }
+
+    public List getAllGroupsPageName(XWikiContext context) throws XWikiException {
+         return userDir.getAllGroupsPageName(context);
+    }
+
+    public List getAllGroups(XWikiContext context) throws XWikiException {
+        return userDir.getAllGroups(context);
+    }
+
+    public List getMembers(String grpPage, XWikiContext context) throws XWikiException {
+        return userDir.getMembers(grpPage, context);
+    }
+
+    public List getUnactivatedMembers(String grpPage, XWikiContext context) throws XWikiException {
+        return userDir.getUnactivatedMembers(grpPage, context);
+    }
+
+    public boolean addParentGroup(String childGroupName, String parentGroupName, XWikiContext context) throws XWikiException {
+        return userDir.addParentGroup(childGroupName, parentGroupName, context);
+    }
+
+    public boolean removeParentGroup(String childGroupName, String parentGroupName, XWikiContext context) throws XWikiException {
+        return userDir.removeParentGroup(childGroupName, parentGroupName, context);
+    }
+
+    public List getParentGroups(String grpName, XWikiContext context) throws XWikiException {
+        return userDir.getParentGroups(grpName, context);
+    }
+
+    public String inviteToGroup(String name, String firstName, String email, String group, XWikiContext context) throws XWikiException {
+        return userDir.inviteToGroup(name, firstName, email, group,  context);
+    }
+
+    public void addUserToGroup(String userPage, String group, XWikiContext context) throws XWikiException {
+        userDir.addUserToGroup(userPage, group,  context);
+    }
+
+    public String getUserName(String userPage, XWikiContext context) throws XWikiException {
+        return userDir.getUserName(userPage, context);
+    }
+
+    public String getUserEmail(String userPage, XWikiContext context) throws XWikiException {
+        return userDir.getUserEmail(userPage, context);
+    }
+
+    public List getUsersDocumentName(String grpPage, XWikiContext context) throws XWikiException {
+        return userDir.getUsersDocumentName(grpPage, context);
+    }
+
+    public boolean removeMemberships(String userPage, String grpPage, XWikiContext context) throws XWikiException {
+        return userDir.removeMemberships(userPage, grpPage, context);
+    }
+
+    public void sendDeactivationEMail(String userPage, String grpPage, XWikiContext context) throws XWikiException {
+        userDir.sendDeactivationEMail(userPage, grpPage, context);
+    }
+
+    public List getUserMemberships(String userPage, XWikiContext context) throws XWikiException {
+        return userDir.getUserMemberships(userPage, context);
+    }
+
+    public boolean deactivateAccount(String userPage, XWikiContext context) throws XWikiException {
+        return userDir.deactivateAccount(userPage, context);
+    }
+
+    public void resendInvitation(String userPage, XWikiContext context) throws XWikiException {
+        userDir.resendInvitation(userPage, context);
+    }
+
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementTools.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementTools.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementTools.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,23 @@
+package com.xpn.xwiki.plugin.usertools;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: jeremi
+ * Date: Aug 18, 2006
+ * Time: 4:11:32 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public interface XWikiUserManagementTools {
+
+    public String inviteUser(String name, String email, XWikiContext context) throws XWikiException;
+    public boolean resendInvitation(String email, XWikiContext context) throws XWikiException;
+    public String getUserSpace(XWikiContext context);
+    public String getUserPage(String email, XWikiContext context);
+    public boolean isValidEmail(String email);
+    public String getUserName(String userPage, XWikiContext context) throws XWikiException;
+
+    public String getEmail(String userPage, XWikiContext context) throws XWikiException;
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsAPI.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsAPI.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsAPI.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
+package com.xpn.xwiki.plugin.usertools;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.api.Api;
+
+
+public class XWikiUserManagementToolsAPI extends Api implements XWikiUserManagementTools {
+    private XWikiUserManagementTools userMngtTools;
+
+    public XWikiUserManagementToolsAPI(XWikiContext context) {
+        super(context);
+    }
+
+    public XWikiUserManagementToolsAPI(XWikiUserManagementTools xWikiUserManagementTools, XWikiContext context) {
+        this(context);
+        this.userMngtTools = xWikiUserManagementTools;
+    }
+
+    public String inviteUser(String name, String email, XWikiContext context) throws XWikiException {
+        return userMngtTools.inviteUser(name, email, context);
+    }
+
+    public boolean resendInvitation(String email, XWikiContext context) throws XWikiException {
+        return userMngtTools.resendInvitation(email, context);
+    }
+
+    public String getUserSpace(XWikiContext context) {
+        return userMngtTools.getUserSpace(context);
+    }
+
+    public String getUserPage(String email, XWikiContext context) {
+        return userMngtTools.getUserPage(email, context);
+    }
+
+    public boolean isValidEmail(String email) {
+        return userMngtTools.isValidEmail(email);
+    }
+
+    public String getUserName(String userPage, XWikiContext context) throws XWikiException {
+        return userMngtTools.getUserName(userPage, context);
+    }
+
+    public String getEmail(String userPage, XWikiContext context) throws XWikiException {
+        return userMngtTools.getEmail(userPage, context);
+    }
+}

Added: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsImpl.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsImpl.java	2006-09-05 11:46:27 UTC (rev 1283)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/usertools/XWikiUserManagementToolsImpl.java	2006-09-05 14:32:13 UTC (rev 1284)
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
+package com.xpn.xwiki.plugin.usertools;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
+import com.xpn.xwiki.plugin.XWikiPluginInterface;
+import com.xpn.xwiki.plugin.PluginException;
+import com.xpn.xwiki.objects.BaseObject;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.api.Document;
+import com.xpn.xwiki.api.Api;
+import org.apache.velocity.VelocityContext;
+
+
+public class XWikiUserManagementToolsImpl extends XWikiDefaultPlugin implements XWikiPluginInterface, XWikiUserManagementTools {
+    public static final String DEFAULT_USER_SPACE = "XWiki";
+    public static final String DEFAULT_INVITATION_MESSAGE_PAGE = "XWiki.InvitationMessage";
+    public static final String DEFAULT_REINVITATION_MESSAGE_PAGE = "XWiki.ReinvitationMessage";
+    public static final String DEFAULT_USER_CLASS = "XWiki.XWikiUsers";
+    public static final String DEFAULT_USERTEMPLATE_CLASS = "XWiki.XWikiUsersTemplate";
+
+    public static final int ERROR_XWIKI_EMAIL_INVALID_EMAIL = 1;
+    public static final int ERROR_XWIKI_EMAIL_INVALID_ADMIN_EMAIL = 2;
+    public static final int ERROR_XWIKI_USER_PAGE_ALREADY_EXIST = 3;
+    public static final int ERROR_XWIKI_EMAIL_CANNOT_PREPARE_VALIDATION_EMAIL = 4;
+
+
+    public XWikiUserManagementToolsImpl(String name, String className, XWikiContext context) {
+        super(name, className, context);
+    }
+
+    public String getName() {
+        return "usermanagementtools";
+    }
+
+    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
+        return new XWikiUserManagementToolsAPI((XWikiUserManagementTools) plugin, context);
+    }
+
+    public String inviteUser(String name, String email, XWikiContext context) throws XWikiException {
+        if (!isValidEmail(email))
+           throw new PluginException(getName(), ERROR_XWIKI_EMAIL_INVALID_EMAIL, "The email is not valid");
+
+        Document userdoc = createUserDocument(email, context);
+        userdoc.use(DEFAULT_USER_CLASS);
+        userdoc.set("last_name", name);
+        userdoc.set("email", email);
+
+
+        userdoc.saveWithProgrammingRights();
+        context.getWiki().setUserDefaultGroup(userdoc.getFullName(), context);
+        BaseObject userObj = userdoc.getObject(DEFAULT_USER_CLASS).getXWikiObject();
+        XWikiDocument doc = context.getWiki().getDocument(getInvitationMessageDocument(context), context);
+        String message = prepareInvitationMessage(doc, name, userObj.getStringValue("password"), email, userObj.getStringValue("validkey"), userdoc.getURL(), context);
+
+        String sender;
+        try {
+            sender = context.getWiki().getXWikiPreference("admin_email", context);
+        } catch (Exception e) {
+            throw new PluginException(getName(), ERROR_XWIKI_EMAIL_INVALID_ADMIN_EMAIL, "Exception while reading the validation email config", e, null);
+
+        }
+
+        context.getWiki().sendMessage(sender, email, message, context);
+        return userdoc.getFullName();
+    }
+
+    public boolean isValidEmail(String email) {
+       if ((email==null) || (email=="") || (email.indexOf('@')==-1)) {
+            return false;
+       }
+       return true;
+     }
+
+    protected Document createUserDocument(String email, XWikiContext context) throws XWikiException {
+        String pageName = getUserPage(email, context);
+        XWikiDocument userDoc = context.getWiki().getDocument(pageName, context);
+        if (!userDoc.isNew()) {
+            throw new PluginException(getName(), ERROR_XWIKI_USER_PAGE_ALREADY_EXIST, "This document already exist, try another name");
+        }
+        Document userApiDoc = new Document(userDoc, context);
+
+        String template = DEFAULT_USERTEMPLATE_CLASS;
+        if ((template != null) && (!template.equals(""))) {
+            XWikiDocument tdoc = context.getWiki().getDocument(template, context);
+            if ((!tdoc.isNew()))
+                userApiDoc.setContent(tdoc.getContent());
+        }
+
+        String password = getRandomPassword();
+        String validkey = context.getWiki().generateValidationKey(16);
+
+        userApiDoc.addObjectFromRequest(DEFAULT_USER_CLASS);
+        userApiDoc.set("active", "0");
+        userApiDoc.set("password", password);
+        userApiDoc.set("validkey", validkey);
+        userApiDoc.setContent("#includeForm(\"XWiki.XWikiUserSheet\")");
+        com.xpn.xwiki.api.Object rightobj = userApiDoc.newObject("XWiki.XWikiRights");
+        rightobj.set("users", pageName) ;
+        rightobj.set("allow", "1");
+        rightobj.set("levels", "edit");
+        rightobj.set("groups", "");
+        return userApiDoc;
+    }
+
+    public String getUserPage(String email, XWikiContext context){
+        String pageName = context.getWiki().convertUsername(email, context);
+
+        return getUserSpace(context) + "." + pageName;
+    }
+
+    public String getUserName(String userPage, XWikiContext context) throws XWikiException {
+        Document doc = new Document(context.getWiki().getDocument(userPage, context), context);
+        doc.use(DEFAULT_USER_CLASS);
+        return doc.get("first_name") + " " + doc.get("last_name");
+    }
+
+    public String getEmail(String userPage, XWikiContext context) throws XWikiException {
+        Document doc = new Document(context.getWiki().getDocument(userPage, context), context);
+        doc.use(DEFAULT_USER_CLASS);
+        return (String) doc.get("email");
+    }
+
+    private String getRandomPassword() {
+        return org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(8);
+    }
+
+    private String prepareInvitationMessage(XWikiDocument doc, String name, String password, String email, String validationUrl, String validKey, XWikiContext context) throws XWikiException {
+
+
+        String content = doc.getContent();
+
+        try {
+            VelocityContext vcontext = (VelocityContext) context.get("vcontext");
+            vcontext.put("name", name);
+            vcontext.put("password", password);
+            vcontext.put("validationUrl", validationUrl);
+            vcontext.put("validKey", validKey);
+            vcontext.put("email", email);
+            content = context.getWiki().parseContent(content, context);
+        } catch (Exception e) {
+            throw new PluginException(getName(), ERROR_XWIKI_EMAIL_CANNOT_PREPARE_VALIDATION_EMAIL, "Exception while preparing the validation email", e, null);
+        }
+        return content;
+    }
+
+
+
+    public String getUserSpace(XWikiContext context)
+    {
+        return DEFAULT_USER_SPACE;
+    }
+
+    protected String getInvitationMessageDocument(XWikiContext context)
+    {
+        return DEFAULT_INVITATION_MESSAGE_PAGE;
+    }
+
+    protected String getReinvitationMessageDocument(XWikiContext context)
+    {
+        return DEFAULT_REINVITATION_MESSAGE_PAGE;
+    }
+
+    public boolean resendInvitation(String email, XWikiContext context) throws XWikiException {
+        XWikiDocument userdoc = context.getWiki().getDocument(getUserPage(email, context), context);
+        BaseObject userObj = userdoc.getObject(DEFAULT_USER_CLASS);
+        XWikiDocument doc = context.getWiki().getDocument(getReinvitationMessageDocument(context), context);
+        String message = prepareInvitationMessage(doc, getUserName(userdoc.getFullName(), context), userObj.getStringValue("password"), email, "", userObj.getStringValue("validkey"), context);
+
+        String sender;
+        try {
+            sender = context.getWiki().getXWikiPreference("admin_email", context);
+        } catch (Exception e) {
+            throw new PluginException(getName(), ERROR_XWIKI_EMAIL_INVALID_ADMIN_EMAIL, "Exception while reading the validation email config", e, null);
+
+        }
+
+        context.getWiki().sendMessage(sender, email, message, context);
+        return true;
+    }
+
+    
+
+
+}





More information about the Xwiki-notifications mailing list