surrealdb_core/iam/entities/
action.rs

1use std::str::FromStr;
2
3use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
4
5use crate::dbs::Statement;
6
7// TODO(sgirones): For now keep it simple. In the future, we will allow for custom roles and policies using a more exhaustive list of actions and resources.
8#[derive(Clone, Debug, Eq, PartialEq, PartialOrd)]
9#[non_exhaustive]
10pub enum Action {
11	View,
12	Edit,
13}
14
15impl std::fmt::Display for Action {
16	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17		match self {
18			Action::View => write!(f, "View"),
19			Action::Edit => write!(f, "Edit"),
20		}
21	}
22}
23
24impl Action {
25	pub fn id(&self) -> String {
26		self.to_string()
27	}
28}
29
30impl std::convert::From<&Action> for EntityUid {
31	fn from(action: &Action) -> Self {
32		EntityUid::from_type_name_and_id(
33			EntityTypeName::from_str("Action").unwrap(),
34			EntityId::from_str(&action.id()).unwrap(),
35		)
36	}
37}
38
39impl std::convert::From<&Action> for Entity {
40	fn from(action: &Action) -> Self {
41		Entity::new(action.into(), Default::default(), Default::default())
42	}
43}
44
45impl From<&Statement<'_>> for Action {
46	fn from(stmt: &Statement) -> Self {
47		match stmt {
48			Statement::Live(_) => Action::View,
49			Statement::Select(_) => Action::View,
50			Statement::Show(_) => Action::View,
51			Statement::Create(_) => Action::Edit,
52			Statement::Upsert(_) => Action::Edit,
53			Statement::Update(_) => Action::Edit,
54			Statement::Relate(_) => Action::Edit,
55			Statement::Delete(_) => Action::Edit,
56			Statement::Insert(_) => Action::Edit,
57			Statement::Access(_) => Action::Edit,
58		}
59	}
60}