Subversion Repositories general

Rev

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