kustos_shared/
access.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5use std::{fmt::Display, str::FromStr};
6
7use crate::subject::ParsingError;
8
9/// Permission access variants
10///
11/// Get, Put, Post, Delete are the respective HTTP methods.
12/// The request middlewares are limited to these methods.
13/// Read and Write can be used for more granular access when used with direct enforce calls.
14#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
15pub enum AccessMethod {
16    Read,
17    Write,
18    Get,
19    Post,
20    Put,
21    Patch,
22    Delete,
23}
24
25impl AccessMethod {
26    pub const GET: AccessMethod = AccessMethod::Get;
27    pub const POST: AccessMethod = AccessMethod::Post;
28    pub const PUT: AccessMethod = AccessMethod::Put;
29    pub const PATCH: AccessMethod = AccessMethod::Patch;
30    pub const DELETE: AccessMethod = AccessMethod::Delete;
31
32    pub fn all_http() -> [AccessMethod; 5] {
33        [Self::GET, Self::POST, Self::PUT, Self::PATCH, Self::DELETE]
34    }
35}
36
37impl FromStr for AccessMethod {
38    type Err = ParsingError;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        Ok(match s {
42            "read" => AccessMethod::Read,
43            "write" => AccessMethod::Write,
44            "GET" => AccessMethod::Get,
45            "POST" => AccessMethod::Post,
46            "PUT" => AccessMethod::Put,
47            "PATCH" => AccessMethod::Patch,
48            "DELETE" => AccessMethod::Delete,
49            _ => {
50                return Err(ParsingError::InvalidAccessMethod {
51                    method: s.to_owned(),
52                });
53            }
54        })
55    }
56}
57
58impl AsRef<str> for AccessMethod {
59    fn as_ref(&self) -> &str {
60        match self {
61            AccessMethod::Read => "read",
62            AccessMethod::Write => "write",
63            AccessMethod::Get => "GET",
64            AccessMethod::Post => "POST",
65            AccessMethod::Patch => "PATCH",
66            AccessMethod::Put => "PUT",
67            AccessMethod::Delete => "DELETE",
68        }
69    }
70}
71
72impl Display for AccessMethod {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        write!(f, "{}", self.as_ref())
75    }
76}
77
78// TODO(r.floren) does this trait impl make any sense now?
79impl From<AccessMethod> for [AccessMethod; 1] {
80    fn from(val: AccessMethod) -> Self {
81        [val]
82    }
83}
84
85impl From<AccessMethod> for Vec<AccessMethod> {
86    fn from(method: AccessMethod) -> Self {
87        vec![method]
88    }
89}
90
91impl From<http::Method> for AccessMethod {
92    fn from(method: http::Method) -> Self {
93        match method {
94            http::Method::GET => Self::GET,
95            http::Method::POST => Self::POST,
96            http::Method::PATCH => Self::PATCH,
97            http::Method::PUT => Self::PUT,
98            http::Method::DELETE => Self::DELETE,
99            _ => unimplemented!(),
100        }
101    }
102}