Skip to main content

execution_engine_core/
auth.rs

1// Copyright 2024 Vincents AI
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use serde::{Deserialize, Serialize};
5
6/// Simple user context for request-level identity and roles
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct UserContext {
9    /// Optional user id (None for anonymous)
10    pub user_id: Option<String>,
11    /// Roles assigned to the user (e.g. "admin")
12    pub roles: Vec<String>,
13    /// Fine-grained permissions
14    pub permissions: Vec<String>,
15}
16
17impl UserContext {
18    pub fn new(user_id: Option<String>, roles: Vec<String>, permissions: Vec<String>) -> Self {
19        Self {
20            user_id,
21            roles,
22            permissions,
23        }
24    }
25
26    pub fn is_admin(&self) -> bool {
27        self.roles.iter().any(|r| r == "admin")
28    }
29
30    pub fn has_role(&self, role: &str) -> bool {
31        self.roles.iter().any(|r| r == role)
32    }
33
34    pub fn has_permission(&self, perm: &str) -> bool {
35        self.permissions.iter().any(|p| p == perm)
36    }
37}