backend/business-management/bin/tools/RoleValidator.js
/**
 * Checks if the user has the permissions needed, otherwise throws an error according to the passed parameters.
 *
 * @param {*} UserRoles Roles of the authenticated user
 * @param {*} name Context name
 * @param {*} method method name
 * @param {*} errorCode  This is the error code that will be thrown if the user do not have the required roles
 * @param {*} errorMessage This is the error message that will be used if the user do not have the required roles
 * @param {*} requiredRoles Array with required roles (The authenticated user must have at least one of the required roles,
 *  otherwise the operation that the user is trying to do will be rejected.
 */
Checks if the user has the permissions needed, otherwise throws an error according to the passed parameters.
Checks if the user has the permissions needed otherwise throws an error according to the passed parameters .
static checkPermissions$(
    userRoles,
    contextName,
    method,
    errorCode,
    errorMessage,
    requiredRoles
  ) {
    return Rx.Observable.from(requiredRoles)
      .map(requiredRole => {
        if (
          userRoles == undefined ||
          userRoles.length == 0 ||
          !userRoles.includes(requiredRole)
        ) {
          return false;
        }
        return true;
      })
      .toArray()
      .mergeMap(validRoles => {
        if (!validRoles.includes(true)) {
          return Rx.Observable.throw(
            new CustomError(contextName, method, errorCode, errorMessage)
          );
        } else {
          return Rx.Observable.of(validRoles);
        }
      });
  }
static checkPermissions$ ( userRoles , contextName , method , errorCode , errorMessage , requiredRoles ) { return Rx . Observable . from ( requiredRoles ) . map ( requiredRole => { if ( userRoles == undefined || userRoles . length == 0 || ! userRoles . includes ( requiredRole ) ) { return false ; } return true ; } ) . toArray ( ) . mergeMap ( validRoles => { if ( ! validRoles . includes ( true ) ) { return Rx . Observable . throw ( new CustomError ( contextName , method , errorCode , errorMessage ) ) ; } else { return Rx . Observable . of ( validRoles ) ; } } ) ; }
Checks if the user has the permissions needed, otherwise throws an error according to the passed parameters.
Checks if the user has the permissions needed otherwise throws an error according to the passed parameters .