Subversion Repositories general

Rev

Rev 1010 | Rev 1016 | 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 ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;

/**
 *
 * @hibernate.class table="mailaliases"
 */
public class MailAlias
        extends GeneralModelObject
{
        private String     address;
        private InetDomain domain;
        private User       owner;
        private Collection destinations; // Collection(MailAliasDestintion)
        private MailAlias  origin;  // save original object state before any changes

        protected MailAlias()
        {
        }

        protected MailAlias(MailAlias origin)
        {
                super(origin);
                this.address      = origin.address;
                this.domain       = origin.domain;
                this.owner        = origin.owner;
                this.destinations = origin.destinations; // FIXME: or make a copy?
        }

    protected MailAlias getOrigin()
    {
        return origin;
    }

    protected void backupMe()
    {
        if(origin == null)
            origin = new MailAlias(this);
    }

        /**
         *
         * @hibernate.property
         */
        public String getAddress()
        {
                return address;
        }

        protected void setAddress(String address)
        {
                this.address = address;
        }

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

                this.address = address;
        }

        /**
         *
         * @hibernate.many-to-one
         */
        public InetDomain getDomain()
        {
                return domain;
        }

        protected void setDomain(InetDomain domain)
        {
                this.domain = domain;
        }

        public void setDomain(User editor, InetDomain domain)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

                this.domain = domain;
        }

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

        protected void setOwner(User owner)
        {
                this.owner = owner;
        }

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

                this.owner = owner;
        }

        /**
         * @return Collection(MailAliasDestination)
         *
         * @hibernate.bag inverse="true" cascade="all-delete-orphan"
         * @hibernate.collection-key column="alias"
     * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.MailAliasDestination"
     */
        protected Collection getDestinations()
        {
                return destinations;
        }

        /**
         * @return Collection(MailAliasDestination)
         */
        public Collection getDestinations(User editor)
                throws ModelException
        {
                if(mayChangeDestinations(editor))
                        return destinations;
                else if(viewableBy(editor))
                        return java.util.Collections.unmodifiableCollection(destinations);
                else
                        throw new ModelSecurityException();
        }

        /**
         * @param destinations Collection(MailAliasDestination)
         */
        protected void setDestinations(Collection destinations)
        {
                this.destinations = destinations;
        }

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

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

        public Object[] getIdentParams()
        {
                return new Object[] { getAddress(), getDomain().getName() };
        }

        public boolean viewableBy(User user)
        {
                return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
        }

        public boolean editableBy(User user)
        {
                return user.isSuperuser()
                        || (domain == null) // just created
                        || user.equals(domain.getOwner());
        }

        public boolean mayChangeDestinations(User user)
        {
                return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
        }

        public boolean deleteableBy(User user)
        {
                return user.isSuperuser() || user.equals(domain.getOwner());
        }

        protected static boolean allowedToCreate(MailAliasManager manager, User editor)
                throws ModelException
        {
                return editor.isSuperuser()
                        || InetDomainManager.getInstance().areInetDomainsAvailable(editor);
        }

        protected static MailAlias createLimitedCopy(MailAlias origin)
        {
                MailAlias m = new MailAlias();
                m.setAddress(origin.getAddress());
                m.setDomain(origin.getDomain());
                m.setOwner(origin.getOwner());
                return m;
        }
}