//
// $Id: User.java,v 1.10 2004/01/13 23:02:04 ray Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
// 
// This library 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 library 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 library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.servlet.user;

import java.sql.Date;

import com.samskivert.jdbc.jora.FieldMask;
import com.samskivert.util.StringUtil;

/**
 * A user object contains information about a registered user in our web
 * application environment. Users are stored in the user repository (a
 * database table) and loaded during a request based on authentication
 * information provided with the request headers.
 *
 * <p><b>Note:</b> Do not modify any of the fields of this object
 * directly. Use the <code>set</code> methods to make updates. If no set
 * methods exist, you shouldn't be modifying that field.
 */
public class User
{
    /** The user's assigned integer userid. */
    public int userId;

    /** The user's chosen username. */
    public String username;

    /** The date this record was created. */
    public Date created;

    /** The user's real name (first, last and whatever else they opt to
     * provide). */
    public String realname;

    /** The user's chosen password (encrypted). */
    public String password;

    /** The user's email address. */
    public String email;

    /** The site identifier of the site through which the user created
     * their account. (Their affiliation, if you will.) */
    public int siteId;

    /**
     * Updates the user's real name.
     */
    public void setRealName (String realname)
    {
	this.realname = realname;
        _dirty.setModified("realname");
    }

    /**
     * Updates the user's password.
     *
     * @param password The user's new (unencrypted) password.
     */
    public void setPassword (String password)
    {
        setPassword(Password.makeFromClear(password));
    }

    /**
     * Updates the user's password.
     *
     * @param password The user's new password.
     */
    public void setPassword (Password password)
    {
	this.password = password.getEncrypted();
        _dirty.setModified("password");
    }

    /**
     * Updates the user's email address.
     */
    public void setEmail (String email)
    {
	this.email = email;
        _dirty.setModified("email");
    }

    /**
     * Updates the user's site id.
     */
    public void setSiteId (int siteId)
    {
        this.siteId = siteId;
        _dirty.setModified("siteId");
    }

    /**
     * Converts a legacy password.
     *
     * @return true if a conversion took place, false if not.
     */
    public boolean updateLegacyPassword (String password)
    {
        // we may have an old crypt() encrypted password
        if (this.password.length() == 13) {
            // check both the case sensitive and insensitive versions for
            // further legacy reasons
            if (this.password.equals(
                    UserUtil.legacyEncrypt(username, password, false)) ||
                this.password.equals(
                    UserUtil.legacyEncrypt(username, password, true))) {
                // update our password with the new encryption method
                setPassword(password);
                return true;
            }
        }

        return false;
    }

    /**
     * Compares the supplied password with the password associated with
     * this user record.
     *
     * @return true if the passwords match, false if they do not.
     */
    public boolean passwordsMatch (Password password)
    {
        return this.password.equals(password.getEncrypted());
    }

    /**
     * @return true if this User has been "deleted".
     */
    public boolean isDeleted ()
    {
        // a deleted account has an "=" in the username
        return (-1 != username.indexOf('='));
    }

    /**
     * Called by the repository to find out which fields have been
     * modified since the object was loaded.
     */
    protected FieldMask getDirtyMask ()
    {
        return _dirty;
    }

    /**
     * Called by the repository to configure this user record with a field
     * mask that it can use to track modified fields.
     */
    protected void setDirtyMask (FieldMask dirty)
    {
        _dirty = dirty;
    }

    /** Returns a string representation of this instance. */
    public String toString ()
    {
        return StringUtil.fieldsToString(this);
    }

    /** Our dirty field mask. */
    protected transient FieldMask _dirty;
}

@@ -4489,32 +4489,264 @@
 f('='));%0A    %7D%0A%0A
+    /**%0A     * Returns true if this user is an admin, false otherwise. The default%0A     * implementation does not track admin status and always returns false.%0A     */%0A    public boolean isAdmin ()%0A    %7B%0A        return false;%0A    %7D%0A%0A
     /**%0A     * C

Provide an overridable notion of admin-ness.