class UserServicePermissionsFilter extends CheMethodInvokerFilter {

  @Override
  protected void filter(GenericResourceMethod genericResourceMethod, Object[] arguments)
      throws ApiException {
    final String methodName = genericResourceMethod.getMethod().getName();
    final Subject subject = EnvironmentContext.getCurrent().getSubject();
    switch (methodName) {
      case "getCurrent":
      case "updatePassword":
      case "getById":
      case "find":
      case "getSettings":
        // public methods
        return;
      case "create":
        final String token = (String) arguments[1];
        if (token != null) {
          // it is available to create user from token without permissions
          if (!userSelfCreationAllowed
              && !subject.hasPermission(SystemDomain.DOMAIN_ID, null, MANAGE_USERS_ACTION)) {
            throw new ForbiddenException(
                "Currently only admins can create accounts. Please contact our Admin Team for further info.");
          }

          return;
        }

        subject.checkPermission(SystemDomain.DOMAIN_ID, null, MANAGE_USERS_ACTION);
        break;
      case "remove":
        final String userToRemove = (String) arguments[0];
        if (subject.getUserId().equals(userToRemove)) {
          // everybody should be able to remove himself
          return;
        }
        subject.checkPermission(SystemDomain.DOMAIN_ID, null, MANAGE_USERS_ACTION);
        break;
      default:
        // unknown method
        throw new ForbiddenException("User is not authorized to perform this operation");
    }
  }

  @Inject public  UserServicePermissionsFilter(
      @Named(USER_SELF_CREATION_ALLOWED) boolean userSelfCreationAllowed);

}