2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2022-04-05 20:41:49 +00:00
|
|
|
use crate::sql::permission::Permission;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn allow(
|
|
|
|
&self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-04-05 20:41:49 +00:00
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2022-05-13 20:46:56 +00:00
|
|
|
stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
2023-04-29 23:23:19 +00:00
|
|
|
// Check if this record exists
|
|
|
|
if self.id.is_some() {
|
|
|
|
// Should we run permissions checks?
|
2023-07-29 18:47:25 +00:00
|
|
|
if opt.check_perms(stm.into()) {
|
2023-04-29 23:23:19 +00:00
|
|
|
// Get the table
|
2023-07-06 14:57:42 +00:00
|
|
|
let tb = self.tb(opt, txn).await?;
|
2023-04-29 23:23:19 +00:00
|
|
|
// Get the permission clause
|
|
|
|
let perms = if stm.is_delete() {
|
|
|
|
&tb.permissions.delete
|
|
|
|
} else if stm.is_select() {
|
|
|
|
&tb.permissions.select
|
|
|
|
} else if self.is_new() {
|
|
|
|
&tb.permissions.create
|
|
|
|
} else {
|
|
|
|
&tb.permissions.update
|
|
|
|
};
|
|
|
|
// Process the table permissions
|
|
|
|
match perms {
|
|
|
|
Permission::None => return Err(Error::Ignore),
|
|
|
|
Permission::Full => return Ok(()),
|
|
|
|
Permission::Specific(e) => {
|
|
|
|
// Disable permissions
|
2023-07-05 21:26:13 +00:00
|
|
|
let opt = &opt.new_with_perms(false);
|
2023-04-29 23:23:19 +00:00
|
|
|
// Process the PERMISSION clause
|
2023-07-06 14:57:42 +00:00
|
|
|
if !e.compute(ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
2023-04-29 23:23:19 +00:00
|
|
|
return Err(Error::Ignore);
|
|
|
|
}
|
2022-04-05 20:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
2022-02-06 01:14:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|