Functional Weave
Code in TypeScript

auth.rbac@1.0.0

impl/rust/access_matrix.rs

2,518 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::auth_rbac_check_access::check_access;  ← checkAccess, another function of this group · built into the same file, even by a slim install
use super::auth_rbac_resolve_roles::{check_policy, parse_permission, rbac_policy_from_value};  ← resolveRoles, another function of this group · built into the same file, even by a slim install
use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

/// The expected answer for every role, every permission the app declares,
/// and both ownerships, in policy order: a test table generated from the
/// policy, so a change to a role shows up as changed expectations in review.
///
/// # Panics
/// Panics on a malformed policy.
pub fn access_matrix(policy: &RbacPolicy) -> Vec<AccessTestCase> {
    check_policy(policy);
    let mut cases = Vec::new();
    for role in &policy.roles {
        for permission in &policy.permissions {
            let (resource_type, action, _) = parse_permission(permission, false);
            for own in [false, true] {
                let subject = AccessSubject { id: "subject".to_string(), roles: vec![role.name.clone()] };
                let resource = AccessResource {
                    resource_type: resource_type.clone(),
                    id: "resource".to_string(),
                    owner_id: Some(if own { "subject" } else { "someone-else" }.to_string()),
                };
                let allowed = check_access(policy, &subject, &action, &resource).allowed;
                let verb = if allowed { "may" } else { "may not" };
                let name = if own {
                    format!("{} {} {} {} they own", role.name, verb, action, resource_type)
                } else {
                    format!("{} {} {} any {}", role.name, verb, action, resource_type)
                };
                cases.push(AccessTestCase {
                    name,
                    role: role.name.clone(),
                    resource_type: resource_type.clone(),
                    action: action.clone(),
                    own,
                    allowed,
                });
            }
        }
    }
    cases
}

pub fn fune_vector(args: &[Value]) -> Value {
    let policy = rbac_policy_from_value(&args[0]);
    Value::Arr(
        access_matrix(&policy)
            .iter()
            .map(|c| {
                Value::obj(vec![
                    ("name", Value::str(&c.name)),
                    ("role", Value::str(&c.role)),
                    ("resourceType", Value::str(&c.resource_type)),
                    ("action", Value::str(&c.action)),
                    ("own", Value::Bool(c.own)),
                    ("allowed", Value::Bool(c.allowed)),
                ])
            })
            .collect(),
    )
}