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 919

Subversion Repositories general

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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