Skip to main content

kellnr_db/
user.rs

1use kellnr_common::account_name::UserName;
2use kellnr_entity::user;
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5
6use crate::UserId;
7
8#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
9pub struct User {
10    #[serde(skip_serializing)]
11    pub id: UserId,
12    pub name: UserName,
13    #[serde(skip_serializing)]
14    pub pwd: String,
15    #[serde(skip_serializing)]
16    pub salt: String,
17    pub is_admin: bool,
18    pub is_read_only: bool,
19    pub created: String,
20}
21
22/// The rights of a user, stored in the `is_admin` and `is_read_only`
23/// columns. An admin can be read-only as well.
24#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
25pub struct UserFlags {
26    pub admin: bool,
27    pub read_only: bool,
28}
29
30impl UserFlags {
31    pub const fn normal() -> Self {
32        Self {
33            admin: false,
34            read_only: false,
35        }
36    }
37
38    pub const fn admin() -> Self {
39        Self {
40            admin: true,
41            read_only: false,
42        }
43    }
44
45    pub const fn read_only() -> Self {
46        Self {
47            admin: false,
48            read_only: true,
49        }
50    }
51}
52
53impl From<user::Model> for User {
54    fn from(u: user::Model) -> Self {
55        Self {
56            id: UserId::new(u.id),
57            name: UserName::from_unchecked(u.name),
58            pwd: u.pwd,
59            salt: u.salt,
60            is_admin: u.is_admin,
61            is_read_only: u.is_read_only,
62            created: u.created,
63        }
64    }
65}