
package de.ivu.elect.business.security.control;

import java.io.Serializable;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;

import de.ivu.elect.business.security.UseCasePermission;

/**
 * Für Hintergrundprozesse ohne SessionContext verwenden!
 */
@Stateless // ELECT-8117!!!
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class ElectCommonPrincipalOderSystem implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient ElectCommonPrincipal electCommonPrincipal;

  public ElectCommonPrincipalOderSystem() {
    super();
  }

  public boolean isSystem() {
    try {
      electCommonPrincipal.isAdmin(); // Exception, wenn kein Kontext...
    } catch (Exception expected) {
      return true;
    }

    return false;
  }

  public boolean hasUseCasePermission(UseCasePermission useCasePermission) {
    if (isSystem()) {
      return true; // System darf alles
    }

    return electCommonPrincipal.hasUseCasePermission(useCasePermission);
  }

  public boolean hatRolle(String rollenName) {
    if (isSystem()) {
      return true; // System darf alles
    }

    return electCommonPrincipal.hatRolle(rollenName);
  }
}
