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
impl FieldPermissions
Sourcepub fn can_read(&self, user_roles: &HashSet<String>) -> bool
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()])));Sourcepub fn can_write(&self, user_roles: &HashSet<String>) -> bool
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));Sourcepub fn can_filter(&self, user_roles: &HashSet<String>) -> bool
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));Sourcepub fn can_sort(&self, user_roles: &HashSet<String>) -> bool
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
impl Clone for FieldPermissions
Source§fn clone(&self) -> FieldPermissions
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FieldPermissions
impl Debug for FieldPermissions
Source§impl Default for FieldPermissions
impl Default for FieldPermissions
Source§fn default() -> FieldPermissions
fn default() -> FieldPermissions
Returns the “default value” for a type. Read more
Source§impl<'de> Deserialize<'de> for FieldPermissions
impl<'de> Deserialize<'de> for FieldPermissions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for FieldPermissions
impl RefUnwindSafe for FieldPermissions
impl Send for FieldPermissions
impl Sync for FieldPermissions
impl Unpin for FieldPermissions
impl UnsafeUnpin for FieldPermissions
impl UnwindSafe for FieldPermissions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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