001/*
002 *  Copyright 2012 Anyware Services
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 org.ametys.core.user;
017
018import java.time.ZonedDateTime;
019
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.core.user.directory.UserDirectory;
023import org.ametys.core.util.SizeUtils.ExcludeFromSizeCalculation;
024
025/**
026 * Implementation of the principal abstraction to represent an user with a login
027 * and eventually a fullname and an email.
028 */
029public class User implements java.security.Principal
030{
031    /**
032     * The identity of this principal.
033     */
034    protected UserIdentity _identity;
035
036    /**
037     * The last name of this principal.
038     */
039    protected String _lastName;
040    
041    /**
042     * The first name of this principal.
043     */
044    protected String _firstName;
045
046    /**
047     * The email of this principal.
048     */
049    protected String _email;
050    
051    /**
052     * The creation date
053     */
054    protected ZonedDateTime _creationDate;
055    
056    /**
057     * The creation origin
058     */
059    protected UserCreationOrigin _creationOrigin;
060    
061    /**
062     * The user directory this user belongs to.
063     */
064    @ExcludeFromSizeCalculation
065    protected UserDirectory _userDirectory;
066
067    /**
068     * Enumeration for the user creation origin
069     *
070     */
071    public enum UserCreationOrigin 
072    {
073        /** User created by system */
074        SYSTEM,
075        /** User created by an administrator */
076        ADMIN,
077        /** User created by user signup */
078        USER_SIGNUP,
079        /** When user creation is unknown or not available */
080        NOT_AVAILABLE
081    }
082    
083    /**
084     * Construct a new UserPrincipal, associated with the specified login et population id.
085     * 
086     * @param login The login of the principal.
087     * @param populationId The id of the population
088     */
089    public User(String login, String populationId)
090    {
091        this(new UserIdentity(login, populationId));
092    }
093    
094    /**
095     * Construct a new UserPrincipal, associated with the specified identity.
096     * @param identity The identity of this user. Cannot be null
097     */
098    public User(UserIdentity identity)
099    {
100        this(identity, null, null, null, null);
101    }
102
103    /**
104     * Construct a new UserPrincipal, associated with a last name, a first name,
105     * an email and identified by a login.
106     * @param identity The identity of this user. Cannot be null
107     * @param lastName The last name
108     * @param firstName The first name 
109     * @param email The email
110     * @param userDirectory The user directory the use rbelongs to. Can be null.
111     */
112    public User(UserIdentity identity, String lastName, String firstName, String email, UserDirectory userDirectory)
113    {
114        this(identity, lastName, firstName, email, userDirectory, null, UserCreationOrigin.NOT_AVAILABLE);
115    }
116    
117    /**
118     * Construct a new UserPrincipal, associated with a last name, a first name,
119     * an email and identified by a login.
120     * @param identity The identity of this user. Cannot be null
121     * @param lastName The last name
122     * @param firstName The first name 
123     * @param email The email
124     * @param creationDate the creation date
125     * @param creationOrigin the creation origin
126     * @param userDirectory The user directory the use rbelongs to. Can be null.
127     */
128    public User(UserIdentity identity, String lastName, String firstName, String email, UserDirectory userDirectory, ZonedDateTime creationDate, UserCreationOrigin creationOrigin)
129    {
130        _identity = identity;
131        _lastName = StringUtils.defaultString(lastName);
132        _firstName = StringUtils.defaultString(firstName);
133        _email = StringUtils.defaultString(email);
134        _userDirectory = userDirectory;
135        _creationDate = creationDate;
136        _creationOrigin = creationOrigin;
137    }
138
139    /**
140     * The identity of the user.
141     * 
142     * @return The identity. 
143     */
144    public UserIdentity getIdentity()
145    {
146        return _identity;
147    }
148    
149    @Override
150    public String getName()
151    {
152        return UserIdentity.userIdentityToString(_identity);
153    }
154    
155    /**
156     * The last name of the user
157     * @return The last name.
158     */
159    public String getLastName()
160    {
161        return _lastName;
162    }
163    
164    /**
165     * The first name of the user
166     * @return The first name.
167     */
168    public String getFirstName()
169    {
170        return _firstName;
171    }
172    
173    /**
174     * The email of the user represented by this Principal.
175     * 
176     * @return The email.
177     */
178    public String getEmail()
179    {
180        return _email;
181    }
182    
183    /**
184     * The fullname of this user.
185     * @return The full name
186     */
187    public String getFullName()
188    {
189        return _getFullName(true);
190    }
191    
192    /**
193     * The fullname to use to display if sort is needed.
194     * Ensure the sort will be on 
195     * @return The sortable name
196     */
197    public String getSortableName()
198    {
199        return _getFullName(false);
200    }
201    
202    /**
203     * The user directory this user belongs to.
204     * @return The user directory
205     */
206    public UserDirectory getUserDirectory()
207    {
208        return _userDirectory;
209    }
210    
211    /**
212     * Get the user's creation date
213     * @return the creation date
214     */
215    public ZonedDateTime getCreationDate()
216    {
217        return _creationDate;
218    }
219    
220    /**
221     * Get the user's creation origin
222     * @return the creation origin
223     */
224    public UserCreationOrigin getCreationOrigin()
225    {
226        return _creationOrigin;
227    }
228    
229    /**
230     * The full name of the user represented by this Principal.
231     * @param firstLast Define the name order if the full name. If true, first name then last name. If false, the contrary.
232     * @return The full name.
233     */
234    protected String _getFullName(boolean firstLast)
235    {
236        StringBuilder sb = new StringBuilder();
237        
238        if (firstLast && StringUtils.isNotEmpty(_firstName))
239        {
240            sb.append(_firstName);
241            
242            if (StringUtils.isNotEmpty(_lastName))
243            {
244                sb.append(' ');
245            }
246        }
247        
248        if (StringUtils.isNotEmpty(_lastName))
249        {
250            sb.append(_lastName);
251        }
252        
253        if (!firstLast && StringUtils.isNotEmpty(_firstName))
254        {
255            if (StringUtils.isNotEmpty(_lastName))
256            {
257                sb.append(' ');
258            }
259            
260            sb.append(_firstName);
261        }
262        
263        return StringUtils.defaultIfEmpty(sb.toString(), _identity.getLogin());
264    }
265    
266    /**
267     * Return a String representation of this object, which exposes only
268     * information that should be public.
269     * 
270     * @return A string representing the user.
271     */
272    @Override
273    public String toString()
274    {
275        StringBuilder sb = new StringBuilder("Principal[");
276        sb.append(_identity.toString());
277        sb.append(" : ");
278        sb.append(getFullName());
279        sb.append(", ");
280        sb.append(_email);
281        sb.append("]");
282        return sb.toString();
283    }
284
285    /**
286     * Test if two principal are equals.
287     * @return true if the given Object represents the same Principal.
288     */
289    @Override
290    public boolean equals(Object another)
291    {
292        if (another == null || !(another instanceof User))
293        {
294            return false;
295        }
296        
297        User otherUser = (User) another;
298        
299        return _identity != null  && _identity.equals(otherUser.getIdentity());
300    }
301    
302    @Override
303    public int hashCode()
304    {
305        return _identity.hashCode();
306    }
307}