View Javadoc

1   /*
2    * Created on Jun 20, 2006
3    *
4    */
5   package org.musicontroller.security;
6   
7   import java.util.Date;
8   import java.util.HashSet;
9   import java.util.LinkedList;
10  import java.util.List;
11  import java.util.Set;
12  
13  import org.acegisecurity.GrantedAuthority;
14  
15  /**
16   * Implements a User
17   * @author Arjan Verstoep
18   * @version $Id: User.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
19   */
20  public class User implements IUser {
21  	
22  	static final long serialVersionUID = 200606201141L;
23  	
24  	private Date _expiryDate;
25  	private boolean _locked;
26  	private boolean _credentialsExpired;
27  	private String _loginname;
28  	private String _name;
29  	private String _encryptedPassword;
30  	private String _disabledMessage;
31  	private Set<Role> _roles;
32  	private long _id;
33  
34  	/**
35  	 * Creates a new non-expired, non-locked, non-disabled User-object with
36  	 * an empty loginname and an empty username. This new object cannot be used to login
37  	 * before the password is set using the setUnencryptedPassword-method, and being
38  	 * persisted using the UserDao.
39  	 * 
40  	 *  @see UserDao
41  	 *  @see IUser
42  	 *  @see Encrypter
43  	 *  @see Role
44  	 *  @see Authority
45  	 */
46  	public User() {
47  		_id = -1L;
48  		_expiryDate = null;
49  		_locked = false;
50  		_credentialsExpired = false;
51  		_loginname = "";
52  		_name = "";
53  		_encryptedPassword = "";
54  		_disabledMessage = "";
55  		_roles = new HashSet<Role>();
56  	}
57  	
58  	public void setId(long id) {
59  		_id = id;
60  	}
61  	
62  	public long getId() {
63  		return _id;
64  	}
65  	
66  	public boolean isAccountNonExpired() {
67  		return (_expiryDate==null) ? true : 
68  				_expiryDate.after(new Date());
69  	}
70  	
71  	public void setExpiryDate(Date expiryDate) {
72  		_expiryDate = expiryDate;
73  	}
74  	
75  	public Date getExpiryDate() {
76  		return _expiryDate;
77  	}
78  
79  	public boolean isAccountNonLocked() {
80  		return !_locked;
81  	}
82  	
83  	public void setAccountNonLocked(boolean nonLocked) {
84  		_locked = !nonLocked;
85  	}
86  
87  	public GrantedAuthority[] getAuthorities() {
88  		GrantedAuthority[] sampleArray = new GrantedAuthority[0];
89  		
90  		Set<GrantedAuthority> allAuthorities = new HashSet<GrantedAuthority>();
91  		for (Role r : _roles) {
92  			allAuthorities.addAll(r.getAuthorities());
93  		}
94  		return allAuthorities.toArray(sampleArray);
95  	}
96  	
97  	/**
98  	 * @return True if this User is an Admin.
99  	 */
100 	public boolean isAdmin() {
101 		return hasRole("ROLE_ADMIN");
102 	}
103 	
104 	/**
105 	 * Returns whether or not this User has a certain role.
106 	 * @param roledescription The description of the role, such as ROLE_USER
107 	 * @return True, if this User has the specified role.
108 	 */
109 	public boolean hasRole(String roledescription) {
110 		if (roledescription==null) return false;
111 		
112 		GrantedAuthority[] authorities = getAuthorities();
113 		for (int i=0;i<authorities.length;i++) {
114 			if (roledescription.equalsIgnoreCase(authorities[i].getAuthority())) {
115 				return true;
116 			}
117 		}
118 		return false;
119 	}
120 	
121 	public void setRoles(Set<Role> roles) {
122 		_roles = roles;
123 	}
124 	
125 	public Set<Role> getRoles() {
126 		return _roles;
127 	}
128 	
129 	public void addRole(Role role) {
130 		_roles.add(role);
131 	}
132 
133 	public boolean isCredentialsNonExpired() {
134 		return !_credentialsExpired;
135 	}
136 	
137 	public void setCredentialsNonExired(boolean nonExpired) {
138 		_credentialsExpired = !nonExpired;
139 	}
140 
141 	public boolean isEnabled() {
142 		return "".equals(_disabledMessage);
143 	}
144 	
145 	public String getPassword() {
146 		return _encryptedPassword;
147 	}
148 	
149 	public void setPassword(String encryptedPassword) {
150 		_encryptedPassword = encryptedPassword;
151 	}
152 
153 	public String getUsername() {
154 		return _loginname;
155 	}
156 	
157 	public String getLoginname() {
158 		return _loginname;
159 	}
160 	
161 	public void setLoginname(String loginname) {
162 		_loginname = loginname;
163 	}
164 	
165 	public String getName() {
166 		return _name;
167 	}
168 	
169 	public void setName(String name) {
170 		_name = name;
171 	}
172 	
173 	/**
174 	 * When the disabledmessage is set, the User is automatically disabled, and cannot logon.
175 	 * @param message A message specifying why the user is disabled.
176 	 */
177 	public void setDisabledMessage(String message) {
178 		_disabledMessage = message==null ? "" : message;
179 	}
180 
181 	public void setUnencryptedPassword(String unencryptedpass) {
182 		this._encryptedPassword=Encrypter.encrypt(unencryptedpass);
183 	}
184 
185 	/**
186 	 * A short description of the user, fit for human
187 	 * consumption. Note: This name need not be unique.
188 	 * @return The users' name.
189 	 */
190 	public String toString() {
191 		return getUsername();
192 	}
193 	
194 	public boolean equals(Object o) {
195 		if (o==null) return false;
196 		if (o instanceof User) {
197 			User other = (User) o;
198 			return getId()==other.getId();
199 		}
200  		return false;
201  	}
202 	
203 	/*
204 	 * (non-Javadoc)
205 	 * @see java.lang.Object#hashCode()
206 	 */
207 	@Override
208 	public int hashCode() {
209 		return Long.valueOf(_id).hashCode();
210 	}
211 
212 	/*
213 	 * (non-Javadoc)
214 	 * @see org.musicontroller.security.IUser#getRolesList()
215 	 */
216 	public List<Role> getRolesList() {
217 		List<Role> result = new LinkedList<Role>();
218 		result.addAll(_roles);
219 		return result;
220 	}
221 
222 	/*
223 	 * (non-Javadoc)
224 	 * @see org.musicontroller.security.IUser#setRolesList(java.util.List)
225 	 */
226 	public void setRolesList(List<Role> roles) {
227 		_roles.clear();
228 		_roles.addAll(roles);
229 	}
230 
231 
232 }