username: type to parse username
This commit is contained in:
parent
0e77f7be5e
commit
bdd5eca9f1
7 changed files with 119 additions and 24 deletions
|
|
@ -6,7 +6,7 @@ use database::Error as DatabaseError;
|
|||
use database::Users as DatabaseUsers;
|
||||
use email_address::EmailAddress;
|
||||
use hash::{PaperKey, Password, Secret};
|
||||
use id::UserID;
|
||||
use id::{UserID, Username};
|
||||
use std::str::FromStr;
|
||||
|
||||
impl From<DatabaseUsers> for User {
|
||||
|
|
@ -16,7 +16,7 @@ impl From<DatabaseUsers> for User {
|
|||
created_at: db.created_at,
|
||||
updated_at: db.updated_at,
|
||||
is_admin: db.is_admin,
|
||||
username: db.username,
|
||||
username: Username(db.username),
|
||||
name: db.name,
|
||||
email: db.email,
|
||||
password: db.password,
|
||||
|
|
@ -41,13 +41,17 @@ impl User {
|
|||
conn: impl SqliteExecutor<'_>,
|
||||
id: &UserID,
|
||||
is_admin: bool,
|
||||
username: &str,
|
||||
username: &Username,
|
||||
password: Option<&Password>,
|
||||
) -> Result<Option<()>, Error> {
|
||||
Ok(
|
||||
DatabaseUsers::insert(conn, &id.0, is_admin, username, password.map(|p| p.hash()))
|
||||
.await?,
|
||||
Ok(DatabaseUsers::insert(
|
||||
conn,
|
||||
&id.0,
|
||||
is_admin,
|
||||
username.as_ref(),
|
||||
password.map(|p| p.hash()),
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn get_by_id(
|
||||
|
|
@ -70,9 +74,9 @@ impl User {
|
|||
|
||||
async fn get_by_username(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
username: &str,
|
||||
username: &Username,
|
||||
) -> Result<Option<Self>, Error> {
|
||||
Ok(DatabaseUsers::get_one_by_username(conn, username)
|
||||
Ok(DatabaseUsers::get_one_by_username(conn, username.as_ref())
|
||||
.await?
|
||||
.map(Self::from))
|
||||
}
|
||||
|
|
@ -93,7 +97,11 @@ impl User {
|
|||
}
|
||||
|
||||
// Get user from username
|
||||
Self::get_by_username(conn, login).await
|
||||
if let Ok(username) = Username::from_str(login) {
|
||||
return Self::get_by_username(conn, &username).await;
|
||||
}
|
||||
|
||||
Err(Error::InvalidLogin(login.into()))
|
||||
}
|
||||
|
||||
pub async fn get_one_from_authorization_code(
|
||||
|
|
@ -136,9 +144,9 @@ impl User {
|
|||
pub async fn set_username(
|
||||
&self,
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
username: &str,
|
||||
username: &Username,
|
||||
) -> Result<(), Error> {
|
||||
DatabaseUsers::set_username(conn, self.id.as_ref(), username)
|
||||
DatabaseUsers::set_username(conn, self.id.as_ref(), username.as_ref())
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
DatabaseError::UniqueConstraint(column) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue