Subversion Repositories general

Rev

Rev 946 | Rev 950 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.hostadmiral.core.model;

import java.util.Collection;
import java.util.Collections;

import ak.hostadmiral.util.Digest;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;

/**
 *
 * @hibernate.class table="users"
 */
public class User
        extends GeneralModelObject
{
        private String     login;
        private String     password;
        private User       boss;
        private Boolean    superuser;
        private Collection loginHistory;

        protected User()
        {
        }

        /**
         *
         * @hibernate.property
         */
        public String getLogin()
        {
                return login;
        }

        protected void setLogin(String login)
        {
                this.login = login;
        }

        public void setLogin(User editor, String login)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

                this.login = login;
        }

        /**
         *
         * @hibernate.property
         */
        protected String getPassword()
        {
                return password;
        }

        protected void setPassword(String password)
        {
                this.password = password;
        }

        public void setPassword(User editor, String password)
                throws ModelException
        {
                if(!mayChangePassword(editor))
                        throw new ModelSecurityException();

                if(password == null)
                        throw new NullPointerException("Null password");

                this.password = Digest.encode(password);
        }

        public boolean checkPassword(String password)
        {
                if(password == null)
                        throw new NullPointerException("Null password");

                return checkMd5Password(Digest.encode(password));
        }

        public boolean checkMd5Password(String password)
        {
                return this.password.equals(password);
        }

        /**
         *
         * @hibernate.many-to-one
         */
        public User getBoss()
        {
                return boss;
        }

        protected void setBoss(User boss)
        {
                this.boss = boss;
        }

        public void setBoss(User editor, User boss)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

                this.boss = boss;
        }

        /**
         *
         * @hibernate.property
         */
        public Boolean getSuperuser()
        {
                return superuser;
        }

        public boolean isSuperuser()
        {
                return (superuser != null) && superuser.booleanValue();
        }

        protected void setSuperuser(Boolean superuser)
        {
                this.superuser = superuser;
        }

        public void setSuperuser(User editor, Boolean superuser)
                throws ModelException
        {
                if(!mayChangeSuperuser(editor))
                        throw new ModelSecurityException();

                this.superuser = superuser;
        }

        /**
         *
         * @hibernate.set                    lazy="true"
         * @hibernate.collection-key         column="usr"
         * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.UserLogin"
         */
        protected Collection getLoginHistory()
        {
                return loginHistory;
        }

        public Collection getLogins()
        {
                return Collections.unmodifiableCollection(loginHistory);
        }

        protected void setLoginHistory(Collection loginHistory)
        {
                this.loginHistory = loginHistory;
        }

        public boolean equals(Object o)
        {
                if(o == null || !(o instanceof User)) return false;

                User u = (User)o;
                return (getId() != null) && (u.getId() != null) && (getId().equals(u.getId()));
        }

        public int hashCode()
        {
                if(getId() == null)
                        return 0;
                else
                        return getId().hashCode();
        }

        public String getTypeKey()
        {
                return ak.hostadmiral.core.CoreResources.TYPE_USER;
        }

        public String getIdentKey()
        {
                return ak.hostadmiral.core.CoreResources.IDENT_USER;
        }

        public Object[] getIdentParams()
        {
                return new Object[] { getLogin() };
        }

        public boolean viewableBy(User user)
        {
                return user.isSuperuser() || user.equals(boss) || user.equals(this);
        }

        public boolean editableBy(User user)
        {
                return user.isSuperuser() || user.equals(boss);
        }

        public boolean deleteableBy(User user)
        {
                return !user.equals(this) && (user.isSuperuser() || user.equals(boss));
        }

        public boolean mayChangePassword(User user)
        {
                return user.isSuperuser() || user.equals(boss) || user.equals(this);
        }

        public boolean mayChangeSuperuser(User user)
        {
                return user.isSuperuser() && !user.equals(this);
        }

        public boolean mayViewAllLogins()
        {
                return isSuperuser();
        }

        protected static boolean allowedToCreate(UserManager manager, User editor)
                throws ModelException
        {
                return editor.isSuperuser();
        }

        protected static User createLimitedCopy(User origin)
        {
                User u = new User();
                u.setLogin(origin.getLogin());
                return u;
        }
}