001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.users;
017
018import java.util.Collection;
019import java.util.HashSet;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.log4j.Logger;
027import org.springframework.security.core.GrantedAuthority;
028import org.springframework.security.core.userdetails.UserDetails;
029
030/**
031 * Details of a single user
032 *
033 */
034@XmlRootElement(name=Definitions.ELEMENT_USER_IDENTITY)
035@XmlAccessorType(XmlAccessType.NONE)
036public class UserIdentity implements UserDetails {
037  private static final Logger LOGGER = Logger.getLogger(UserIdentity.class);
038  /** serial UID */
039  private static final long serialVersionUID = 967143020744296850L;
040  private boolean _accountNonExpired = true;
041  private boolean _accountNonLocked = true;
042  private Collection<GrantedAuthority> _authorities = new HashSet<>();
043  private boolean _credentialsNonExpired = true;
044  private boolean _enabled = true;
045  private String _password = null;
046  @XmlElement(name=Definitions.ELEMENT_USER_ID)
047  private Long _userId = null;
048  @XmlElement(name=Definitions.ELEMENT_USERNAME)
049  private String _username = null;
050  
051
052  /**
053   * 
054   * @param userId
055   */
056  public UserIdentity(Long userId){
057    _userId = userId;
058  }
059  
060  /**
061   * create an empty user identity
062   */
063  public UserIdentity(){
064    // nothing needed
065  }
066  
067  /**
068   * 
069   * @param password
070   * @param userId
071   * @param username
072   */
073  public UserIdentity(String password, Long userId, String username){
074    _userId = userId;
075    _username = username;
076    _password = password;
077  }
078  
079  /**
080   * 
081   * @return user id value
082   */
083  public Long getUserId(){
084    return _userId;
085  }
086  
087  /**
088   * 
089   * @param userId can be null
090   * @return true if userId is set
091   */
092  public static boolean isValid(UserIdentity userId){
093    if(userId == null){
094      return false;
095    }else{
096      return userId.isValid();
097    }
098  }
099  
100  /**
101   * use the static, implemented for sub-classing
102   * @return true if valid
103   */
104  protected boolean isValid(){
105    return (_userId == null ? false : true);
106  }
107
108  @Override
109  public Collection<? extends GrantedAuthority> getAuthorities() {
110    return _authorities;
111  }
112
113  /**
114   * @param authority
115   */
116  public void addAuthority(GrantedAuthority authority) {
117    _authorities.add(authority);
118  }
119
120  @Override
121  public String getPassword() {
122    return _password;
123  }
124
125  @Override
126  public String getUsername() {
127    return _username;
128  }
129
130  @Override
131  public boolean isAccountNonExpired() {
132    return _accountNonExpired;
133  }
134
135  @Override
136  public boolean isAccountNonLocked() {
137    return _accountNonLocked;
138  }
139
140  @Override
141  public boolean isCredentialsNonExpired() {
142    return _credentialsNonExpired;
143  }
144
145  @Override
146  public boolean isEnabled() {
147    return _enabled;
148  }
149
150  /**
151   * @param userId the userId to set
152   */
153  public void setUserId(Long userId) {
154    _userId = userId;
155  }
156
157  /**
158   * @param username the username to set
159   */
160  public void setUsername(String username) {
161    _username = username;
162  }
163
164  /**
165   * @param password the password to set
166   */
167  public void setPassword(String password) {
168    _password = password;
169  }
170
171  /**
172   * @param accountNonExpired the accountNonExpired to set
173   */
174  public void setAccountNonExpired(boolean accountNonExpired) {
175    _accountNonExpired = accountNonExpired;
176  }
177
178  /**
179   * @param accountNonLocked the accountNonLocked to set
180   */
181  public void setAccountNonLocked(boolean accountNonLocked) {
182    _accountNonLocked = accountNonLocked;
183  }
184
185  /**
186   * @param credentialsNonExpired the credentialsNonExpired to set
187   */
188  public void setCredentialsNonExpired(boolean credentialsNonExpired) {
189    _credentialsNonExpired = credentialsNonExpired;
190  }
191
192  /**
193   * @param enabled the enabled to set
194   */
195  public void setEnabled(boolean enabled) {
196    _enabled = enabled;
197  }
198  
199  /**
200   * 
201   * @param userIdOne can be null
202   * @param userIdTwo can be null
203   * @return true if the user are the same, comparison is done by user id
204   */
205  public static boolean equals(UserIdentity userIdOne, UserIdentity userIdTwo){
206    if(!UserIdentity.isValid(userIdOne) || !UserIdentity.isValid(userIdTwo)){
207      return false;
208    }else{
209      return (userIdOne.getUserId().equals(userIdTwo.getUserId()));
210    }
211  }
212  
213  /**
214   * 
215   * @param userIdentity can be null
216   * @param userId can be null
217   * @return true if the user are the same, comparison is done by user id
218   */
219  public static boolean equals(UserIdentity userIdentity, Long userId){
220    if(!UserIdentity.isValid(userIdentity) || userId == null){
221      return false;
222    }else{
223      return (userIdentity.getUserId().equals(userId));
224    }
225  }
226  
227  /**
228   * 
229   * @param grantedAuthority
230   * @param userIdentity
231   * @return true if grantedAuthority is not null, userIdentity was valid and the given authority was present in the given user object
232   */
233  public static boolean hasAuthority(GrantedAuthority grantedAuthority, UserIdentity userIdentity){
234    if(grantedAuthority == null){
235      LOGGER.debug("Granted authority was null.");
236    }else if(!UserIdentity.isValid(userIdentity)){
237      LOGGER.debug("Invalid user id.");
238    }else{
239      return userIdentity.getAuthorities().contains(grantedAuthority);
240    }
241    return false;
242  }
243}