PermissionType.java

  1. package org.itracker.model;

  2. /**
  3.  * Enumeration for permission types.
  4.  *
  5.  * @author johnny
  6.  */
  7. public enum PermissionType implements IntCodeEnum<PermissionType> {

  8.     /**
  9.      * User Admin Permission.
  10.      * Currently this is equivalent to super user, since the permission can't be granted,
  11.      * and is only available to an admin.
  12.      */
  13.     USER_ADMIN(-1),

  14.     /**
  15.      * Product Admin Permission
  16.      */
  17.     PRODUCT_ADMIN(1),

  18.     /**
  19.      * Issue Create Permission
  20.      */
  21.     ISSUE_CREATE(2),

  22.     /**
  23.      * Issue Edit Permission.
  24.      * Users with this permission can edit any issue in the project.
  25.      */
  26.     ISSUE_EDIT_ALL(3),

  27.     /**
  28.      * Issue Close Permission.
  29.      * Users with this permission can close issues in the project.
  30.      */
  31.     ISSUE_CLOSE(4),

  32.     /**
  33.      * Issue Assign to Self Permission.
  34.      * Users with this permission can assign issues to themselves.
  35.      */
  36.     ISSUE_ASSIGN_SELF(5),

  37.     /**
  38.      * Issue Assign to Others Permissions.
  39.      * Users with this permission can assign issues to anyone,
  40.      * given than those users have the ability to recieve the assignment.
  41.      */
  42.     ISSUE_ASSIGN_OTHERS(6),

  43.     /**
  44.      * View All Issues Permission.  Users can view all issues in the project.
  45.      */
  46.     ISSUE_VIEW_ALL(7),

  47.     /**
  48.      * View Users Issues Permission.  Users can view thier own issues.
  49.      * This includes ones they are the creator or owner of.
  50.      */
  51.     ISSUE_VIEW_USERS(8),

  52.     /**
  53.      * Edit Users Issues Permission.
  54.      * Users with this permission can edit any issue they created or own.
  55.      * They are limited to editing the description, adding history entries,
  56.      * and adding attachments.
  57.      */
  58.     ISSUE_EDIT_USERS(9),

  59.     /**
  60.      * Issue Unassign Self Permission.
  61.      * Users with this permission can unassign issues they own.
  62.      */
  63.     ISSUE_UNASSIGN_SELF(10),

  64.     /**
  65.      * Issue Assignable.
  66.      * Users with this permission can be assigned any issue in the system.
  67.      * To determine if a user can be assigned an issue,
  68.      * it will be a combination of users with EDIT_ALL,
  69.      * users with EDIT_USERS if they are the creator,
  70.      * and users with this permission and EDIT_USERS.
  71.      */
  72.     ISSUE_ASSIGNABLE(11),

  73.     /**
  74.      * Create for Others.
  75.      * Users with this permission are allowed to create issues on behalf of other users.
  76.      * The system will treat the issue as if the other user had created it.
  77.      * The actual creator will be logged in the audit log.
  78.      */
  79.     ISSUE_CREATE_OTHERS(12),

  80.     /**
  81.      * Full edit permission.
  82.      * This defines what levelof editing a user has for an issue.
  83.      * Without this permission, users will
  84.      * be limited to editing only the description, attachments, custom fields,
  85.      * and history of an issue.
  86.      */
  87.     ISSUE_EDIT_FULL(13);

  88.     /* The project value matches the enum order (except for USER_ADMIN which is at position 0) */
  89.     private static final PermissionType[] PERMISSION_TYPES = values();

  90.     /**
  91.      * The integer value of this enum member.
  92.      */
  93.     private final int code;

  94.     /**
  95.      * Creates a new instance of this enum.
  96.      *
  97.      * @param code unique value representing this instance
  98.      */
  99.     PermissionType(Integer code) {
  100.         this.code = code;
  101.     }

  102.     /**
  103.      * Returns the integer value representing this enum member.
  104.      *
  105.      * @return unique value representing this instance
  106.      */
  107.     public Integer getCode() {
  108.         return code;
  109.     }

  110.     public static PermissionType valueOf(Integer type) {
  111.         return values()[0].fromCode(type);
  112.     }

  113.     @Deprecated
  114.     public static PermissionType[] valueOf(int[] type) {
  115.         if (null == type) {
  116.             return null;
  117.         }
  118.         final PermissionType[] result = new PermissionType[type.length];
  119.         int c = 0;
  120.         for (Integer i: type) {
  121.             result[c++] = valueOf(i);
  122.         }
  123.         return result;
  124.     }


  125.     /**
  126.      * Returns the enum instance matching the integer value.
  127.      *
  128.      * @param code unique value of the enum instance to return
  129.      * @return enum instance matching the int value
  130.      */
  131.     public PermissionType fromCode(Integer code) {
  132.         if (code == 0 || code < -1 || code > 13) {
  133.             throw new IllegalArgumentException("Unknown PermissionType code " + code);
  134.         }
  135.         if (code == -1) {
  136.             return PERMISSION_TYPES[0];
  137.         }
  138.         return PERMISSION_TYPES[code];
  139.     }

  140.     public String name(Project project) {
  141.         if (null != project) {
  142.             if (project.isNew()) {
  143.                 throw new IllegalStateException("New project can't be granted.");
  144.             }
  145.             return name() + "#" + project.getId();
  146.         }
  147.         return name();
  148.     }

  149. }