Skip to main content

FieldPermissions

Struct FieldPermissions 

Source
pub struct FieldPermissions {
    pub readable_roles: HashSet<String>,
    pub writable_roles: HashSet<String>,
    pub filterable_roles: HashSet<String>,
    pub sortable_roles: HashSet<String>,
}
Expand description

字段权限配置

定义不同角色对字段的访问权限,包括读取、写入、筛选和排序权限。 如果角色列表为空,表示允许所有用户访问。

§示例

use yang_base::table::FieldPermissions;
use std::collections::HashSet;

// 创建一个只有管理员可以读写的字段权限
let admin_only = FieldPermissions {
    readable_roles: HashSet::from(["admin".to_string()]),
    writable_roles: HashSet::from(["admin".to_string()]),
    filterable_roles: HashSet::from(["admin".to_string()]),
    sortable_roles: HashSet::from(["admin".to_string()]),
};

// 检查权限
let admin_roles = HashSet::from(["admin".to_string()]);
let user_roles = HashSet::from(["user".to_string()]);

assert!(admin_only.can_read(&admin_roles));
assert!(!admin_only.can_read(&user_roles));

// 创建一个所有人都可以访问的字段权限
let public = FieldPermissions::default();
assert!(public.can_read(&user_roles));
assert!(public.can_write(&user_roles));

Fields§

§readable_roles: HashSet<String>

可读角色集合

如果集合为空,表示所有用户都可以读取该字段

§writable_roles: HashSet<String>

可写角色集合

如果集合为空,表示所有用户都可以写入该字段

§filterable_roles: HashSet<String>

可筛选角色集合

如果集合为空,表示所有用户都可以使用该字段进行筛选

§sortable_roles: HashSet<String>

可排序角色集合

如果集合为空,表示所有用户都可以使用该字段进行排序

Implementations§

Source§

impl FieldPermissions

Source

pub fn can_read(&self, user_roles: &HashSet<String>) -> bool

检查用户是否有读取权限

§参数
  • user_roles: 用户的角色集合(HashSet,支持 O(1) 查找)
§返回值

如果用户有读取权限返回 true,否则返回 false

§示例
use yang_base::table::FieldPermissions;
use std::collections::HashSet;

let permissions = FieldPermissions {
    readable_roles: HashSet::from(["admin".to_string(), "user".to_string()]),
    ..Default::default()
};

let admin_roles = HashSet::from(["admin".to_string()]);
let user_roles = HashSet::from(["user".to_string()]);

assert!(permissions.can_read(&admin_roles));
assert!(permissions.can_read(&user_roles));
assert!(!permissions.can_read(&HashSet::from(["guest".to_string()])));
Source

pub fn can_write(&self, user_roles: &HashSet<String>) -> bool

检查用户是否有写入权限

§参数
  • user_roles: 用户的角色集合(HashSet,支持 O(1) 查找)
§返回值

如果用户有写入权限返回 true,否则返回 false

§示例
use yang_base::table::FieldPermissions;
use std::collections::HashSet;

let permissions = FieldPermissions {
    writable_roles: HashSet::from(["admin".to_string()]),
    ..Default::default()
};

let admin_roles = HashSet::from(["admin".to_string()]);

assert!(permissions.can_write(&admin_roles));
Source

pub fn can_filter(&self, user_roles: &HashSet<String>) -> bool

检查用户是否有筛选权限

§参数
  • user_roles: 用户的角色列表
§返回值

如果用户有筛选权限返回 true,否则返回 false

§示例
use yang_base::table::FieldPermissions;
use std::collections::HashSet;

let permissions = FieldPermissions {
    filterable_roles: HashSet::from(["admin".to_string()]),
    ..Default::default()
};

let admin_roles = HashSet::from(["admin".to_string()]);
assert!(permissions.can_filter(&admin_roles));
Source

pub fn can_sort(&self, user_roles: &HashSet<String>) -> bool

检查用户是否有排序权限

§参数
  • user_roles: 用户的角色集合(HashSet,支持 O(1) 查找)
§返回值

如果用户有排序权限返回 true,否则返回 false

§示例
use yang_base::table::FieldPermissions;
use std::collections::HashSet;

let permissions = FieldPermissions {
    sortable_roles: HashSet::from(["admin".to_string()]),
    ..Default::default()
};

let admin_roles = HashSet::from(["admin".to_string()]);
assert!(permissions.can_sort(&admin_roles));

Trait Implementations§

Source§

impl Clone for FieldPermissions

Source§

fn clone(&self) -> FieldPermissions

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldPermissions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FieldPermissions

Source§

fn default() -> FieldPermissions

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for FieldPermissions

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for FieldPermissions

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more