Skip to main content

nil_server_database/model/
user.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::Database;
5use crate::error::{Error, Result};
6use crate::sql_types::hashed_password::HashedPassword;
7use crate::sql_types::id::UserId;
8use crate::sql_types::player_id::PlayerId;
9use crate::sql_types::zoned::Zoned;
10use diesel::prelude::*;
11use nil_crypto::password::Password;
12use std::fmt;
13use tokio::task::spawn_blocking;
14
15#[derive(Identifiable, Queryable, Selectable, Clone)]
16#[diesel(table_name = crate::schema::user)]
17#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
18pub struct User {
19  pub id: UserId,
20  pub player_id: PlayerId,
21  pub password: HashedPassword,
22  pub created_at: Zoned,
23  pub updated_at: Zoned,
24}
25
26impl User {
27  #[inline]
28  pub async fn verify_password(&self, password: Password) -> Result<bool> {
29    let hashed = self.password.clone();
30    spawn_blocking(move || hashed.verify(&password))
31      .await?
32      .map_err(Into::into)
33  }
34}
35
36impl fmt::Debug for User {
37  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38    f.debug_struct("User")
39      .field("id", &self.id.to_string())
40      .field("player_id", &self.player_id)
41      .field("created_at", &self.created_at.to_string())
42      .field("updated_at", &self.updated_at.to_string())
43      .finish_non_exhaustive()
44  }
45}
46
47#[derive(Insertable, Clone)]
48#[diesel(table_name = crate::schema::user)]
49pub struct NewUser {
50  player_id: PlayerId,
51  password: HashedPassword,
52  created_at: Zoned,
53  updated_at: Zoned,
54}
55
56impl NewUser {
57  pub async fn new(player_id: impl Into<PlayerId>, password: Password) -> Result<Self> {
58    let player_id: PlayerId = player_id.into();
59    let id_len = player_id.trim().chars().count();
60
61    if !(1..=20).contains(&id_len) {
62      return Err(Error::InvalidUsername(player_id));
63    }
64
65    Ok(Self {
66      player_id,
67      password: hash_password(password).await?,
68      created_at: Zoned::now(),
69      updated_at: Zoned::now(),
70    })
71  }
72
73  pub fn player_id(&self) -> PlayerId {
74    self.player_id.clone()
75  }
76
77  pub async fn create(self, database: &Database) -> Result<usize> {
78    database.create_user(self).await
79  }
80}
81
82impl fmt::Debug for NewUser {
83  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84    f.debug_struct("NewUser")
85      .field("player_id", &self.player_id)
86      .field("created_at", &self.created_at.to_string())
87      .field("updated_at", &self.updated_at.to_string())
88      .finish_non_exhaustive()
89  }
90}
91
92async fn hash_password(password: Password) -> Result<HashedPassword> {
93  let pass_len = password.trim().chars().count();
94  if !(3..=50).contains(&pass_len) {
95    return Err(Error::InvalidPassword);
96  }
97
98  spawn_blocking(move || HashedPassword::new(&password))
99    .await?
100    .map_err(Into::into)
101}