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 | |||
1045 | dev | 44 | protected void resetOrigin() |
45 | { |
||
46 | origin = null; |
||
47 | } |
||
48 | |||
919 | dev | 49 | /** |
50 | * |
||
51 | * @hibernate.property |
||
52 | */ |
||
53 | public String getAddress() |
||
54 | { |
||
55 | return address; |
||
56 | } |
||
57 | |||
921 | dev | 58 | protected void setAddress(String address) |
919 | dev | 59 | { |
60 | this.address = address; |
||
61 | } |
||
62 | |||
921 | dev | 63 | public void setAddress(User editor, String address) |
64 | throws ModelException |
||
65 | { |
||
66 | if(!editableBy(editor)) |
||
67 | throw new ModelSecurityException(); |
||
68 | |||
69 | this.address = address; |
||
70 | } |
||
71 | |||
919 | dev | 72 | /** |
73 | * |
||
74 | * @hibernate.many-to-one |
||
75 | */ |
||
76 | public InetDomain getDomain() |
||
77 | { |
||
78 | return domain; |
||
79 | } |
||
80 | |||
921 | dev | 81 | protected void setDomain(InetDomain domain) |
919 | dev | 82 | { |
83 | this.domain = domain; |
||
84 | } |
||
85 | |||
921 | dev | 86 | public void setDomain(User editor, InetDomain domain) |
87 | throws ModelException |
||
88 | { |
||
89 | if(!editableBy(editor)) |
||
90 | throw new ModelSecurityException(); |
||
91 | |||
92 | this.domain = domain; |
||
93 | } |
||
94 | |||
919 | dev | 95 | /** |
96 | * |
||
97 | * @hibernate.many-to-one |
||
98 | */ |
||
99 | public User getOwner() |
||
100 | { |
||
101 | return owner; |
||
102 | } |
||
103 | |||
921 | dev | 104 | protected void setOwner(User owner) |
919 | dev | 105 | { |
106 | this.owner = owner; |
||
107 | } |
||
108 | |||
921 | dev | 109 | public void setOwner(User editor, User owner) |
110 | throws ModelException |
||
111 | { |
||
112 | if(!editableBy(editor)) |
||
113 | throw new ModelSecurityException(); |
||
114 | |||
115 | this.owner = owner; |
||
116 | } |
||
117 | |||
919 | dev | 118 | /** |
119 | * @return Collection(MailAliasDestination) |
||
120 | * |
||
1016 | dev | 121 | * @hibernate.bag inverse="true" cascade="all-delete-orphan" lazy="true" |
919 | dev | 122 | * @hibernate.collection-key column="alias" |
924 | dev | 123 | * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.MailAliasDestination" |
919 | dev | 124 | */ |
921 | dev | 125 | protected Collection getDestinations() |
919 | dev | 126 | { |
127 | return destinations; |
||
128 | } |
||
129 | |||
921 | dev | 130 | /** |
131 | * @return Collection(MailAliasDestination) |
||
132 | */ |
||
133 | public Collection getDestinations(User editor) |
||
134 | throws ModelException |
||
135 | { |
||
136 | if(mayChangeDestinations(editor)) |
||
137 | return destinations; |
||
138 | else if(viewableBy(editor)) |
||
139 | return java.util.Collections.unmodifiableCollection(destinations); |
||
140 | else |
||
141 | throw new ModelSecurityException(); |
||
142 | } |
||
143 | |||
919 | dev | 144 | /** |
145 | * @param destinations Collection(MailAliasDestination) |
||
146 | */ |
||
147 | protected void setDestinations(Collection destinations) |
||
148 | { |
||
149 | this.destinations = destinations; |
||
150 | } |
||
151 | |||
152 | public String getTypeKey() |
||
153 | { |
||
1051 | dev | 154 | return ak.hostadmiral.core.resources.CoreResources.TYPE_MAIL_ALIAS; |
919 | dev | 155 | } |
156 | |||
157 | public String getIdentKey() |
||
158 | { |
||
1051 | dev | 159 | return ak.hostadmiral.core.resources.CoreResources.IDENT_MAIL_ALIAS; |
919 | dev | 160 | } |
161 | |||
162 | public Object[] getIdentParams() |
||
163 | { |
||
164 | return new Object[] { getAddress(), getDomain().getName() }; |
||
165 | } |
||
166 | |||
167 | public boolean viewableBy(User user) |
||
168 | { |
||
169 | return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner); |
||
170 | } |
||
171 | |||
172 | public boolean editableBy(User user) |
||
173 | { |
||
1010 | dev | 174 | return user.isSuperuser() |
175 | || (domain == null) // just created |
||
176 | || user.equals(domain.getOwner()); |
||
919 | dev | 177 | } |
178 | |||
921 | dev | 179 | public boolean mayChangeDestinations(User user) |
180 | { |
||
181 | return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner); |
||
182 | } |
||
183 | |||
919 | dev | 184 | public boolean deleteableBy(User user) |
185 | { |
||
186 | return user.isSuperuser() || user.equals(domain.getOwner()); |
||
187 | } |
||
921 | dev | 188 | |
189 | protected static boolean allowedToCreate(MailAliasManager manager, User editor) |
||
190 | throws ModelException |
||
191 | { |
||
192 | return editor.isSuperuser() |
||
193 | || InetDomainManager.getInstance().areInetDomainsAvailable(editor); |
||
194 | } |
||
1011 | dev | 195 | |
196 | protected static MailAlias createLimitedCopy(MailAlias origin) |
||
197 | { |
||
198 | MailAlias m = new MailAlias(); |
||
199 | m.setAddress(origin.getAddress()); |
||
200 | m.setDomain(origin.getDomain()); |
||
201 | m.setOwner(origin.getOwner()); |
||
202 | return m; |
||
203 | } |
||
919 | dev | 204 | } |