/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * 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.
 *
 */
package com.xpn.xwiki.api;

import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.user.api.XWikiUser;

/**
 * Scriptable API for easy handling of users. For the moment this API is very limited, containing
 * only one method. In the future it should be extended to provide useful methods for working with
 * users.
 * 
 * @version $Id: $
 * @since Platform-1.0
 */
public class User extends Api
{
    /** Logging helper object. */
    protected static final Log LOG = LogFactory.getLog(User.class);

    /** The wrapped XWikiUser object. */
    private XWikiUser user;

    /**
     * Constructs a wrapper for the given protected XWikiUser object.
     * 
     * @param user The XWikiUser object that should be wrapper.
     * @param context The current {@link XWikiContext context}.
     */
    public User(XWikiUser user, XWikiContext context)
    {
        super(context);
        this.user = user;
    }

    /**
     * Expose the wrapped XWikiUser object. Requires programming rights.
     * 
     * @return The wrapped XWikiUser object, or <tt>null</tt> if the user does not have
     *         programming rights.
     */
    public XWikiUser getUser()
    {
        if (hasProgrammingRights()) {
            return user;
        }
        return null;
    }

    /**
     * Check if the user belongs to a group or not.
     * 
     * @param groupName The group to check.
     * @return <tt>true</tt> if the user does belong to the specified group, false otherwise or if
     *         an exception occurs.
     */
    public boolean isUserInGroup(String groupName)
    {
        try {
            return user.isUserInGroup(groupName, getXWikiContext());
        } catch (Exception ex) {
            LOG.warn(new MessageFormat("Unhandled exception checking if user {0}"
                + " belongs to group {1}").format(new java.lang.Object[] {user, groupName}), ex);
        }
        return false;
    }

    /**
     * <p>
     * See if the user is global (i.e. registered in the main wiki) or local to a virtual wiki.
     * </p>
     * <p>
     * This method is not public, as the underlying implementation is not fully functional
     * </p>
     * 
     * @return <tt>true</tt> if the user is global, false otherwise or if an exception occurs.
     */
    protected boolean isMain()
    {
        return user.isMain();
    }
}

@@ -1100,16 +1100,99 @@
 ntext;%0D%0A
+import com.xpn.xwiki.doc.XWikiDocument;%0D%0Aimport com.xpn.xwiki.objects.BaseObject;%0D%0A
 import c
@@ -3535,12 +3535,786 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    /**%0D%0A     * API to retrieve the e-mail address of this user. This e-mail address is taken from the user%0D%0A     * profile. If the user hasn't changed his profile, then this is the e-mail address he filled in%0D%0A     * the registration form.%0D%0A     * %0D%0A     * @return The e-mail address from the user profile, or %3Ctt%3Enull%3C/tt%3E if there is an error%0D%0A     *         retrieving the email.%0D%0A     */%0D%0A    public String getEmail()%0D%0A    %7B%0D%0A        XWikiDocument userDoc;%0D%0A        try %7B%0D%0A            userDoc = getXWikiContext().getWiki().getDocument(user.getUser(), getXWikiContext());%0D%0A            BaseObject obj = userDoc.getObject(%22XWiki.XWikiUsers%22);%0D%0A            return obj.getStringValue(%22email%22);%0D%0A        %7D catch (Exception e) %7B%0D%0A            return null;%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

Add a method to retrieve the e-mail address of a user Done. Patch submitted by Marius Florea, applied with minor javadoc changes.