Skip to main content

stackless_core/state/
grants.rs

1//! Execution grants are written by the authenticated caller boundary.
2
3use super::{StateError, Store};
4
5impl Store {
6    pub fn host_execution_allowed(&self, owner_id: &str) -> Result<bool, StateError> {
7        Ok(self
8            .query_row(
9                "SELECT host_execution FROM execution_grants WHERE owner_id = ?1",
10                &[owner_id.into()],
11                |row| row.get_i64(0),
12            )?
13            .unwrap_or(0)
14            == 1)
15    }
16
17    pub fn grant_host_execution(&self, owner_id: &str) -> Result<(), StateError> {
18        let changed = self.execute(
19            "INSERT INTO execution_grants(owner_id, host_execution, granted_at)
20             SELECT ?1, 1, ?2 WHERE EXISTS(SELECT 1 FROM instances WHERE instance_id = ?1 AND status = 'active')
21             ON CONFLICT(owner_id) DO UPDATE SET host_execution = 1, granted_at = excluded.granted_at",
22            &[owner_id.into(), Self::now().into()],
23        )?;
24        if changed != 1 {
25            return Err(StateError::ResourceInvariant {
26                detail: "execution grant owner is not active".into(),
27            });
28        }
29        Ok(())
30    }
31}