// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use log::debug;
use pib_service_api_permissions::{Method, Resource};
use pib_service_core_types::UserId;

use crate::{Backend, Error, Result};

impl Backend {
    pub(crate) async fn enforce_permissions(
        &self,
        resource: Resource,
        method: Method,
        user_id: UserId,
    ) -> Result<()> {
        debug!(
            "Enforcing permissions to resource {resource:?} with method {method:?} for user {user_id}"
        );
        let admission = self
            .permissions
            .request_permission(resource, method, user_id)
            .await?;
        debug!("Permission is:{admission:?}");

        if admission.is_denied() {
            return Err(Error::Forbidden);
        }

        Ok(())
    }
}
