SecuredAction.java

/*
 * Copyright 2021 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.community;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceException;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.blocks.util.EntityIdSerializer;
import org.gringlobal.model.LazyLoading;
import org.gringlobal.model.Site;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * GGCE Security Action is an ACL OID to which ACL entries are assigned.
 * 
 * @author Matija Obreza
 */
@Entity
@Table(name = "acl_ggce_action", uniqueConstraints = {
		@UniqueConstraint(name = "UQ_site_action", columnNames = { "site_site_id", "action" })
})
@Cacheable
@Getter
@Setter
@NoArgsConstructor
public class SecuredAction extends AuditedVersionedModel implements AclAwareModel, Copyable<SecuredAction>, LazyLoading<SecuredAction> {

	private static final long serialVersionUID = -4051910283980607046L;

	@JsonProperty(value = "siteId")
	@JsonSerialize(using = EntityIdSerializer.class)
	@ManyToOne(cascade = {}, optional = true)
	private Site site;

	@Column(name = "action")
	@Schema(implementation = SecurityAction.class)
	private String action;

	@JsonProperty(value = "parentActionId")
	@JsonSerialize(using = EntityIdSerializer.class)
	@ManyToOne(cascade = {}, optional = true)
	private SecuredAction parentAction;

	public SecuredAction(Long id) {
		super();
		setId(id);
	}

	public SecuredAction(String action, Site site) {
		this.action = action;
		this.site = site;
	}

	public SecuredAction(String action, Site site, SecuredAction parentAction) {
		this.action = action;
		this.site = site;
		this.parentAction = parentAction;
	}

	/**
	 * Cleanup
	 */
	@PrePersist
	@PreUpdate
	private void prePersist() {
		if (this.site == null) {
			// Parent action is not available if site is null
			this.parentAction = null;
		}
		try {
			SecurityAction.valueOf(this.action);
		} catch (IllegalArgumentException e) {
			throw new PersistenceException("Invalid value for secured action: ".concat(action), e);
		}
	}
	
	/**
	 * Secured actions inherit permissions from the same action, but with `site =
	 * null`.
	 *
	 * @return the parent action (for any site)
	 */
	@Override
	public AclAwareModel aclParentObject() {
		return parentAction;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof SecuredAction;
	}
}