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 org.apache.commons.lang3.StringUtils;
019
020import org.ametys.core.user.directory.UserDirectory;
021
022/**
023 * Implementation of the principal abstraction to represent an user with a login
024 * and eventually a fullname and an email.
025 */
026public class User implements java.security.Principal
027{
028    /**
029     * The identity of this principal.
030     */
031    protected UserIdentity _identity;
032
033    /**
034     * The last name of this principal.
035     */
036    protected String _lastName;
037    
038    /**
039     * The first name of this principal.
040     */
041    protected String _firstName;
042
043    /**
044     * The email of this principal.
045     */
046    protected String _email;
047
048    /**
049     * The user directory this user belongs to.
050     */
051    protected UserDirectory _userDirectory;
052
053    /**
054     * Construct a new UserPrincipal, associated with the specified login et population id.
055     * 
056     * @param login The login of the principal.
057     * @param populationId The id of the population
058     */
059    public User(String login, String populationId)
060    {
061        this(new UserIdentity(login, populationId));
062    }
063    
064    /**
065     * Construct a new UserPrincipal, associated with the specified identity.
066     * @param identity The identity of this user. Cannot be null
067     */
068    public User(UserIdentity identity)
069    {
070        this(identity, null, null, null, null);
071    }
072
073    /**
074     * Construct a new UserPrincipal, associated with a last name, a first name,
075     * an email and identified by a login.
076     * @param identity The identity of this user. Cannot be null
077     * @param lastName The last name
078     * @param firstName The first name 
079     * @param email The email
080     * @param userDirectory The user directory the use rbelongs to. Can be null.
081     */
082    public User(UserIdentity identity, String lastName, String firstName, String email, UserDirectory userDirectory)
083    {
084        _identity = identity;
085        _lastName = StringUtils.defaultString(lastName);
086        _firstName = StringUtils.defaultString(firstName);
087        _email = StringUtils.defaultString(email);
088        _userDirectory = userDirectory;
089    }
090
091    /**
092     * The identity of the user.
093     * 
094     * @return The identity. 
095     */
096    public UserIdentity getIdentity()
097    {
098        return _identity;
099    }
100    
101    @Override
102    public String getName()
103    {
104        return UserIdentity.userIdentityToString(_identity);
105    }
106    
107    /**
108     * The last name of the user
109     * @return The last name.
110     */
111    public String getLastName()
112    {
113        return _lastName;
114    }
115    
116    /**
117     * The first name of the user
118     * @return The first name.
119     */
120    public String getFirstName()
121    {
122        return _firstName;
123    }
124    
125    /**
126     * The email of the user represented by this Principal.
127     * 
128     * @return The email.
129     */
130    public String getEmail()
131    {
132        return _email;
133    }
134    
135    /**
136     * The fullname of this user.
137     * @return The full name
138     */
139    public String getFullName()
140    {
141        return _getFullName(true);
142    }
143    
144    /**
145     * The fullname to use to display if sort is needed.
146     * Ensure the sort will be on 
147     * @return The sortable name
148     */
149    public String getSortableName()
150    {
151        return _getFullName(false);
152    }
153    
154    /**
155     * The user directory this user belongs to.
156     * @return The user directory
157     */
158    public UserDirectory getUserDirectory()
159    {
160        return _userDirectory;
161    }
162    
163    /**
164     * The full name of the user represented by this Principal.
165     * @param firstLast Define the name order if the full name. If true, first name then last name. If false, the contrary.
166     * @return The full name.
167     */
168    protected String _getFullName(boolean firstLast)
169    {
170        StringBuilder sb = new StringBuilder();
171        
172        if (firstLast && StringUtils.isNotEmpty(_firstName))
173        {
174            sb.append(_firstName);
175            
176            if (StringUtils.isNotEmpty(_lastName))
177            {
178                sb.append(' ');
179            }
180        }
181        
182        if (StringUtils.isNotEmpty(_lastName))
183        {
184            sb.append(_lastName);
185        }
186        
187        if (!firstLast && StringUtils.isNotEmpty(_firstName))
188        {
189            if (StringUtils.isNotEmpty(_lastName))
190            {
191                sb.append(' ');
192            }
193            
194            sb.append(_firstName);
195        }
196        
197        return StringUtils.defaultIfEmpty(sb.toString(), _identity.getLogin());
198    }
199    
200    /**
201     * Return a String representation of this object, which exposes only
202     * information that should be public.
203     * 
204     * @return A string representing the user.
205     */
206    @Override
207    public String toString()
208    {
209        StringBuilder sb = new StringBuilder("Principal[");
210        sb.append(_identity.toString());
211        sb.append(" : ");
212        sb.append(getFullName());
213        sb.append(", ");
214        sb.append(_email);
215        sb.append("]");
216        return sb.toString();
217    }
218
219    /**
220     * Test if two principal are equals.
221     * @return true if the given Object represents the same Principal.
222     */
223    @Override
224    public boolean equals(Object another)
225    {
226        if (another == null || !(another instanceof User))
227        {
228            return false;
229        }
230        
231        User otherUser = (User) another;
232        
233        return _identity != null  && _identity.equals(otherUser.getIdentity());
234    }
235    
236    @Override
237    public int hashCode()
238    {
239        return _identity.hashCode();
240    }
241}