pslink_shared/apirequests/
users.rs

1//! Types for user requesting and saving
2use enum_map::{Enum, EnumMap};
3use serde::{Deserialize, Serialize};
4
5use crate::datatypes::User;
6
7use super::general::{EditMode, Filter, Operation, Ordering};
8
9/// Request an ordered and filtered list of users from the server.
10#[derive(Clone, Deserialize, Serialize, Debug)]
11pub struct UserRequestForm {
12    // The filters up to one for each column
13    pub filter: EnumMap<UserOverviewColumns, Filter>,
14    // Order According to this column
15    pub order: Option<Operation<UserOverviewColumns, Ordering>>,
16    // Return a maximum of `amount` results
17    pub amount: usize,
18}
19
20impl Default for UserRequestForm {
21    fn default() -> Self {
22        Self {
23            filter: EnumMap::default(),
24            order: None,
25            amount: 20,
26        }
27    }
28}
29
30/// Data to login.
31#[derive(Debug, Deserialize, Default, Serialize, Clone)]
32pub struct LoginUser {
33    pub username: String,
34    pub password: String,
35}
36
37/// The Struct that is responsible for creating and editing users.
38#[derive(Default, Debug, Clone, Serialize, Deserialize)]
39pub struct UserDelta {
40    pub edit: EditMode,
41    pub id: Option<i64>,
42    pub username: String,
43    pub email: String,
44    pub password: Option<String>,
45    pub role: Role,
46}
47
48impl From<User> for UserDelta {
49    /// Automatically create a `UserDelta` from a User.
50    fn from(u: User) -> Self {
51        Self {
52            edit: EditMode::Edit,
53            id: Some(u.id),
54            username: u.username,
55            email: u.email,
56            password: None,
57            role: u.role,
58        }
59    }
60}
61
62/// The columns in the user view table. The table can be ordered according to these.
63#[allow(clippy::use_self)]
64#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, Eq, Hash, Enum)]
65pub enum UserOverviewColumns {
66    Id,
67    Email,
68    Username,
69}
70
71/// The possible roles a user could have. They are stored as i64 in the database
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Copy)]
73pub enum Role {
74    NotAuthenticated,
75    Disabled,
76    Regular,
77    Admin,
78}
79
80impl Role {
81    #[must_use]
82    pub const fn convert(i: i64) -> Self {
83        match i {
84            0 => Self::Disabled,
85            1 => Self::Regular,
86            2 => Self::Admin,
87            _ => Self::NotAuthenticated,
88        }
89    }
90
91    #[must_use]
92    pub const fn to_i64(self) -> i64 {
93        match self {
94            Role::NotAuthenticated => 3,
95            Role::Disabled => 0,
96            Role::Regular => 1,
97            Role::Admin => 2,
98        }
99    }
100}
101
102impl Default for Role {
103    fn default() -> Self {
104        Self::Regular
105    }
106}