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 925

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