feat(project): add project permission helpers
zhenyi committed
Added lib/project/permission.rs lib/project/permission.rs +40 −0 View lib/project/permission.rs
@@ -0,0 +1,40 @@
1use std::collections::HashSet;
2
3pub const VIEW_PROJECT: &str = "view_project";
4pub const EDIT_PROJECT: &str = "edit_project";
5pub const DELETE_PROJECT: &str = "delete_project";
6pub const TRANSITION_STATE: &str = "transition_state";
7
8pub const MANAGE_MEMBERS: &str = "manage_members";
9pub const MANAGE_DOCUMENTS: &str = "manage_documents";
10pub const MANAGE_VIEWS: &str = "manage_views";
11pub const MANAGE_LINKS: &str = "manage_links";
12pub const MANAGE_ACTIVITIES: &str = "manage_activities";
13
14pub const CREATE_ISSUE_LINK: &str = "create_issue_link";
15pub const STAR: &str = "star";
16
17#[derive(Debug, Clone, Default)]
18pub struct Permissions {
19 granted: HashSet<String>,
20}
21
22impl Permissions {
23 pub fn from_vec(perms: &[&'static str]) -> Self {
24 Self {
25 granted: perms.iter().map(|s| s.to_string()).collect(),
26 }
27 }
28
29 pub fn has(&self, action: &str) -> bool {
30 self.granted.contains(action)
31 }
32
33 pub fn require(&self, action: &str) -> Result<(), String> {
34 if self.has(action) {
35 Ok(())
36 } else {
37 Err(action.to_string())
38 }
39 }
40}