EN · DE · RU · FR · ES

#657: AccessException.java

projectforge-business/src/main/java/org/projectforge/framework/access/AccessException.java Type: Java · Role: Component · Source: projectforge-business/src/main/java/org/projectforge/framework/access/AccessException.java 246 lines · 160 code · 52 comments · 34 blank
Source code file at projectforge-business/src/main/java/org/projectforge/framework/access/AccessException.java containing Java code for the Component layer.

Code Structure

Package: org.projectforge.framework.access

Classes: AccessException

Extends: UserException

Methods (11): getUser, getTaskId, getAccessType, setAccessType, getTaskNode, setClazz, getOperationType, setOperationType, setMessage, setUser, toString

Fields (7): serialVersionUID, taskTree, user, taskId, accessType, message, operationType

Imports: 10 packages

Source Code (abridged)

package org.projectforge.framework.access;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.projectforge.business.task.TaskDO;
import org.projectforge.business.task.TaskNode;
import org.projectforge.business.task.TaskTree;
import org.projectforge.common.i18n.MessageParam;
import org.projectforge.common.i18n.MessageParamType;
import org.projectforge.common.i18n.UserException;
import org.projectforge.framework.persistence.user.api.ThreadLocalUserContext;
import org.projectforge.framework.persistence.user.entities.PFUserDO;

import java.util.ResourceBundle;

/**
 * This class will be thrown by AccessChecker, if no access is given for the demanded action by an user.
 *
 * @author Kai Reinhard (k.reinhard@micromata.de)
 */
public class AccessException extends UserException {
  private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AccessException.class);

  private static final long serialVersionUID = 147795804616526958L;

  public final static String I18N_KEY_STANDARD = "access.exception.standard";

  public final static String I18N_KEY_STANDARD_WITH_TASK = "access.exception.standardWithTask";

  private TaskTree taskTree;

  protected PFUserDO user = null;

  protected Long taskId = null;

  protected AccessType accessType = null;

  protected String message = null;

  protected OperationType operationType = null;

  protected Class<?> clazz = null;

  public AccessException(final String i18nKey, final Object... params) {
    this(ThreadLocalUserContext.getLoggedInUser(), i18nKey, params);
  }

  public AccessException(final PFUserDO user, final String i18nKey, final Object... params) {
    super(i18nKey, params);
    this.user = user;
    log.info("AccessException: " + this);
  }

  public AccessException(final AccessType accessType, final OperationType operationType) {
    this(ThreadLocalUserContext.getLoggedInUser(), accessType, operationType);
  }

  public AccessException(final PFUserDO user, final AccessType accessType,
                         final OperationType operationType) {
    super(I18N_KEY_STANDARD, new MessageParam[]{new MessageParam(accessType), new MessageParam(operationType)});
    this.user = user;
    this.accessType = accessType;
    this.operationType = operationType;
    log.info("AccessException: " + this);
  }

  public AccessException(final Long taskId, final AccessType accessType,
                         final OperationType operationType) {
    this(ThreadLocalUserContext.getLoggedInUser(), taskId, accessType, operationType);
  }

  public AccessException(final PFUserDO user, final Long taskId, final AccessType accessType,
                         final OperationType operationType) {
    super(I18N_KEY_STANDARD_WITH_TASK,
        new MessageParam[]{new MessageParam(taskId), new MessageParam(accessType),
            new MessageParam(operationType)});
    this.user = user;
    this.taskId = taskId;
    this.accessType = accessType;
    this.operationType = operationType;
    log.info("AccessException: " + this);
  }

  /**
   * The order of the args is task id, accessType and operationType.
   *
   * @return the arguments for the message formatter from type Object[3].
   */
  public Object[] getMessageArgs(final ResourceBundle bundle) {
    final Object[] result = new Object[3];
    if (taskTree != null && this.taskId != null) {
      final TaskDO task = TaskTree.getInstance().getTaskById(taskId);
      if (task != null) {
        result[0] = task.getTitle();
      } else {
        result[0] = taskId;
      }
    } else {
      result[0] = taskId;
    }
    if (accessType != null) {
      result[1] = bundle.getString(accessType.getI18nKey());
    }
    if (operationType != null) {
      result[2] = bundle.getString(operationType.getI18nKey());
    }
    return result;
  }

  public MessageParam[] getMessageArgs() {
    final MessageParam[] result = new MessageParam[3];
    if (taskTree != null && this.taskId != null) {
      final TaskDO task = TaskTree.getInstance().getTaskById(taskId);
      if (task != null) {
        result[0] = new MessageParam(task.getTitle());
      } else {
        result[0] = new MessageParam(taskId);
      }
    } else {
      result[0] = new MessageParam(taskId);
// ... (truncated, total 224 lines)

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
67805f2fc ThreadLocalUserContext.user -> ThreadLocalUserContext.loggedInUser (renamed for avoiding mis-understandings in code).
4c04cfd65 MAJOR-CHANGE! Migration of integer id's to Long id's (including fk's etc.)
b6092df09 Copyright 2023 -> 2024