package info.bukova.isspst.security;

import info.bukova.isspst.Constants;
import info.bukova.isspst.data.RequirementBase;
import info.bukova.isspst.data.RequirementState;

import org.springframework.security.core.Authentication;

/**
 * Abstraktní třída pro vyhodnocení práv editace nových požadavků.
 * 
 * @author pepa
 *
 */
public abstract class AbstractRequirementEvaluator extends AbstractModuleEvaluator implements Evaluator {
	
	protected abstract Class<?> getServiceClass();
	
	@Override
	public boolean evaluate(Authentication authentication,
			Object targetDomainObject, String permission) {
		
		if (!hasModulePermission(authentication, getServiceClass(), permission)) {
			return false;
		}
		
		if (targetDomainObject instanceof RequirementBase) {
			RequirementBase req = (RequirementBase) targetDomainObject;

			if (permission.equals(Constants.PERM_EDIT_NEW)) {
				return req.getState() == RequirementState.NEW;
			}

			if (permission.equals(Constants.PERM_DELETE_NEW)) {
				return req.getState() == RequirementState.NEW;
			}
		}
		
		return true;
	}

}
