001package org.apache.archiva.redback.rest.api.model.v2;
002
003import io.swagger.v3.oas.annotations.media.Schema;
004
005import javax.xml.bind.annotation.XmlRootElement;
006import java.io.Serializable;
007import java.time.Instant;
008import java.time.OffsetDateTime;
009import java.time.ZoneId;
010
011/*
012 * Licensed to the Apache Software Foundation (ASF) under one
013 * or more contributor license agreements.  See the NOTICE file
014 * distributed with this work for additional information
015 * regarding copyright ownership.  The ASF licenses this file
016 * to you under the Apache License, Version 2.0 (the
017 * "License"); you may not use this file except in compliance
018 * with the License.  You may obtain a copy of the License at
019 *
020 * http://www.apache.org/licenses/LICENSE-2.0
021 *
022 * Unless required by applicable law or agreed to in writing,
023 * software distributed under the License is distributed on an
024 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
025 * KIND, either express or implied.  See the License for the
026 * specific language governing permissions and limitations
027 * under the License.
028 */
029
030@XmlRootElement( name = "user" )
031@Schema(name="User", description = "User information data")
032public class UserInfo extends BaseUserInfo
033    implements Serializable
034{
035
036    private static final long serialVersionUID = 822423853981984867L;
037
038    private String fullName;
039
040    private String email;
041
042    private boolean validated;
043
044    private boolean locked;
045
046    private boolean passwordChangeRequired;
047
048    private boolean permanent;
049
050    // Display Only Fields.
051    private OffsetDateTime timestampAccountCreation;
052
053    private OffsetDateTime timestampLastLogin;
054
055    private OffsetDateTime timestampLastPasswordChange;
056
057    /**
058     * with some userManagerImpl it's not possible to edit users;
059     * @since 2.1
060     */
061    private boolean readOnly;
062
063    /**
064     * as we can user multiple userManagers implementation we must track from which one this one comes.
065     * @since 2.1
066     */
067    private String userManagerId;
068
069    /**
070     * for request validation
071     *
072     * @since 2.2
073     */
074    private String validationToken;
075
076
077    public UserInfo()
078    {
079        // no op
080    }
081
082    public UserInfo( String userId, String fullName, String email, boolean validated, boolean locked )
083    {
084        this.userId = userId;
085        this.fullName = fullName;
086        this.email = email;
087        this.validated = validated;
088        this.locked = locked;
089    }
090
091    public UserInfo( org.apache.archiva.redback.users.User user )
092    {
093        setUserId( user.getUsername() );
094        this.setEmail( user.getEmail() );
095        this.setFullName( user.getFullName() );
096        this.setLocked( user.isLocked() );
097        this.setValidated( user.isValidated() );
098        this.setPasswordChangeRequired( user.isPasswordChangeRequired() );
099        this.setPermanent( user.isPermanent() );
100        this.setUserManagerId( user.getUserManagerId() );
101        this.setId( user.getId() );
102
103        if (user.getAccountCreationDate()==null) {
104            setTimestampAccountCreationByInstant( Instant.EPOCH );
105        } else {
106            setTimestampAccountCreationByInstant( user.getAccountCreationDate().toInstant() );
107        }
108        if (user.getLastLoginDate()==null) {
109            setTimestampLastLoginByInstant( Instant.EPOCH );
110        } else
111        {
112            setTimestampLastLoginByInstant( user.getLastLoginDate( ).toInstant( ) );
113        }
114        if (user.getLastPasswordChange()==null) {
115            setTimestampLastLoginByInstant( Instant.EPOCH );
116        } else
117        {
118            setTimestampLastPasswordChangeByInstant( user.getLastPasswordChange( ).toInstant( ) );
119        }
120    }
121
122
123    @Schema( name="full_name", description = "The full name of the user" )
124    public String getFullName( )
125    {
126        return fullName;
127    }
128
129    public void setFullName( String fullName )
130    {
131        this.fullName = fullName;
132    }
133
134    @Schema( description = "Email address" )
135    public String getEmail( )
136    {
137        return email;
138    }
139
140    public void setEmail( String email )
141    {
142        this.email = email;
143    }
144
145    @Schema( description = "True, if user is validated, or False, if user is still in register phase.")
146    public boolean isValidated()
147    {
148        return validated;
149    }
150
151    public void setValidated( boolean validated )
152    {
153        this.validated = validated;
154    }
155
156    @Schema(description = "True, if user is locked.")
157    public boolean isLocked()
158    {
159        return locked;
160    }
161
162    public void setLocked( boolean isLocked )
163    {
164        this.locked = isLocked;
165    }
166
167    @Schema( name="password_change_required", description = "True, if user has to change his password" )
168    public boolean isPasswordChangeRequired( )
169    {
170        return passwordChangeRequired;
171    }
172
173    public void setPasswordChangeRequired( boolean passwordChangeRequired )
174    {
175        this.passwordChangeRequired = passwordChangeRequired;
176    }
177
178    @Schema(description = "True, if this is not a temporary user.")
179    public boolean isPermanent()
180    {
181        return permanent;
182    }
183
184    public void setPermanent( boolean permanent )
185    {
186        this.permanent = permanent;
187    }
188
189    @Schema(name="timestamp_account_creation", description = "The date and time, when the account was first created.")
190    public OffsetDateTime getTimestampAccountCreation()
191    {
192        return timestampAccountCreation;
193    }
194
195    public void setTimestampAccountCreation( OffsetDateTime timestampAccountCreation )
196    {
197        this.timestampAccountCreation = timestampAccountCreation;
198    }
199
200    public void setTimestampAccountCreationByInstant( Instant timestampAccountCreation )
201    {
202        this.timestampAccountCreation = OffsetDateTime.ofInstant( timestampAccountCreation, ZoneId.systemDefault() );
203    }
204
205    @Schema(name="timestamp_last_login", description = "Date and time of the last successful login")
206    public OffsetDateTime getTimestampLastLogin()
207    {
208        return timestampLastLogin;
209    }
210
211    public void setTimestampLastLogin( OffsetDateTime timestampLastLogin )
212    {
213        this.timestampLastLogin = timestampLastLogin;
214    }
215
216    public void setTimestampLastLoginByInstant( Instant timestampLastLogin )
217    {
218        this.timestampLastLogin = OffsetDateTime.ofInstant( timestampLastLogin, ZoneId.systemDefault( ) );
219    }
220
221    @Schema(name="timestamp_last_password_change", description = "Date and time of the last password change")
222    public OffsetDateTime getTimestampLastPasswordChange()
223    {
224        return timestampLastPasswordChange;
225    }
226
227    public void setTimestampLastPasswordChange( OffsetDateTime timestampLastPasswordChange )
228    {
229        this.timestampLastPasswordChange = timestampLastPasswordChange;
230    }
231
232    public void setTimestampLastPasswordChangeByInstant( Instant timestampLastPasswordChange )
233    {
234        this.timestampLastPasswordChange = OffsetDateTime.ofInstant( timestampLastPasswordChange, ZoneId.systemDefault() );
235    }
236
237    @Schema(name="read_only", description = "True, if this is user has readonly access")
238    public boolean isReadOnly()
239    {
240        return readOnly;
241    }
242
243    public void setReadOnly( boolean readOnly )
244    {
245        this.readOnly = readOnly;
246    }
247
248    @Schema( name="user_manager_id", description = "Id of the usermanager, where this user is registered")
249    public String getUserManagerId()
250    {
251        return userManagerId;
252    }
253
254    public void setUserManagerId( String userManagerId )
255    {
256        this.userManagerId = userManagerId;
257    }
258
259    @Schema( name="validation_token", description = "Current validation token of this user")
260    public String getValidationToken() {
261        return validationToken;
262    }
263
264    public void setValidationToken(String validationToken) {
265        this.validationToken = validationToken;
266    }
267
268    @Override
269    public String toString()
270    {
271        return "User{" +
272            "username='" + getUserId( ) + '\'' +
273            ", fullName='" + fullName + '\'' +
274            ", email='" + email + '\'' +
275            ", validated=" + validated +
276            ", locked=" + locked +
277            //", password='" + password + '\'' +
278            ", passwordChangeRequired=" + passwordChangeRequired +
279            ", permanent=" + permanent +
280            ", timestampAccountCreation='" + timestampAccountCreation + '\'' +
281            ", timestampLastLogin='" + timestampLastLogin + '\'' +
282            ", timestampLastPasswordChange='" + timestampLastPasswordChange + '\'' +
283            ", readOnly=" + readOnly +
284            ", userManagerId='" + userManagerId + '\'' +
285            ", validationToken='" + validationToken + '\'' +
286            '}';
287    }
288
289    @Override
290    public boolean equals( Object o )
291    {
292        if ( this == o )
293        {
294            return true;
295        }
296        if ( !( o instanceof UserInfo ) )
297        {
298            return false;
299        }
300
301        UserInfo user = (UserInfo) o;
302
303        if ( !getUserId( ).equals( user.getUserId( ) ) )
304        {
305            return false;
306        }
307
308        return true;
309    }
310
311    @Override
312    public int hashCode()
313    {
314        return getUserId( ).hashCode();
315    }
316}