PermissionManager.js

const bitwiseOperations = require("./utils/BitwiseOperations");
const PermissionConstants = require("./utils/PermissionConstants");

class PermissionManager {
  /**
   * Creates an instance of the PermissionManager with the given permissions.
   * @param {number} currentPermissions - Bitmask representing the user's permissions.
   */
  constructor(currentPermissions) {
    this.currentPermissions = currentPermissions;
  }

  /**
   * Checks if the user has the 'search' permission.
   * @return {boolean} True if the user can search, false otherwise.
   */
  canSearch() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.SEARCH
    );
  }

  /**
   * Checks if the user has the 'read' permission.
   * @return {boolean} True if the user can read, false otherwise.
   */
  canRead() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.READ
    );
  }

  /**
   * Checks if the user has the 'create' permission.
   * @return {boolean} True if the user can create, false otherwise.
   */
  canCreate() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.CREATE
    );
  }

  /**
   * Checks if the user has the 'update' permission.
   * @return {boolean} True if the user can update, false otherwise.
   */
  canUpdate() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.UPDATE
    );
  }

  /**
   * Checks if the user has the 'remove' permission.
   * @return {boolean} True if the user can remove, false otherwise.
   */
  canRemove() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.REMOVE
    );
  }

  /**
   * Checks if the user has the 'operate' permission.
   * @return {boolean} True if the user can operate, false otherwise.
   */
  canOperate() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.OPERate
    );
  }

  /**
   * Checks if the user has the 'admin' permission.
   * @return {boolean} True if the user is an admin, false otherwise.
   */
  isAdmin() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.ADMIN
    );
  }

  /**
   * Checks if the user has the 'superadmin' permission.
   * @return {boolean} True if the user is a super admin, false otherwise.
   */
  isSuperAdmin() {
    return bitwiseOperations.hasPermission(
      this.currentPermissions,
      PermissionConstants.SUPERADMIN
    );
  }
}

module.exports = PermissionManager;