Rev 1011 | Rev 1016 | 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 | |
1015 | dev | 3 | import java.util.Iterator; |
4 | import java.util.Collection; |
||
5 | import java.util.HashSet; |
||
6 | |||
924 | dev | 7 | import ak.hostadmiral.util.ModelException; |
8 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 9 | |
10 | /** |
||
11 | * |
||
12 | * @hibernate.class table="mailboxes" |
||
13 | */ |
||
14 | public class Mailbox |
||
15 | extends GeneralModelObject |
||
16 | { |
||
17 | private String login; |
||
1015 | dev | 18 | private Collection passwords; // Collection(PasswordStore) |
919 | dev | 19 | private InetDomain domain; |
20 | private User owner; |
||
21 | private Boolean virusCheck; |
||
22 | private Boolean spamCheck; |
||
23 | private SystemUser systemUser; |
||
1011 | dev | 24 | private Mailbox origin; // save original object state before any changes |
919 | dev | 25 | |
26 | protected Mailbox() |
||
27 | { |
||
28 | } |
||
29 | |||
1011 | dev | 30 | protected Mailbox(Mailbox origin) |
31 | { |
||
32 | super(origin); |
||
33 | this.login = origin.login; |
||
1015 | dev | 34 | |
35 | if(origin.passwords == null) |
||
36 | this.passwords = null; |
||
37 | else |
||
38 | this.passwords = new HashSet(origin.passwords); |
||
39 | |||
1011 | dev | 40 | this.domain = origin.domain; |
41 | this.owner = origin.owner; |
||
42 | this.virusCheck = origin.virusCheck; |
||
43 | this.spamCheck = origin.spamCheck; |
||
44 | this.systemUser = origin.systemUser; |
||
45 | } |
||
46 | |||
47 | protected Mailbox getOrigin() |
||
48 | { |
||
49 | return origin; |
||
50 | } |
||
51 | |||
52 | protected void backupMe() |
||
53 | { |
||
54 | if(origin == null) |
||
55 | origin = new Mailbox(this); |
||
56 | } |
||
57 | |||
919 | dev | 58 | /** |
59 | * |
||
60 | * @hibernate.property |
||
61 | */ |
||
62 | public String getLogin() |
||
63 | { |
||
64 | return login; |
||
65 | } |
||
66 | |||
921 | dev | 67 | protected void setLogin(String login) |
919 | dev | 68 | { |
69 | this.login = login; |
||
70 | } |
||
71 | |||
921 | dev | 72 | public void setLogin(User editor, String login) |
73 | throws ModelException |
||
74 | { |
||
75 | if(!editableBy(editor)) |
||
76 | throw new ModelSecurityException(); |
||
77 | |||
1011 | dev | 78 | backupMe(); |
921 | dev | 79 | this.login = login; |
80 | } |
||
81 | |||
1015 | dev | 82 | protected void addPasswordStore(PasswordStore ps) |
919 | dev | 83 | { |
1015 | dev | 84 | if(passwords == null) passwords = new HashSet(); |
85 | passwords.add(ps); |
||
919 | dev | 86 | } |
87 | |||
921 | dev | 88 | public void setPassword(User editor, String password) |
89 | throws ModelException |
||
919 | dev | 90 | { |
1015 | dev | 91 | if(!editableBy(editor)) |
92 | throw new ModelSecurityException(); |
||
93 | |||
919 | dev | 94 | if(password == null) |
95 | throw new NullPointerException("Null password"); |
||
96 | |||
1011 | dev | 97 | backupMe(); |
1015 | dev | 98 | |
99 | for(Iterator i = passwords.iterator(); i.hasNext(); ) { |
||
100 | Object o = i.next(); |
||
101 | if(!(o instanceof PasswordStore)) |
||
102 | throw new ModelException("It's not a password store"); |
||
103 | ((PasswordStore)o).setNewPassword(password); |
||
104 | } |
||
919 | dev | 105 | } |
106 | |||
1015 | dev | 107 | /** |
108 | * |
||
109 | * @hibernate.set cascade="all" |
||
110 | * @hibernate.collection-key column="obj" |
||
111 | * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.PasswordStoreAbstract" |
||
112 | */ |
||
113 | protected Collection getPasswords() |
||
114 | { |
||
115 | return passwords; |
||
116 | } |
||
117 | |||
118 | protected void setPasswords(Collection passwords) |
||
119 | { |
||
120 | this.passwords = passwords; |
||
121 | } |
||
122 | |||
919 | dev | 123 | /** |
124 | * |
||
125 | * @hibernate.many-to-one |
||
126 | */ |
||
127 | public InetDomain getDomain() |
||
128 | { |
||
129 | return domain; |
||
130 | } |
||
131 | |||
921 | dev | 132 | protected void setDomain(InetDomain domain) |
919 | dev | 133 | { |
134 | this.domain = domain; |
||
135 | } |
||
136 | |||
921 | dev | 137 | public void setDomain(User editor, InetDomain domain) |
138 | throws ModelException |
||
139 | { |
||
140 | if(!editableBy(editor)) |
||
141 | throw new ModelSecurityException(); |
||
142 | |||
1011 | dev | 143 | backupMe(); |
921 | dev | 144 | this.domain = domain; |
145 | } |
||
146 | |||
919 | dev | 147 | /** |
148 | * |
||
149 | * @hibernate.many-to-one |
||
150 | */ |
||
151 | public User getOwner() |
||
152 | { |
||
153 | return owner; |
||
154 | } |
||
155 | |||
921 | dev | 156 | protected void setOwner(User owner) |
919 | dev | 157 | { |
158 | this.owner = owner; |
||
159 | } |
||
160 | |||
921 | dev | 161 | public void setOwner(User editor, User owner) |
162 | throws ModelException |
||
163 | { |
||
164 | if(!editableBy(editor)) |
||
165 | throw new ModelSecurityException(); |
||
166 | |||
1011 | dev | 167 | backupMe(); |
921 | dev | 168 | this.owner = owner; |
169 | } |
||
170 | |||
919 | dev | 171 | /** |
172 | * |
||
173 | * @hibernate.property |
||
174 | */ |
||
175 | public Boolean getVirusCheck() |
||
176 | { |
||
177 | return virusCheck; |
||
178 | } |
||
179 | |||
921 | dev | 180 | protected void setVirusCheck(Boolean virusCheck) |
919 | dev | 181 | { |
182 | this.virusCheck = virusCheck; |
||
183 | } |
||
184 | |||
921 | dev | 185 | public void setVirusCheck(User editor, Boolean virusCheck) |
186 | throws ModelException |
||
187 | { |
||
188 | if(!editableBy(editor)) |
||
189 | throw new ModelSecurityException(); |
||
190 | |||
1011 | dev | 191 | backupMe(); |
921 | dev | 192 | this.virusCheck = virusCheck; |
193 | } |
||
194 | |||
919 | dev | 195 | /** |
196 | * |
||
197 | * @hibernate.property |
||
198 | */ |
||
199 | public Boolean getSpamCheck() |
||
200 | { |
||
201 | return spamCheck; |
||
202 | } |
||
203 | |||
921 | dev | 204 | protected void setSpamCheck(Boolean spamCheck) |
919 | dev | 205 | { |
206 | this.spamCheck = spamCheck; |
||
207 | } |
||
208 | |||
921 | dev | 209 | public void setSpamCheck(User editor, Boolean spamCheck) |
210 | throws ModelException |
||
211 | { |
||
212 | if(!editableBy(editor)) |
||
213 | throw new ModelSecurityException(); |
||
214 | |||
1011 | dev | 215 | backupMe(); |
921 | dev | 216 | this.spamCheck = spamCheck; |
217 | } |
||
218 | |||
919 | dev | 219 | /** |
220 | * |
||
221 | * @hibernate.many-to-one |
||
222 | */ |
||
223 | public SystemUser getSystemUser() |
||
224 | { |
||
225 | return systemUser; |
||
226 | } |
||
227 | |||
921 | dev | 228 | protected void setSystemUser(SystemUser systemUser) |
919 | dev | 229 | { |
230 | this.systemUser = systemUser; |
||
231 | } |
||
232 | |||
921 | dev | 233 | public void setSystemUser(User editor, SystemUser systemUser) |
234 | throws ModelException |
||
235 | { |
||
236 | if(!editableBy(editor)) |
||
237 | throw new ModelSecurityException(); |
||
238 | |||
1011 | dev | 239 | backupMe(); |
921 | dev | 240 | this.systemUser = systemUser; |
241 | } |
||
242 | |||
919 | dev | 243 | public String getTypeKey() |
244 | { |
||
924 | dev | 245 | return ak.hostadmiral.core.CoreResources.TYPE_MAILBOX; |
919 | dev | 246 | } |
247 | |||
248 | public String getIdentKey() |
||
249 | { |
||
924 | dev | 250 | return ak.hostadmiral.core.CoreResources.IDENT_MAILBOX; |
919 | dev | 251 | } |
252 | |||
253 | public Object[] getIdentParams() |
||
254 | { |
||
255 | return new Object[] { getLogin(), getDomain().getName() }; |
||
256 | } |
||
257 | |||
258 | public boolean viewableBy(User user) |
||
259 | { |
||
260 | return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner); |
||
261 | } |
||
262 | |||
263 | public boolean editableBy(User user) |
||
264 | { |
||
1010 | dev | 265 | return user.isSuperuser() |
266 | || (domain == null) // just created |
||
267 | || user.equals(domain.getOwner()); |
||
919 | dev | 268 | } |
269 | |||
270 | public boolean deleteableBy(User user) |
||
271 | { |
||
272 | return user.isSuperuser() || user.equals(domain.getOwner()); |
||
273 | } |
||
921 | dev | 274 | |
275 | protected static boolean allowedToCreate(MailboxManager manager, User editor) |
||
276 | throws ModelException |
||
277 | { |
||
278 | return editor.isSuperuser() |
||
279 | || InetDomainManager.getInstance().areInetDomainsAvailable(editor); |
||
280 | } |
||
949 | dev | 281 | |
282 | protected static Mailbox createLimitedCopy(Mailbox origin) |
||
283 | { |
||
1011 | dev | 284 | Mailbox m = new Mailbox(); |
285 | m.setLogin(origin.getLogin()); |
||
286 | m.setDomain(origin.getDomain()); |
||
287 | m.setOwner(origin.getOwner()); |
||
288 | return m; |
||
949 | dev | 289 | } |
919 | dev | 290 | } |