Exemple #1
0
    public static AllowedMethods fromValue(String allowedMethod) throws IllegalArgumentException {
      for (AllowedMethods method : AllowedMethods.values()) {
        String methodString = method.toString();
        if (methodString == null && allowedMethod == null) return method;
        if (methodString != null && methodString.equals(allowedMethod)) return method;
      }

      throw new IllegalArgumentException("Cannot create enum from " + allowedMethod + " value!");
    }
Exemple #2
0
  /**
   * Grants a new permission for a given subject and resource.
   *
   * @param subjectid the subject to give permissions to
   * @param resourceName the resource name/type
   * @param permission the set or HTTP methods allowed
   * @return true if successful
   */
  public boolean grantResourcePermission(
      String subjectid, String resourceName, EnumSet<AllowedMethods> permission) {
    // clean up resource name
    resourceName = Utils.noSpaces(resourceName, "-");

    if (!StringUtils.isBlank(subjectid)
        && !StringUtils.isBlank(resourceName)
        && permission != null
        && !permission.isEmpty()) {
      if (!getResourcePermissions().containsKey(subjectid)) {
        Map<String, List<String>> perm = new HashMap<String, List<String>>();
        perm.put(resourceName, new ArrayList<String>(permission.size()));
        for (AllowedMethods allowedMethod : permission) {
          perm.get(resourceName).add(allowedMethod.toString());
        }
        getResourcePermissions().put(subjectid, perm);
      } else {
        if (permission.containsAll(AllowedMethods.ALL_VALUES)
            || permission.contains(AllowedMethods.READ_WRITE)
            || (permission.contains(AllowedMethods.READ_ONLY)
                && permission.contains(AllowedMethods.WRITE_ONLY))
            || (permission.contains(AllowedMethods.GET)
                && permission.contains(AllowedMethods.WRITE_ONLY))) {
          permission = AllowedMethods.READ_AND_WRITE;
        } else {
          if (permission.contains(AllowedMethods.WRITE_ONLY)) {
            permission = AllowedMethods.WRITE;
          } else if (permission.contains(AllowedMethods.READ_ONLY)) {
            permission = AllowedMethods.READ;
          }
        }
        List<String> perm = new ArrayList<String>(permission.size());
        for (AllowedMethods allowedMethod : permission) {
          perm.add(allowedMethod.toString());
        }
        getResourcePermissions().get(subjectid).put(resourceName, perm);
      }
      return true;
    }
    return false;
  }