risingwave_common/acl/
mod.rsuse std::fmt::Formatter;
use std::sync::LazyLock;
use enumflags2::{bitflags, make_bitflags, BitFlags};
use parse_display::Display;
use risingwave_pb::user::grant_privilege::PbAction;
#[bitflags]
#[repr(u64)]
#[derive(Clone, Copy, Debug, Display, Eq, PartialEq)]
pub enum AclMode {
#[display("a")]
Insert = 1 << 0, #[display("r")]
Select = 1 << 1, #[display("w")]
Update = 1 << 2, #[display("d")]
Delete = 1 << 3,
#[display("D")]
Truncate = 1 << 4, #[display("x")]
References = 1 << 5,
#[display("t")]
Trigger = 1 << 6,
#[display("X")]
Execute = 1 << 7, #[display("U")]
Usage = 1 << 8, #[display("C")]
Create = 1 << 9, #[display("T")]
CreateTemp = 1 << 10, #[display("c")]
Connect = 1 << 11, #[display("s")]
Set = 1 << 12, #[display("A")]
AlterSystem = 1 << 13, #[display("m")]
Maintain = 1 << 14, }
impl From<PbAction> for AclMode {
fn from(action: PbAction) -> Self {
match action {
PbAction::Unspecified => unreachable!(),
PbAction::Select => AclMode::Select,
PbAction::Insert => AclMode::Insert,
PbAction::Update => AclMode::Update,
PbAction::Delete => AclMode::Delete,
PbAction::Create => AclMode::Create,
PbAction::Connect => AclMode::Connect,
PbAction::Usage => AclMode::Usage,
PbAction::Execute => AclMode::Execute,
}
}
}
impl From<AclMode> for PbAction {
fn from(val: AclMode) -> Self {
match val {
AclMode::Select => PbAction::Select,
AclMode::Insert => PbAction::Insert,
AclMode::Update => PbAction::Update,
AclMode::Delete => PbAction::Delete,
AclMode::Create => PbAction::Create,
AclMode::Connect => PbAction::Connect,
AclMode::Usage => PbAction::Usage,
AclMode::Execute => PbAction::Execute,
_ => unreachable!(),
}
}
}
#[derive(Clone, Debug)]
pub struct AclModeSet {
pub modes: BitFlags<AclMode>,
}
pub static ALL_AVAILABLE_DATABASE_MODES: LazyLock<AclModeSet> =
LazyLock::new(|| make_bitflags!(AclMode::{Create | Connect}).into());
pub static ALL_AVAILABLE_SCHEMA_MODES: LazyLock<AclModeSet> =
LazyLock::new(|| make_bitflags!(AclMode::{Create | Usage}).into());
pub static ALL_AVAILABLE_TABLE_MODES: LazyLock<AclModeSet> =
LazyLock::new(|| make_bitflags!(AclMode::{Select | Insert | Update | Delete}).into());
pub static ALL_AVAILABLE_SOURCE_MODES: LazyLock<AclModeSet> = LazyLock::new(AclModeSet::readonly);
pub static ALL_AVAILABLE_MVIEW_MODES: LazyLock<AclModeSet> = LazyLock::new(AclModeSet::readonly);
pub static ALL_AVAILABLE_SINK_MODES: LazyLock<AclModeSet> = LazyLock::new(AclModeSet::readonly);
pub static ALL_AVAILABLE_SUBSCRIPTION_MODES: LazyLock<AclModeSet> =
LazyLock::new(AclModeSet::empty);
pub static ALL_AVAILABLE_FUNCTION_MODES: LazyLock<AclModeSet> =
LazyLock::new(|| BitFlags::from(AclMode::Execute).into());
pub static ALL_AVAILABLE_CONNECTION_MODES: LazyLock<AclModeSet> =
LazyLock::new(|| BitFlags::from(AclMode::Usage).into());
impl AclModeSet {
pub fn empty() -> Self {
Self {
modes: BitFlags::empty(),
}
}
pub fn readonly() -> Self {
Self {
modes: BitFlags::from(AclMode::Select),
}
}
pub fn has_mode(&self, mode: AclMode) -> bool {
self.modes.contains(mode)
}
pub fn iter(&self) -> impl Iterator<Item = AclMode> + '_ {
self.modes.iter()
}
}
impl From<BitFlags<AclMode>> for AclModeSet {
fn from(modes: BitFlags<AclMode>) -> Self {
Self { modes }
}
}
impl std::fmt::Display for AclModeSet {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.modes)
}
}