Subversion Repositories general

Rev

Rev 950 | Rev 1010 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.core.model;
919 dev 2
 
949 dev 3
import java.util.Collection;
4
import java.util.Collections;
950 dev 5
import java.util.Locale;
6
import java.util.StringTokenizer;
949 dev 7
 
924 dev 8
import ak.hostadmiral.util.Digest;
9
import ak.hostadmiral.util.ModelException;
10
import ak.hostadmiral.util.ModelSecurityException;
919 dev 11
 
12
/**
13
 *
14
 * @hibernate.class table="users"
15
 */
16
public class User
17
	extends GeneralModelObject
18
{
949 dev 19
	private String     login;
20
	private String     password;
21
	private User       boss;
22
	private Boolean    superuser;
950 dev 23
	private Locale     locale = Locale.getDefault();
949 dev 24
	private Collection loginHistory;
919 dev 25
 
26
	protected User()
27
	{
28
	}
29
 
30
	/**
31
	 *
32
	 * @hibernate.property
33
	 */
34
	public String getLogin()
35
	{
36
		return login;
37
	}
38
 
39
	protected void setLogin(String login)
40
	{
41
		this.login = login;
42
	}
43
 
44
	public void setLogin(User editor, String login)
45
		throws ModelException
46
	{
47
		if(!editableBy(editor))
48
			throw new ModelSecurityException();
49
 
50
		this.login = login;
51
	}
52
 
53
	/**
54
	 *
55
	 * @hibernate.property
56
	 */
57
	protected String getPassword()
58
	{
59
		return password;
60
	}
61
 
62
	protected void setPassword(String password)
63
	{
899 dev 64
		this.password = password;
919 dev 65
	}
66
 
67
	public void setPassword(User editor, String password)
68
		throws ModelException
69
	{
950 dev 70
		if(!partEditableBy(editor))
919 dev 71
			throw new ModelSecurityException();
72
 
73
		if(password == null)
74
			throw new NullPointerException("Null password");
75
 
899 dev 76
		this.password = Digest.encode(password);
919 dev 77
	}
78
 
79
	public boolean checkPassword(String password)
80
	{
81
		if(password == null)
82
			throw new NullPointerException("Null password");
83
 
899 dev 84
		return checkMd5Password(Digest.encode(password));
919 dev 85
	}
86
 
87
	public boolean checkMd5Password(String password)
88
	{
899 dev 89
		return this.password.equals(password);
919 dev 90
	}
911 dev 91
 
92
	/**
93
	 *
94
	 * @hibernate.many-to-one
95
	 */
96
	public User getBoss()
97
	{
98
		return boss;
99
	}
100
 
918 dev 101
	protected void setBoss(User boss)
911 dev 102
	{
103
		this.boss = boss;
104
	}
105
 
918 dev 106
	public void setBoss(User editor, User boss)
919 dev 107
		throws ModelException
918 dev 108
	{
919 dev 109
		if(!editableBy(editor))
110
			throw new ModelSecurityException();
111
 
918 dev 112
		this.boss = boss;
113
	}
114
 
911 dev 115
	/**
116
	 *
117
	 * @hibernate.property
118
	 */
119
	public Boolean getSuperuser()
120
	{
121
		return superuser;
122
	}
123
 
914 dev 124
	public boolean isSuperuser()
125
	{
126
		return (superuser != null) && superuser.booleanValue();
127
	}
919 dev 128
 
918 dev 129
	protected void setSuperuser(Boolean superuser)
911 dev 130
	{
131
		this.superuser = superuser;
132
	}
133
 
918 dev 134
	public void setSuperuser(User editor, Boolean superuser)
919 dev 135
		throws ModelException
918 dev 136
	{
919 dev 137
		if(!mayChangeSuperuser(editor))
138
			throw new ModelSecurityException();
139
 
918 dev 140
		this.superuser = superuser;
141
	}
919 dev 142
 
949 dev 143
	/**
144
	 *
950 dev 145
	 * @hibernate.property column="locale"
146
	 */
147
	protected String getLocaleName()
148
	{
149
		return locale.toString();
150
	}
151
 
152
	protected void setLocaleName(String localeName)
153
	{
154
		String language = null;
155
		String country  = null;
156
 
157
		if(localeName != null) {
158
			StringTokenizer t = new StringTokenizer(localeName, "_");
159
			if(t.hasMoreTokens()) language = t.nextToken();
160
			if(t.hasMoreTokens()) country  = t.nextToken();
161
		}
162
 
163
		if(language == null)
164
			this.locale = Locale.getDefault();
165
		else if(country == null)
166
			this.locale = new Locale(language);
167
		else
168
			this.locale = new Locale(language, country);
169
	}
170
 
171
	public void setLocaleName(User editor, String localeName)
172
		throws ModelException
173
	{
174
		if(!partEditableBy(editor))
175
			throw new ModelSecurityException();
176
 
177
		setLocaleName(localeName);
178
	}
179
 
180
	public Locale getLocale()
181
	{
182
		return locale;
183
	}
184
 
185
	public void setLocale(User editor, Locale locale)
186
		throws ModelException
187
	{
188
		if(!partEditableBy(editor))
189
			throw new ModelSecurityException();
190
 
191
		this.locale = locale;
192
	}
193
 
194
	/**
195
	 *
949 dev 196
	 * @hibernate.set                    lazy="true"
197
	 * @hibernate.collection-key         column="usr"
198
	 * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.UserLogin"
199
	 */
200
	protected Collection getLoginHistory()
201
	{
202
		return loginHistory;
203
	}
204
 
205
	public Collection getLogins()
206
	{
207
		return Collections.unmodifiableCollection(loginHistory);
208
	}
209
 
210
	protected void setLoginHistory(Collection loginHistory)
211
	{
212
		this.loginHistory = loginHistory;
213
	}
214
 
919 dev 215
	public boolean equals(Object o)
216
	{
217
		if(o == null || !(o instanceof User)) return false;
218
 
219
		User u = (User)o;
945 dev 220
		return (getId() != null) && (u.getId() != null) && (getId().equals(u.getId()));
919 dev 221
	}
222
 
950 dev 223
	protected void update(User origin)
224
	{
225
		this.login     = origin.login;
226
		this.boss      = origin.boss;
227
		this.superuser = origin.superuser;
228
		this.locale    = origin.locale;
229
	}
230
 
919 dev 231
	public int hashCode()
232
	{
945 dev 233
		if(getId() == null)
919 dev 234
			return 0;
235
		else
945 dev 236
			return getId().hashCode();
919 dev 237
	}
238
 
239
	public String getTypeKey()
240
	{
924 dev 241
		return ak.hostadmiral.core.CoreResources.TYPE_USER;
919 dev 242
	}
243
 
244
	public String getIdentKey()
245
	{
924 dev 246
		return ak.hostadmiral.core.CoreResources.IDENT_USER;
919 dev 247
	}
248
 
249
	public Object[] getIdentParams()
250
	{
251
		return new Object[] { getLogin() };
252
	}
253
 
254
	public boolean viewableBy(User user)
255
	{
256
		return user.isSuperuser() || user.equals(boss) || user.equals(this);
257
	}
258
 
259
	public boolean editableBy(User user)
260
	{
261
		return user.isSuperuser() || user.equals(boss);
262
	}
263
 
264
	public boolean deleteableBy(User user)
265
	{
946 dev 266
		return !user.equals(this) && (user.isSuperuser() || user.equals(boss));
919 dev 267
	}
268
 
950 dev 269
	// editor is allowed to change some additional properties
270
	public boolean partEditableBy(User user)
919 dev 271
	{
272
		return user.isSuperuser() || user.equals(boss) || user.equals(this);
273
	}
274
 
275
	public boolean mayChangeSuperuser(User user)
276
	{
277
		return user.isSuperuser() && !user.equals(this);
278
	}
279
 
949 dev 280
	public boolean mayViewAllLogins()
281
	{
282
		return isSuperuser();
283
	}
284
 
919 dev 285
	protected static boolean allowedToCreate(UserManager manager, User editor)
286
		throws ModelException
287
	{
288
		return editor.isSuperuser();
289
	}
946 dev 290
 
291
	protected static User createLimitedCopy(User origin)
292
	{
293
		User u = new User();
294
		u.setLogin(origin.getLogin());
295
		return u;
296
	}
919 dev 297
}