Go to most recent revision | Details | 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.*; |
||
4 | import net.sf.hibernate.*; |
||
5 | import net.sf.hibernate.type.Type; |
||
924 | dev | 6 | import ak.hostadmiral.util.HibernateUtil; |
7 | import ak.hostadmiral.util.ModelException; |
||
8 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 9 | |
10 | public class UserManager |
||
946 | dev | 11 | implements UserBeforeDeleteListener |
919 | dev | 12 | { |
946 | dev | 13 | private static UserManager userManager = null; |
919 | dev | 14 | private static boolean registered = false; |
946 | dev | 15 | |
16 | public static UserManager getInstance() |
||
17 | { |
||
18 | return userManager; |
||
19 | } |
||
20 | |||
919 | dev | 21 | protected static void register() |
22 | { |
||
23 | synchronized(MailboxManager.class) { |
||
24 | if(registered) return; |
||
25 | |||
26 | registered = true; |
||
27 | try { |
||
28 | HibernateUtil.getConfiguration().addResource( |
||
950 | dev | 29 | "ak/hostadmiral/core/model/User.hbm.xml"); |
949 | dev | 30 | HibernateUtil.getConfiguration().addResource( |
950 | dev | 31 | "ak/hostadmiral/core/model/UserLogin.hbm.xml"); |
946 | dev | 32 | |
33 | userManager = new UserManager(); |
||
919 | dev | 34 | } |
35 | catch(Exception ex) { |
||
36 | ex.printStackTrace(); |
||
37 | throw new RuntimeException(ex.getMessage()); |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | static { |
||
43 | register(); |
||
44 | } |
||
45 | |||
946 | dev | 46 | private Collection beforeDeleteListeners = new ArrayList(); |
950 | dev | 47 | private Map loggedinUsers = new WeakHashMap(); |
946 | dev | 48 | |
919 | dev | 49 | private UserManager() |
50 | { |
||
946 | dev | 51 | addBeforeDeleteListener(this); |
919 | dev | 52 | } |
53 | |||
54 | public User create(User editor) |
||
55 | throws ModelException |
||
56 | { |
||
57 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
58 | |||
59 | return new User(); |
||
60 | } |
||
61 | |||
62 | public boolean allowedToCreate(User editor) |
||
63 | throws ModelException |
||
64 | { |
||
65 | return User.allowedToCreate(this, editor); |
||
66 | } |
||
67 | |||
68 | public User get(User editor, Long id) |
||
69 | throws ModelException |
||
70 | { |
||
71 | User user; |
||
72 | |||
73 | try { |
||
74 | user = (User)HibernateUtil.currentSession().load(User.class, id); |
||
75 | } |
||
76 | catch(HibernateException ex) |
||
77 | { |
||
78 | throw new ModelException(ex); |
||
79 | } |
||
80 | |||
81 | if(!user.viewableBy(editor)) |
||
82 | throw new ModelSecurityException(); |
||
83 | |||
84 | return user; |
||
85 | } |
||
86 | |||
923 | dev | 87 | public boolean loginExists(User editor, User user, String login) |
88 | throws ModelException |
||
89 | { |
||
90 | try { |
||
91 | if(user.getId() == null) |
||
92 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
93 | "select count(*) from User u where login = ?", |
||
94 | login, Hibernate.STRING) |
||
95 | .next()).intValue() > 0; |
||
96 | else |
||
97 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
98 | "select count(*) from User u where login = ? and u != ?", |
||
99 | new Object[] { login, user }, |
||
100 | new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } ) |
||
101 | .next()).intValue() > 0; |
||
102 | } |
||
103 | catch(HibernateException ex) |
||
104 | { |
||
105 | throw new ModelException(ex); |
||
106 | } |
||
107 | } |
||
108 | |||
919 | dev | 109 | public User findForLogin(String login) |
110 | throws ModelException |
||
111 | { |
||
112 | try { |
||
113 | List list = HibernateUtil.currentSession().find( |
||
949 | dev | 114 | "from User where login = ? and enabled = ?", |
951 | dev | 115 | new Object[] { login, Boolean.TRUE }, |
949 | dev | 116 | new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } ); |
919 | dev | 117 | |
118 | if(list.size() == 0) |
||
119 | return null; |
||
120 | else |
||
121 | return (User)list.get(0); |
||
122 | } |
||
123 | catch(HibernateException ex) |
||
124 | { |
||
125 | throw new ModelException(ex); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public void save(User editor, User user) |
||
130 | throws ModelException |
||
131 | { |
||
950 | dev | 132 | if(!user.editableBy(editor) && !user.partEditableBy(editor) |
919 | dev | 133 | && !user.mayChangeSuperuser(editor)) |
134 | { |
||
135 | throw new ModelSecurityException(); |
||
136 | } |
||
137 | |||
138 | user.setModUser(editor); |
||
139 | |||
140 | try { |
||
141 | HibernateUtil.currentSession().saveOrUpdate(user); |
||
142 | } |
||
143 | catch(HibernateException ex) |
||
144 | { |
||
145 | throw new ModelException(ex); |
||
146 | } |
||
950 | dev | 147 | |
148 | // update user if he is logged in |
||
149 | for(Iterator i = loggedinUsers.keySet().iterator(); i.hasNext(); ) { |
||
150 | User u = (User)i.next(); |
||
151 | if(u.equals(user)) |
||
152 | u.update(user); |
||
153 | } |
||
919 | dev | 154 | } |
155 | |||
946 | dev | 156 | public void addBeforeDeleteListener(UserBeforeDeleteListener listener) |
157 | { |
||
158 | beforeDeleteListeners.add(listener); |
||
159 | } |
||
160 | |||
161 | public void removeBeforeDeleteListener(UserBeforeDeleteListener listener) |
||
162 | { |
||
163 | beforeDeleteListeners.remove(listener); |
||
164 | } |
||
165 | |||
949 | dev | 166 | public Collection beforeDelete(User editor, User user, Collection known) |
946 | dev | 167 | throws ModelException |
168 | { |
||
169 | Collection cascade = new ArrayList(); |
||
170 | |||
171 | for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) { |
||
172 | UserBeforeDeleteListener listener = (UserBeforeDeleteListener)i.next(); |
||
949 | dev | 173 | Collection subcascade = listener.userBeforeDelete(editor, user, known); |
946 | dev | 174 | if(subcascade != null) |
175 | cascade.addAll(subcascade); |
||
176 | } |
||
177 | |||
178 | return cascade; |
||
179 | } |
||
180 | |||
919 | dev | 181 | public void delete(User editor, User user) |
182 | throws ModelException |
||
183 | { |
||
184 | if(!user.deleteableBy(editor)) |
||
185 | throw new ModelSecurityException(); |
||
186 | |||
187 | try { |
||
188 | HibernateUtil.currentSession().delete(user); |
||
189 | } |
||
190 | catch(HibernateException ex) |
||
191 | { |
||
192 | throw new ModelException(ex); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | public Collection listUsers(User editor) |
||
197 | throws ModelException |
||
198 | { |
||
199 | try { |
||
200 | if(editor.isSuperuser()) { |
||
201 | return HibernateUtil.currentSession().find("from User"); |
||
202 | } |
||
203 | else { |
||
204 | return HibernateUtil.currentSession().find( |
||
205 | "from User u where u = ? or u.boss = ?", |
||
206 | new Object[] { editor, editor}, |
||
207 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ); |
||
208 | } |
||
209 | } |
||
210 | catch(HibernateException ex) |
||
211 | { |
||
212 | throw new ModelException(ex); |
||
213 | } |
||
214 | } |
||
215 | |||
923 | dev | 216 | public boolean areUsersAvailable(User editor) |
919 | dev | 217 | throws ModelException |
218 | { |
||
219 | try { |
||
220 | if(editor.isSuperuser()) { |
||
221 | return true; |
||
222 | } |
||
223 | else { |
||
224 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
923 | dev | 225 | "select count(*) from User u where u = ? or u.boss = ?", |
919 | dev | 226 | new Object[] { editor, editor}, |
227 | new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } ) |
||
228 | .next()).intValue() > 0; |
||
229 | } |
||
230 | } |
||
231 | catch(HibernateException ex) |
||
232 | { |
||
233 | throw new ModelException(ex); |
||
234 | } |
||
235 | } |
||
236 | |||
949 | dev | 237 | public User loginUser(String login, String password, String ip) |
919 | dev | 238 | throws ModelException |
239 | { |
||
949 | dev | 240 | User user = (login == null || password == null) ? null : findForLogin(login); |
241 | boolean success = (user == null) ? false : user.checkPassword(password); |
||
951 | dev | 242 | UserLogin userLogin = new UserLogin(user, login, new Date(), Boolean.valueOf(success), ip); |
919 | dev | 243 | |
949 | dev | 244 | // save login information |
245 | try { |
||
246 | HibernateUtil.currentSession().saveOrUpdate(userLogin); |
||
919 | dev | 247 | } |
949 | dev | 248 | catch(HibernateException ex) |
249 | { |
||
250 | throw new ModelException(ex); |
||
251 | } |
||
919 | dev | 252 | |
950 | dev | 253 | if(success) { |
254 | loggedinUsers.put(user, Boolean.TRUE); |
||
949 | dev | 255 | return user; |
950 | dev | 256 | } |
257 | else { |
||
949 | dev | 258 | return null; // wrong login or password |
950 | dev | 259 | } |
919 | dev | 260 | } |
261 | |||
949 | dev | 262 | public Collection listFailedLogins(User editor) |
946 | dev | 263 | throws ModelException |
919 | dev | 264 | { |
949 | dev | 265 | if(!editor.mayViewAllLogins()) |
266 | { |
||
267 | throw new ModelSecurityException(); |
||
268 | } |
||
269 | |||
270 | try { |
||
271 | return HibernateUtil.currentSession().find( |
||
272 | "from UserLogin where success = ?", |
||
951 | dev | 273 | Boolean.FALSE, Hibernate.BOOLEAN); |
949 | dev | 274 | } |
275 | catch(HibernateException ex) |
||
276 | { |
||
277 | throw new ModelException(ex); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | public Collection userBeforeDelete(User editor, User user, Collection known) |
||
282 | throws ModelException |
||
283 | { |
||
946 | dev | 284 | Collection subusers; |
919 | dev | 285 | |
946 | dev | 286 | try { |
287 | subusers = HibernateUtil.currentSession().find( |
||
288 | "from User where boss = ?", |
||
289 | user, Hibernate.entity(User.class) ); |
||
290 | } |
||
291 | catch(HibernateException ex) |
||
292 | { |
||
293 | throw new ModelException(ex); |
||
294 | } |
||
295 | |||
296 | Collection cascade = new ArrayList(); |
||
297 | for(Iterator i = subusers.iterator(); i.hasNext(); ) { |
||
298 | User u = (User)i.next(); |
||
299 | if(u.viewableBy(editor)) { |
||
300 | if(u.deleteableBy(editor)) |
||
301 | cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.DELETE, |
||
949 | dev | 302 | this.beforeDelete(editor, u, known))); |
946 | dev | 303 | else |
304 | cascade.add(new CascadeDeleteElement(u, CascadeDeleteElement.FORBIDDEN, null)); |
||
305 | } |
||
306 | else { |
||
307 | cascade.add(new CascadeDeleteElement(User.createLimitedCopy(u), |
||
308 | CascadeDeleteElement.FORBIDDEN, null)); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return cascade; |
||
919 | dev | 313 | } |
314 | |||
315 | public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
||
949 | dev | 316 | public static final Comparator LOGINS_TIME_COMPARATOR = new LoginsTimeComparator(); |
919 | dev | 317 | |
318 | private static class LoginComparator |
||
319 | implements Comparator |
||
320 | { |
||
321 | public int compare(Object o1, Object o2) |
||
322 | { |
||
323 | if(!(o1 instanceof User) || !(o2 instanceof User)) |
||
324 | throw new ClassCastException("not a User"); |
||
325 | |||
326 | User a1 = (User)o1; |
||
327 | User a2 = (User)o2; |
||
328 | |||
329 | if(a1 == null && a2 == null) |
||
330 | return 0; |
||
331 | else if(a1 == null && a2 != null) |
||
332 | return -1; |
||
333 | else if(a1 != null && a2 == null) |
||
334 | return 1; |
||
335 | else |
||
336 | return a1.getLogin().compareToIgnoreCase(a2.getLogin()); |
||
337 | } |
||
338 | |||
339 | public boolean equals(Object obj) |
||
340 | { |
||
341 | return (obj instanceof LoginComparator); |
||
342 | } |
||
343 | } |
||
949 | dev | 344 | |
345 | private static class LoginsTimeComparator |
||
346 | implements Comparator |
||
347 | { |
||
348 | public int compare(Object o1, Object o2) |
||
349 | { |
||
350 | if(!(o1 instanceof UserLogin) || !(o2 instanceof UserLogin)) |
||
351 | throw new ClassCastException("not a UserLogin"); |
||
352 | |||
353 | UserLogin a1 = (UserLogin)o1; |
||
354 | UserLogin a2 = (UserLogin)o2; |
||
355 | |||
356 | if(a1 == null && a2 == null) |
||
357 | return 0; |
||
358 | else if(a1 == null && a2 != null) |
||
359 | return -1; |
||
360 | else if(a1 != null && a2 == null) |
||
361 | return 1; |
||
362 | else |
||
363 | return a1.getLoginTime().compareTo(a2.getLoginTime()); |
||
364 | } |
||
365 | |||
366 | public boolean equals(Object obj) |
||
367 | { |
||
368 | return (obj instanceof LoginComparator); |
||
369 | } |
||
370 | } |
||
919 | dev | 371 | } |