Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "date" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Warning: Attempt to read property "msg" on null in /home/www/websvn.26th.net/html/blame.php on line 247

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /home/www/websvn.26th.net/html/blame.php on line 247
WebSVN – general – Blame – /hostadmiral/trunk/src/ak/hostadmiral/core/model/SystemUserManager.java/ – Rev 950

Subversion Repositories general

Rev

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