surrealdb/iam/entities/resources/
actor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use revision::revisioned;
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::str::FromStr;

use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid, RestrictedExpression};
use serde::{Deserialize, Serialize};

use super::{Level, Resource, ResourceKind};
use crate::iam::Role;
use crate::sql::statements::{DefineTokenStatement, DefineUserStatement};

//
// User
//
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Hash, Serialize, Deserialize)]
#[revisioned(revision = 1)]
pub struct Actor {
	res: Resource,
	roles: Vec<Role>,
}

impl Default for Actor {
	fn default() -> Self {
		Self {
			res: ResourceKind::Actor.on_level(Level::No),
			roles: Vec::new(),
		}
	}
}

impl std::fmt::Display for Actor {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		if self.res.level() == &Level::No {
			return write!(f, "Actor::Anonymous");
		}

		write!(
			f,
			"{}{}::{}({})",
			self.res.level(),
			self.res.kind(),
			self.res.id(),
			self.roles.iter().map(|r| format!("{}", r)).collect::<Vec<String>>().join(", ")
		)
	}
}

impl Actor {
	pub fn new(id: String, roles: Vec<Role>, level: Level) -> Self {
		Self {
			res: Resource::new(id, super::ResourceKind::Actor, level),
			roles,
		}
	}

	/// Checks if the actor has the given role.
	pub fn has_role(&self, role: &Role) -> bool {
		self.roles.contains(role)
	}

	// Cedar policy helpers
	pub fn cedar_attrs(&self) -> HashMap<String, RestrictedExpression> {
		[
			("type", self.kind().into()),
			("level", self.level().into()),
			("roles", RestrictedExpression::new_set(self.roles.iter().map(|r| r.into()))),
		]
		.into_iter()
		.map(|(x, v)| (x.into(), v))
		.collect()
	}

	pub fn cedar_parents(&self) -> HashSet<EntityUid> {
		let mut parents = HashSet::with_capacity(1);
		parents.insert(self.res.level().into());
		parents
	}

	pub fn cedar_entities(&self) -> Vec<Entity> {
		let mut entities = Vec::new();

		entities.push(self.into());

		for role in self.roles.iter() {
			entities.push(role.into());
		}

		for level in self.res.level().cedar_entities() {
			entities.push(level);
		}

		entities
	}
}

impl Deref for Actor {
	type Target = Resource;
	fn deref(&self) -> &Self::Target {
		&self.res
	}
}

impl std::convert::From<&Actor> for EntityUid {
	fn from(actor: &Actor) -> Self {
		EntityUid::from_type_name_and_id(
			EntityTypeName::from_str("Actor").unwrap(),
			EntityId::from_str(actor.id()).unwrap(),
		)
	}
}

impl std::convert::From<&Actor> for Entity {
	fn from(actor: &Actor) -> Self {
		Entity::new(actor.into(), actor.cedar_attrs(), actor.cedar_parents())
	}
}

impl std::convert::From<(&DefineUserStatement, Level)> for Actor {
	fn from(val: (&DefineUserStatement, Level)) -> Self {
		Self::new(val.0.name.to_string(), val.0.roles.iter().map(Role::from).collect(), val.1)
	}
}

impl std::convert::From<(&DefineTokenStatement, Level)> for Actor {
	fn from(val: (&DefineTokenStatement, Level)) -> Self {
		Self::new(val.0.name.to_string(), Vec::default(), val.1)
	}
}