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 915

Subversion Repositories general

Rev

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

Rev Author Line No. Line
904 dev 1
package ak.hostcaptain.core.model;
899 dev 2
 
3
import java.util.*;
4
import net.sf.hibernate.*;
904 dev 5
import ak.hostcaptain.util.HibernateUtil;
6
import ak.hostcaptain.util.ModelException;
914 dev 7
import ak.hostcaptain.util.ModelSecurityException;
899 dev 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(
904 dev 20
					"/ak/hostcaptain/core/model/SystemUser.hbm.xml");
899 dev 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
 
914 dev 37
	public SystemUser create(User editor)
38
		throws ModelException
899 dev 39
	{
914 dev 40
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
41
 
899 dev 42
		return new SystemUser();
43
	}
44
 
914 dev 45
	public boolean allowedToCreate(User editor)
899 dev 46
		throws ModelException
47
	{
914 dev 48
		return editor.isSuperuser();
49
	}
50
 
51
	public SystemUser get(User editor, Long id)
52
		throws ModelException
53
	{
54
		SystemUser user;
55
 
899 dev 56
		try {
914 dev 57
			user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
899 dev 58
		}
59
		catch(HibernateException ex)
60
		{
61
			throw new ModelException(ex);
62
		}
914 dev 63
 
64
		if(!user.viewableBy(editor))
65
			throw new ModelSecurityException();
66
 
67
		return user;
899 dev 68
	}
69
 
914 dev 70
	protected SystemUser findForName(String name)
899 dev 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
 
914 dev 88
	protected SystemUser findForUid(Integer uid)
899 dev 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
 
914 dev 106
	public void save(User editor, SystemUser systemUser)
899 dev 107
		throws ModelException
108
	{
914 dev 109
		if(!systemUser.editableBy(editor))
110
			throw new ModelSecurityException();
111
 
899 dev 112
		try {
113
			HibernateUtil.currentSession().saveOrUpdate(systemUser);
114
		}
115
		catch(HibernateException ex)
116
		{
117
			throw new ModelException(ex);
118
		}
119
	}
120
 
914 dev 121
	public void delete(User editor, SystemUser systemUser)
899 dev 122
		throws ModelException
123
	{
914 dev 124
		if(!systemUser.deleteableBy(editor))
125
			throw new ModelSecurityException();
126
 
899 dev 127
		try {
128
			HibernateUtil.currentSession().delete(systemUser);
129
		}
130
		catch(HibernateException ex)
131
		{
132
			throw new ModelException(ex);
133
		}
134
	}
135
 
914 dev 136
	public Collection listSystemUsers(User editor)
899 dev 137
		throws ModelException
138
	{
139
		try {
915 dev 140
			if(editor.isSuperuser()) {
914 dev 141
				return HibernateUtil.currentSession().find("from SystemUser");
915 dev 142
			}
143
			else {
914 dev 144
				return HibernateUtil.currentSession().find(
145
					"select u from SystemUser u left join u.owner o where o is null or o=?",
146
					editor, Hibernate.entity(User.class));
915 dev 147
			}
899 dev 148
		}
149
		catch(HibernateException ex)
150
		{
151
			throw new ModelException(ex);
152
		}
153
	}
154
 
914 dev 155
	public boolean areSystemUsersAvailable(User editor)
156
		throws ModelException
157
	{
158
		try {
159
			if(editor.isSuperuser())
160
				return true;
161
			else
162
				return ((Integer)HibernateUtil.currentSession().iterate(
163
					"select count(*) from SystemUser u left join u.owner o where o is null or o=?",
164
					editor, Hibernate.entity(User.class)).next()).intValue() > 0;
165
		}
166
		catch(HibernateException ex)
167
		{
168
			throw new ModelException(ex);
169
		}
170
	}
171
 
899 dev 172
	private static SystemUserManager systemUserManager = null;
173
 
174
	public static SystemUserManager getInstance()
175
	{
176
		if(systemUserManager == null)
177
			systemUserManager = new SystemUserManager();
178
 
179
		return systemUserManager;
180
	}
181
 
182
	public static final Comparator UID_COMPARATOR  = new UidComparator();
183
	public static final Comparator NAME_COMPARATOR = new NameComparator();
184
 
185
	private static class UidComparator
186
		implements Comparator
187
	{
188
		public int compare(Object o1, Object o2)
189
		{
190
			if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
191
				throw new ClassCastException("not a SystemUser");
192
 
193
		    SystemUser a1 = (SystemUser)o1;
194
		    SystemUser a2 = (SystemUser)o2;
195
 
196
		    if(a1 == null && a2 == null)
197
		    	return 0;
198
		    else if(a1 == null && a2 != null)
199
		    	return -1;
200
		    else if(a1 != null && a2 == null)
201
		    	return 1;
202
		    else
203
		    	return a1.getUid().compareTo(a2.getUid());
204
		}
205
 
206
		public boolean equals(Object obj)
207
		{
208
			return (obj instanceof UidComparator);
209
		}
210
	}
211
 
212
	private static class NameComparator
213
		implements Comparator
214
	{
215
		public int compare(Object o1, Object o2)
216
		{
217
			if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser))
218
				throw new ClassCastException("not a SystemUser");
219
 
220
		    SystemUser a1 = (SystemUser)o1;
221
		    SystemUser a2 = (SystemUser)o2;
222
 
223
		    if(a1 == null && a2 == null)
224
		    	return 0;
225
		    else if(a1 == null && a2 != null)
226
		    	return -1;
227
		    else if(a1 != null && a2 == null)
228
		    	return 1;
229
		    else
230
		    	return a1.getName().compareToIgnoreCase(a2.getName());
231
		}
232
 
233
		public boolean equals(Object obj)
234
		{
235
			return (obj instanceof NameComparator);
236
		}
237
	}
238
}