Skip to main content

arks_server/web/core/
route.rs

1//! Typed route URL builders and declarative per-route access policies.
2
3use super::ctx::MountPrefix;
4use super::html::url_encode_component;
5
6/// Declarative authorization & archival policy evaluated centrally by Web UI middleware.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum AccessPolicy {
9    /// Public endpoint (login, register, assets, robots.txt).
10    Public,
11    /// Read-only repository page (enforces repository visibility when private).
12    ReadRepo,
13    /// State-mutating repository action (requires write RBAC, CSRF, and non-archived repo).
14    WriteRepo,
15    /// Repository settings mutation permitted even while archived (e.g. unarchiving).
16    WriteRepoUnarchive,
17    /// Administrative endpoint (requires admin role and CSRF on mutations).
18    Admin,
19}
20
21/// Typed application routes generating percent-encoded, mount-aware URLs.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum AppRoute<'a> {
24    Home,
25    Login,
26    Register,
27    Logout,
28    Search,
29    Notifications,
30    Releases,
31    Branches,
32    Tags,
33    Commits {
34        rev: Option<&'a str>,
35    },
36    Commit {
37        oid: &'a str,
38    },
39    Compare {
40        spec: Option<&'a str>,
41    },
42    Tree {
43        rev: &'a str,
44        path: &'a str,
45    },
46    Blob {
47        rev: &'a str,
48        path: &'a str,
49    },
50    Issues {
51        state: Option<&'a str>,
52        label: Option<&'a str>,
53    },
54    IssueDetail {
55        id: u64,
56    },
57    Pulls,
58    PullNew,
59    PullDetail {
60        branch: &'a str,
61    },
62    Actions,
63    ActionRun {
64        id: u64,
65    },
66    Projects,
67    Wiki {
68        slug: Option<&'a str>,
69    },
70    Security,
71    Insights,
72    Settings {
73        section: Option<&'a str>,
74    },
75    Admin,
76    Agents,
77    /// Extension path (e.g. `v2.0` `"/graph"`, `"/stacks"`, `"/agents/topology"`).
78    Custom(&'a str),
79}
80
81impl<'a> AppRoute<'a> {
82    /// Format this route under `mount`, percent-encoding dynamic path and query values.
83    pub fn to_url(self, mount: &MountPrefix) -> String {
84        match self {
85            AppRoute::Home => mount.path("/"),
86            AppRoute::Login => mount.path("/login"),
87            AppRoute::Register => mount.path("/register"),
88            AppRoute::Logout => mount.path("/logout"),
89            AppRoute::Search => mount.path("/search"),
90            AppRoute::Notifications => mount.path("/notifications"),
91            AppRoute::Releases => mount.path("/releases"),
92            AppRoute::Branches => mount.path("/branches"),
93            AppRoute::Tags => mount.path("/tags"),
94            AppRoute::Commits { rev: None } => mount.path("/commits"),
95            AppRoute::Commits { rev: Some(r) } => {
96                mount.path(&format!("/commits/{}", encode_ref_path(r)))
97            }
98            AppRoute::Commit { oid } => {
99                mount.path(&format!("/commit/{}", url_encode_component(oid)))
100            }
101            AppRoute::Compare { spec: None } => mount.path("/compare"),
102            AppRoute::Compare { spec: Some(s) } => mount.path(&format!("/compare/{s}")),
103            AppRoute::Tree { rev, path } => {
104                if path.is_empty() {
105                    mount.path(&format!("/tree/{}", encode_ref_path(rev)))
106                } else {
107                    mount.path(&format!(
108                        "/tree/{}/{}",
109                        encode_ref_path(rev),
110                        encode_ref_path(path)
111                    ))
112                }
113            }
114            AppRoute::Blob { rev, path } => mount.path(&format!(
115                "/blob/{}/{}",
116                encode_ref_path(rev),
117                encode_ref_path(path)
118            )),
119            AppRoute::Issues { state, label } => {
120                let mut url = mount.path("/issues");
121                let mut sep = '?';
122                if let Some(st) = state {
123                    url.push(sep);
124                    url.push_str("state=");
125                    url.push_str(&url_encode_component(st));
126                    sep = '&';
127                }
128                if let Some(lbl) = label {
129                    url.push(sep);
130                    url.push_str("label=");
131                    url.push_str(&url_encode_component(lbl));
132                }
133                url
134            }
135            AppRoute::IssueDetail { id } => mount.path(&format!("/issues/{id}")),
136            AppRoute::Pulls => mount.path("/pulls"),
137            AppRoute::PullNew => mount.path("/pulls/new"),
138            AppRoute::PullDetail { branch } => {
139                mount.path(&format!("/pulls/{}", encode_ref_path(branch)))
140            }
141            AppRoute::Actions => mount.path("/actions"),
142            AppRoute::ActionRun { id } => mount.path(&format!("/actions/runs/{id}")),
143            AppRoute::Projects => mount.path("/projects"),
144            AppRoute::Wiki { slug: None } => mount.path("/wiki"),
145            AppRoute::Wiki { slug: Some(s) } => {
146                mount.path(&format!("/wiki/{}", url_encode_component(s)))
147            }
148            AppRoute::Security => mount.path("/security"),
149            AppRoute::Insights => mount.path("/insights"),
150            AppRoute::Settings { section: None } => mount.path("/settings"),
151            AppRoute::Settings { section: Some(s) } => {
152                mount.path(&format!("/settings/{}", encode_ref_path(s)))
153            }
154            AppRoute::Admin => mount.path("/admin"),
155            AppRoute::Agents => mount.path("/agents"),
156            AppRoute::Custom(rel) => mount.path(rel),
157        }
158    }
159}
160
161fn encode_ref_path(path: &str) -> String {
162    path.split('/')
163        .map(url_encode_component)
164        .collect::<Vec<_>>()
165        .join("/")
166}
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171
172    #[test]
173    fn typed_routes_apply_mount_prefix_and_percent_encoding() {
174        let root = MountPrefix::root();
175        let multi = MountPrefix::new("alpha");
176
177        assert_eq!(
178            AppRoute::Issues {
179                state: Some("open"),
180                label: Some("bug & ui"),
181            }
182            .to_url(&multi),
183            "/alpha/issues?state=open&label=bug%20%26%20ui"
184        );
185        assert_eq!(
186            AppRoute::PullDetail {
187                branch: "feat/my branch"
188            }
189            .to_url(&root),
190            "/pulls/feat/my%20branch"
191        );
192        assert_eq!(AppRoute::Custom("/graph").to_url(&multi), "/alpha/graph");
193    }
194}