<?php

function hasPermission($permission_name) {
    if (isset($_SESSION['user']['permissions']) && in_array($permission_name, $_SESSION['user']['permissions'])) {
        return true;
    }
    return false;
}

function redirect_if_not_authenticated() {
    if (!isset($_SESSION['user'])) {
        header('Location: login.php');
        exit();
    }
}

function redirect_if_no_permission($permission_name) {
    if (!hasPermission($permission_name)) {
        // You can redirect to a specific error page or the index page with an error message.
        $_SESSION['flash_message'] = [
            'type' => 'danger',
            'message' => 'You do not have permission to access this page.'
        ];
        header('Location: index.php');
        exit();
    }
}
