#![cfg(feature = "picodata")]
use std::ffi::CString;
use crate::error;
use crate::ffi::tarantool as ffi;
use crate::space::SpaceId;
#[repr(u16)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PrivType {
Read = 1,
Write = 2,
Execute = 4,
Session = 8,
Usage = 16,
Create = 32,
Drop = 64,
Alter = 128,
Reference = 256,
Trigger = 512,
Insert = 1024,
Update = 2048,
Delete = 4096,
Grant = 8192,
Revoke = 16384,
All = u16::MAX,
}
pub fn box_access_check_space(space_id: SpaceId, user_access: PrivType) -> crate::Result<()> {
let ret = unsafe { ffi::box_access_check_space(space_id, user_access as u16) };
if ret == -1 {
Err(error::Error::Tarantool(error::TarantoolError::last()))
} else {
Ok(())
}
}
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub enum SchemaObjectType {
#[default]
Unknown = 0,
Universe = 1,
Space = 2,
Function = 3,
User = 4,
Role = 5,
Sequence = 6,
Collation = 7,
ObjectTypeMax = 8,
EntitySpace = 9,
EntityFunction = 10,
EntityUser = 11,
EntityRole = 12,
EntitySequence = 13,
EntityCollation = 14,
EntityTypeMax = 15,
}
impl SchemaObjectType {
fn is_entity(&self) -> bool {
*self as u32 > SchemaObjectType::ObjectTypeMax as u32
}
}
pub fn box_access_check_ddl(
object_name: &str,
object_id: u32,
owner_id: u32,
object_type: SchemaObjectType,
access: PrivType,
) -> crate::Result<()> {
assert!(
!object_type.is_entity() || matches!(access, PrivType::Grant | PrivType::Revoke),
"Entity scoped permissons can be checked only with Grant or Revoke"
);
if matches!(access, PrivType::Grant | PrivType::Revoke) {
assert_eq!(
owner_id,
crate::session::uid().expect("there must be current user"),
"This is incorrect use of the API. For grant and revoke owner_id must be current user (grantor)."
)
}
let name = CString::new(object_name).expect("object name may not contain interior null bytes");
let ret = unsafe {
ffi::box_access_check_ddl(
name.as_ptr(),
object_id,
owner_id,
object_type as u32,
access as u16,
)
};
if ret == -1 {
Err(error::Error::Tarantool(error::TarantoolError::last()))
} else {
Ok(())
}
}