WebUser.java

/*
 * Copyright 2019 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.gringlobal.model;

import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.auditlog.annotations.HideAuditValue;
import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.compatibility.LookupDisplay;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Auto-generated by:
 * org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 */
@Entity
@Audited
@Cacheable
@Table(name = "web_user")
@DiscriminatorValue(value = "3")
@Getter
@Setter
public class WebUser extends AclSid implements LazyLoading<WebUser>, UserDetails {

	private static final long serialVersionUID = 2951338763001537328L;

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_enabled", nullable = false, length = 1)
	private String isEnabled = "N";

	@Basic
	@Column(name = "last_login_date")
	private Date lastLoginDate;

	@Basic
	@HideAuditValue
	@Column(nullable = false)
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	private String password;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "sys_lang_id", nullable = false)
	private SysLang sysLang;

	@Basic
	@NotNull
	@Size(min = 1, max = 50)
	@Column(name = "user_name", nullable = false, updatable = false, length = 50)
	@LookupDisplay
	private String username;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "web_cooperator_id")
	@JsonIgnoreProperties(value = { "createdBy", "modifiedBy", "ownedBy", "webUser" })
	@Field(type = FieldType.Object)
	private WebCooperator webCooperator;

	@Transient
	@JsonIgnore
	private List<GrantedAuthority> runtimeAuthorities;

	/**
	 * Update SID. We're using the "webuser:" prefix to avoid conflicts with SysUser.
	 */
	@PrePersist
	@PreUpdate
	private void prePersist() {
		setSid("webuser:" + this.username);
	}

	public WebUser() {
		setPrincipal(true);
	}

	public WebUser(final Long id) {
		setId(id);
	}

	@Override
	@Transient
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// runtimeAuthorities contain the final set!
		if (CollectionUtils.isNotEmpty(runtimeAuthorities)) {
			return runtimeAuthorities;
		}
		throw new RuntimeException("BUG: runtimeAuthorities are not set!");
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return this.isEnabled.equals("Y");
	}

	// Keeping JSON compatibility
	public Instant getModifiedDate() {
		return getLastModifiedDate();
	}

	@Override
	public void lazyLoad() {
		if (this.sysLang != null) {
			this.sysLang.getId();
		}
		if (this.webCooperator != null) {
			this.webCooperator.getId();
		}
	}

	@Override
	public String toString() {
		return this.getUsername() + " (WebUser)";
	}
}