User.java

/**
 * Copyright 2014 Global Crop Diversity Trust
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package org.genesys.server.model.impl;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.security.model.BasicUser;
import org.genesys.server.model.UserRole;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.genesys.spring.validation.javax.SupportedLanguage;
import org.genesys.spring.validation.javax.ZoneIdValidation;

import java.time.Instant;

@Cacheable
@Entity
@Table(name = "\"user\"")
@DiscriminatorValue(value = "1")
@Getter
@Setter
public class User extends BasicUser<UserRole> {

	private static final long serialVersionUID = 4564013753931115445L;

	/** Custom password for access to file repository via FTP */
	@JsonIgnore
	@Column(length = 60)
	private String ftpPassword;

	/** User's preferred language */
	@NotNull
	@NotBlank
	@SupportedLanguage
	private String language = "en";

	/** User's timezone */
	@Size(max = 20)
	@Column(length = 20, nullable = false)
	@NotNull
	@ZoneIdValidation
	private String timezone = "UTC";

	/** User accepted terms of use of AI tools */
	@Column(nullable = false)
	private boolean aiOptin;

	/** When user changed their AI preference */
	@Column
	private Instant aiDate;

	/** User opted into receiving our newsletter */
	@Column(nullable = false)
	private boolean newsletterOptin;

	/** When user changed their Newsletter preference */
	@Column
	private Instant newsletterDate;

	@Override
	public String toString() {
		return "User id=" + getId() + " email=" + getEmail();
	}

	/**
	 * @deprecated use {@link #isAccountNonExpired()}
	 */
	@Deprecated
	public boolean isAccountExpired() {
		return !isAccountNonExpired();
	}

	public boolean hasRole(String roleName) {
		for (final UserRole userRole : getRoles()) {
			if (userRole.getName().equalsIgnoreCase(roleName)) {
				return true;
			}
		}
		return false;
	}

}