SysUser.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.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.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.auditlog.annotations.HideAuditValue;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.custom.validation.javax.OneLine;
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;
import com.fasterxml.jackson.annotation.JsonView;

/**
 * The user in GRIN-Global. This implementation includes {@link UserDetails},
 * making this entity compatible with Spring Security.
 *
 * @author org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 * @author mobreza
 */
@Entity
@Audited
@Cacheable
@Table(name = "sys_user")
@DiscriminatorValue(value = "1")
@Getter
@Setter
public class SysUser extends AclSid implements UserDetails {
	private static final long serialVersionUID = 1L;
	public static final String PROVIDER_LOCAL = "local";

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "cooperator_id")
	private Cooperator cooperator;

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

	@Basic
	@NotBlank
	@HideAuditValue
	@Size(max = 2000)
	@Column(nullable = false, length = 2000)
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	private String password;

	@Basic
	@OneLine
	@NotBlank
	@Size(max = 50)
	@Column(name = "user_name", nullable = false, length = 50)
	@JsonProperty(access = JsonProperty.Access.READ_ONLY)
	@LookupDisplay
	private String username;

	/** The account type. */
	@JsonView(JsonViews.Protected.class)
	@Column(length = 20)
	private String provider = PROVIDER_LOCAL;

	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "sysUser")
	@JsonIgnoreProperties({ "sysUser" })
	private List<SysGroupUserMap> groupMaps;

	@Transient
	@JsonIgnore
	private List<GrantedAuthority> runtimeAuthorities;

	/**
	 * Populate sysUserId
	 */
	@PrePersist
	@PreUpdate
	private void prePersist() {
		setSid(this.username);
	}

	public SysUser() {
		setPrincipal(true);
	}

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

	public Cooperator getCooperator() {
		return cooperator;
	}

	public void setCooperator(final Cooperator cooperator) {
		this.cooperator = cooperator;
	}

	public String getIsEnabled() {
		return isEnabled;
	}

	public void setIsEnabled(final String isEnabled) {
		this.isEnabled = isEnabled;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	@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 String getUsername() {
		return this.username;
	}

	public void setUsername(final String username) {
		this.username = username;
	}

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

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

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

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

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public List<GrantedAuthority> getRuntimeAuthorities() {
		return runtimeAuthorities;
	}

	public void setRuntimeAuthorities(List<GrantedAuthority> runtimeAuthorities) {
		this.runtimeAuthorities = ListUtils.unmodifiableList(runtimeAuthorities);
	}

	public List<SysGroupUserMap> getGroupMaps() {
		return groupMaps;
	}

	public void setGroupMaps(List<SysGroupUserMap> groupMaps) {
		this.groupMaps = groupMaps;
	}

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

	@Override
	public String toString() {
		return getUsername();
	}
}