surrealdb_core/iam/entities/
action.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
use std::str::FromStr;

use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};

use crate::dbs::Statement;

// 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.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum Action {
	View,
	Edit,
}

impl std::fmt::Display for Action {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Action::View => write!(f, "View"),
			Action::Edit => write!(f, "Edit"),
		}
	}
}

impl Action {
	pub fn id(&self) -> String {
		self.to_string()
	}
}

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

impl std::convert::From<&Action> for Entity {
	fn from(action: &Action) -> Self {
		Entity::new(action.into(), Default::default(), Default::default())
	}
}

impl From<&Statement<'_>> for Action {
	fn from(stmt: &Statement) -> Self {
		match stmt {
			Statement::Live(_) => Action::View,
			Statement::Select(_) => Action::View,
			Statement::Show(_) => Action::View,
			Statement::Create(_) => Action::Edit,
			Statement::Upsert(_) => Action::Edit,
			Statement::Update(_) => Action::Edit,
			Statement::Relate(_) => Action::Edit,
			Statement::Delete(_) => Action::Edit,
			Statement::Insert(_) => Action::Edit,
			Statement::Access(_) => Action::Edit,
		}
	}
}