Subversion Repositories general

Rev

Rev 1011 | Rev 1045 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.core.model;
919 dev 2
 
3
import java.util.Collection;
924 dev 4
import ak.hostadmiral.util.ModelException;
5
import ak.hostadmiral.util.ModelSecurityException;
919 dev 6
 
7
/**
8
 *
9
 * @hibernate.class table="mailaliases"
10
 */
11
public class MailAlias
12
	extends GeneralModelObject
13
{
14
	private String     address;
15
	private InetDomain domain;
16
	private User       owner;
17
	private Collection destinations; // Collection(MailAliasDestintion)
1011 dev 18
	private MailAlias  origin;  // save original object state before any changes
919 dev 19
 
20
	protected MailAlias()
21
	{
22
	}
23
 
1011 dev 24
	protected MailAlias(MailAlias origin)
25
	{
26
		super(origin);
27
		this.address      = origin.address;
28
		this.domain       = origin.domain;
29
		this.owner        = origin.owner;
30
		this.destinations = origin.destinations; // FIXME: or make a copy?
31
	}
32
 
33
    protected MailAlias getOrigin()
34
    {
35
        return origin;
36
    }
37
 
38
    protected void backupMe()
39
    {
40
        if(origin == null)
41
            origin = new MailAlias(this);
42
    }
43
 
919 dev 44
	/**
45
	 *
46
	 * @hibernate.property
47
	 */
48
	public String getAddress()
49
	{
50
		return address;
51
	}
52
 
921 dev 53
	protected void setAddress(String address)
919 dev 54
	{
55
		this.address = address;
56
	}
57
 
921 dev 58
	public void setAddress(User editor, String address)
59
		throws ModelException
60
	{
61
		if(!editableBy(editor))
62
			throw new ModelSecurityException();
63
 
64
		this.address = address;
65
	}
66
 
919 dev 67
	/**
68
	 *
69
	 * @hibernate.many-to-one
70
	 */
71
	public InetDomain getDomain()
72
	{
73
		return domain;
74
	}
75
 
921 dev 76
	protected void setDomain(InetDomain domain)
919 dev 77
	{
78
		this.domain = domain;
79
	}
80
 
921 dev 81
	public void setDomain(User editor, InetDomain domain)
82
		throws ModelException
83
	{
84
		if(!editableBy(editor))
85
			throw new ModelSecurityException();
86
 
87
		this.domain = domain;
88
	}
89
 
919 dev 90
	/**
91
	 *
92
	 * @hibernate.many-to-one
93
	 */
94
	public User getOwner()
95
	{
96
		return owner;
97
	}
98
 
921 dev 99
	protected void setOwner(User owner)
919 dev 100
	{
101
		this.owner = owner;
102
	}
103
 
921 dev 104
	public void setOwner(User editor, User owner)
105
		throws ModelException
106
	{
107
		if(!editableBy(editor))
108
			throw new ModelSecurityException();
109
 
110
		this.owner = owner;
111
	}
112
 
919 dev 113
 	/**
114
 	 * @return Collection(MailAliasDestination)
115
 	 *
1016 dev 116
 	 * @hibernate.bag inverse="true" cascade="all-delete-orphan" lazy="true"
919 dev 117
	 * @hibernate.collection-key column="alias"
924 dev 118
     * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.MailAliasDestination"
919 dev 119
     */
921 dev 120
	protected Collection getDestinations()
919 dev 121
	{
122
		return destinations;
123
	}
124
 
921 dev 125
 	/**
126
 	 * @return Collection(MailAliasDestination)
127
	 */
128
	public Collection getDestinations(User editor)
129
		throws ModelException
130
	{
131
		if(mayChangeDestinations(editor))
132
			return destinations;
133
		else if(viewableBy(editor))
134
			return java.util.Collections.unmodifiableCollection(destinations);
135
		else
136
			throw new ModelSecurityException();
137
	}
138
 
919 dev 139
	/**
140
	 * @param destinations Collection(MailAliasDestination)
141
	 */
142
	protected void setDestinations(Collection destinations)
143
	{
144
		this.destinations = destinations;
145
	}
146
 
147
	public String getTypeKey()
148
	{
924 dev 149
		return ak.hostadmiral.core.CoreResources.TYPE_MAIL_ALIAS;
919 dev 150
	}
151
 
152
	public String getIdentKey()
153
	{
924 dev 154
		return ak.hostadmiral.core.CoreResources.IDENT_MAIL_ALIAS;
919 dev 155
	}
156
 
157
	public Object[] getIdentParams()
158
	{
159
		return new Object[] { getAddress(), getDomain().getName() };
160
	}
161
 
162
	public boolean viewableBy(User user)
163
	{
164
		return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
165
	}
166
 
167
	public boolean editableBy(User user)
168
	{
1010 dev 169
		return user.isSuperuser()
170
			|| (domain == null) // just created
171
			|| user.equals(domain.getOwner());
919 dev 172
	}
173
 
921 dev 174
	public boolean mayChangeDestinations(User user)
175
	{
176
		return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
177
	}
178
 
919 dev 179
	public boolean deleteableBy(User user)
180
	{
181
		return user.isSuperuser() || user.equals(domain.getOwner());
182
	}
921 dev 183
 
184
	protected static boolean allowedToCreate(MailAliasManager manager, User editor)
185
		throws ModelException
186
	{
187
		return editor.isSuperuser()
188
			|| InetDomainManager.getInstance().areInetDomainsAvailable(editor);
189
	}
1011 dev 190
 
191
	protected static MailAlias createLimitedCopy(MailAlias origin)
192
	{
193
		MailAlias m = new MailAlias();
194
		m.setAddress(origin.getAddress());
195
		m.setDomain(origin.getDomain());
196
		m.setOwner(origin.getOwner());
197
		return m;
198
	}
919 dev 199
}