checkPermission method

bool checkPermission(
  1. String moduleId,
  2. Permission permission, {
  3. String? resource,
  4. bool logViolation = true,
})

Check if a module has permission for an action

Implementation

bool checkPermission(
  String moduleId,
  Permission permission, {
  String? resource,
  bool logViolation = true,
}) {
  // 1. Allow self-access (module accessing its own resources)
  // If the resource starts with "moduleId.", it's owned by the module.
  if (resource != null &&
      (moduleId == resource || resource.startsWith('$moduleId.'))) {
    return true;
  }

  // 2. Check registered permissions
  final permissions = _modulePermissions[moduleId];
  final allowed = permissions?.allows(permission, resource) ?? false;

  if (!allowed) {
    if (!_enabled) {
      if (logViolation) {
        AirLogger.warning(
          'Permission Bypass (DEBUG): Module "$moduleId" lacks ${permission.name} '
          'permission for ${resource ?? "any resource"}. Register it in the module.',
        );
      }
      return true; // Bypass when disabled (typically in debug)
    }

    if (logViolation) {
      _logViolation(
        moduleId,
        permission,
        resource,
        permissions == null
            ? 'No permissions registered'
            : 'Permission denied',
      );
    }
    return false;
  }

  return true;
}