surrealdb_sql/statements/remove/
token.rs

1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::{Base, Ident, Value};
6use derive::Store;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display, Formatter};
10
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
12#[revisioned(revision = 1)]
13pub struct RemoveTokenStatement {
14	pub name: Ident,
15	pub base: Base,
16}
17
18impl RemoveTokenStatement {
19	/// Process this type returning a computed simple Value
20	pub(crate) async fn compute(
21		&self,
22		_ctx: &Context<'_>,
23		opt: &Options,
24		txn: &Transaction,
25	) -> Result<Value, Error> {
26		// Allowed to run?
27		opt.is_allowed(Action::Edit, ResourceKind::Actor, &self.base)?;
28
29		match &self.base {
30			Base::Ns => {
31				// Claim transaction
32				let mut run = txn.lock().await;
33				// Clear the cache
34				run.clear_cache();
35				// Delete the definition
36				let key = crate::key::namespace::tk::new(opt.ns(), &self.name);
37				run.del(key).await?;
38				// Ok all good
39				Ok(Value::None)
40			}
41			Base::Db => {
42				// Claim transaction
43				let mut run = txn.lock().await;
44				// Clear the cache
45				run.clear_cache();
46				// Delete the definition
47				let key = crate::key::database::tk::new(opt.ns(), opt.db(), &self.name);
48				run.del(key).await?;
49				// Ok all good
50				Ok(Value::None)
51			}
52			Base::Sc(sc) => {
53				// Claim transaction
54				let mut run = txn.lock().await;
55				// Clear the cache
56				run.clear_cache();
57				// Delete the definition
58				let key = crate::key::scope::tk::new(opt.ns(), opt.db(), sc, &self.name);
59				run.del(key).await?;
60				// Ok all good
61				Ok(Value::None)
62			}
63			_ => Err(Error::InvalidLevel(self.base.to_string())),
64		}
65	}
66}
67
68impl Display for RemoveTokenStatement {
69	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
70		write!(f, "REMOVE TOKEN {} ON {}", self.name, self.base)
71	}
72}