users: get user by id, email, username
This commit is contained in:
parent
b8f6cae85e
commit
d790d2ff29
8 changed files with 254 additions and 6 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -580,6 +580,12 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "email_address"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2153bd83ebc09db15bcbdc3e2194d901804952e3dc96967e1cd3b0c5c32d112"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "encoding_rs"
|
name = "encoding_rs"
|
||||||
version = "0.8.32"
|
version = "0.8.32"
|
||||||
|
|
@ -3031,6 +3037,7 @@ version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"database",
|
"database",
|
||||||
|
"email_address",
|
||||||
"hash",
|
"hash",
|
||||||
"id",
|
"id",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|
|
||||||
14
crates/database/queries/users/get_one_by_email.sql
Normal file
14
crates/database/queries/users/get_one_by_email.sql
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
select id,
|
||||||
|
created_at as "created_at: DateTime<Utc>",
|
||||||
|
updated_at as "updated_at: DateTime<Utc>",
|
||||||
|
is_admin as "is_admin: bool",
|
||||||
|
username,
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
password_recover,
|
||||||
|
paper_key,
|
||||||
|
is_archived as "is_archived: bool"
|
||||||
|
from users
|
||||||
|
|
||||||
|
where email is (?)
|
||||||
14
crates/database/queries/users/get_one_by_username.sql
Normal file
14
crates/database/queries/users/get_one_by_username.sql
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
select id,
|
||||||
|
created_at as "created_at: DateTime<Utc>",
|
||||||
|
updated_at as "updated_at: DateTime<Utc>",
|
||||||
|
is_admin as "is_admin: bool",
|
||||||
|
username,
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
password_recover,
|
||||||
|
paper_key,
|
||||||
|
is_archived as "is_archived: bool"
|
||||||
|
from users
|
||||||
|
|
||||||
|
where username is (?)
|
||||||
|
|
@ -88,6 +88,84 @@
|
||||||
},
|
},
|
||||||
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n business_name,\n business_logo,\n url\n\nfrom settings\n\nwhere id is 0\n"
|
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n business_name,\n business_logo,\n url\n\nfrom settings\n\nwhere id is 0\n"
|
||||||
},
|
},
|
||||||
|
"69752cc2a3fc91d4e7a39e0b167695f431380bd40df9638b5df3534715de04b0": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "created_at: DateTime<Utc>",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "updated_at: DateTime<Utc>",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_admin: bool",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Int64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "username",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "name",
|
||||||
|
"ordinal": 5,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "email",
|
||||||
|
"ordinal": 6,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "password",
|
||||||
|
"ordinal": 7,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "password_recover",
|
||||||
|
"ordinal": 8,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "paper_key",
|
||||||
|
"ordinal": 9,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_archived: bool",
|
||||||
|
"ordinal": 10,
|
||||||
|
"type_info": "Int64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n is_admin as \"is_admin: bool\",\n username,\n name,\n email,\n password,\n password_recover,\n paper_key,\n is_archived as \"is_archived: bool\"\nfrom users\n\nwhere email is (?)\n"
|
||||||
|
},
|
||||||
"87906834faa6f185aee0e4d893b9754908b1c173e9dce383663d723891a89cd1": {
|
"87906834faa6f185aee0e4d893b9754908b1c173e9dce383663d723891a89cd1": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
|
|
@ -108,6 +186,84 @@
|
||||||
},
|
},
|
||||||
"query": "update settings\n\nset first_admin = ?\n\nwhere id is 0\n"
|
"query": "update settings\n\nset first_admin = ?\n\nwhere id is 0\n"
|
||||||
},
|
},
|
||||||
|
"b652e67e8e1cd0e2b55c830c5569eb1c6caf73857215b4298265cce5c5462902": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "created_at: DateTime<Utc>",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "updated_at: DateTime<Utc>",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_admin: bool",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Int64"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "username",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "name",
|
||||||
|
"ordinal": 5,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "email",
|
||||||
|
"ordinal": 6,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "password",
|
||||||
|
"ordinal": 7,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "password_recover",
|
||||||
|
"ordinal": 8,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "paper_key",
|
||||||
|
"ordinal": 9,
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "is_archived: bool",
|
||||||
|
"ordinal": 10,
|
||||||
|
"type_info": "Int64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Right": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "select id,\n created_at as \"created_at: DateTime<Utc>\",\n updated_at as \"updated_at: DateTime<Utc>\",\n is_admin as \"is_admin: bool\",\n username,\n name,\n email,\n password,\n password_recover,\n paper_key,\n is_archived as \"is_archived: bool\"\nfrom users\n\nwhere username is (?)\n"
|
||||||
|
},
|
||||||
"c5a57c971d07532ec0cc897b5ac06e0814e506f9c24647d1eaf44174dc0a5954": {
|
"c5a57c971d07532ec0cc897b5ac06e0814e506f9c24647d1eaf44174dc0a5954": {
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
|
|
|
||||||
|
|
@ -51,4 +51,24 @@ impl Users {
|
||||||
.await
|
.await
|
||||||
.map_err(handle_error)
|
.map_err(handle_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_one_by_email(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
email: &str,
|
||||||
|
) -> Result<Option<Self>, Error> {
|
||||||
|
sqlx::query_file_as!(Self, "queries/users/get_one_by_email.sql", email)
|
||||||
|
.fetch_optional(conn)
|
||||||
|
.await
|
||||||
|
.map_err(handle_error)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_one_by_username(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
username: &str,
|
||||||
|
) -> Result<Option<Self>, Error> {
|
||||||
|
sqlx::query_file_as!(Self, "queries/users/get_one_by_username.sql", username)
|
||||||
|
.fetch_optional(conn)
|
||||||
|
.await
|
||||||
|
.map_err(handle_error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ async fn avatar(
|
||||||
size: Option<u32>,
|
size: Option<u32>,
|
||||||
) -> Result<FileFromBytes> {
|
) -> Result<FileFromBytes> {
|
||||||
// Verify existence of user
|
// Verify existence of user
|
||||||
let _user = User::get_by_id(&mut *db, &user_id.0)
|
let _user = User::get_by_login(&mut *db, user_id.0.as_ref())
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| Error::not_found(user_id.0.to_string()))?;
|
.ok_or_else(|| Error::not_found(user_id.0.to_string()))?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,4 @@ hash = { path = "../hash" }
|
||||||
id = { path = "../id" }
|
id = { path = "../id" }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
chrono = { workspace = true }
|
chrono = { workspace = true }
|
||||||
|
email_address = { version = "0.2.4", default-features = false }
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,10 @@ use crate::error::Error;
|
||||||
use crate::User;
|
use crate::User;
|
||||||
use database::sqlx::SqliteExecutor;
|
use database::sqlx::SqliteExecutor;
|
||||||
use database::Users as DatabaseUsers;
|
use database::Users as DatabaseUsers;
|
||||||
|
use email_address::EmailAddress;
|
||||||
use hash::Password;
|
use hash::Password;
|
||||||
use id::UserID;
|
use id::UserID;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
impl From<DatabaseUsers> for User {
|
impl From<DatabaseUsers> for User {
|
||||||
fn from(db: DatabaseUsers) -> Self {
|
fn from(db: DatabaseUsers) -> Self {
|
||||||
|
|
@ -43,12 +45,46 @@ impl User {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_by_id(
|
async fn get_by_id(conn: impl SqliteExecutor<'_>, id: &UserID) -> Result<Option<Self>, Error> {
|
||||||
conn: impl SqliteExecutor<'_>,
|
|
||||||
id: &UserID,
|
|
||||||
) -> Result<Option<Self>, Error> {
|
|
||||||
Ok(DatabaseUsers::get_one_by_id(conn, &id.0)
|
Ok(DatabaseUsers::get_one_by_id(conn, &id.0)
|
||||||
.await?
|
.await?
|
||||||
.map(Self::from))
|
.map(Self::from))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_by_email(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
email: &EmailAddress,
|
||||||
|
) -> Result<Option<Self>, Error> {
|
||||||
|
Ok(DatabaseUsers::get_one_by_email(conn, email.as_str())
|
||||||
|
.await?
|
||||||
|
.map(Self::from))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_by_username(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
username: &str,
|
||||||
|
) -> Result<Option<Self>, Error> {
|
||||||
|
Ok(DatabaseUsers::get_one_by_username(conn, username)
|
||||||
|
.await?
|
||||||
|
.map(Self::from))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get by ID, Email, Username (in that order)
|
||||||
|
pub async fn get_by_login(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
login: &str,
|
||||||
|
) -> Result<Option<Self>, Error> {
|
||||||
|
// Parse login as ID
|
||||||
|
if let Ok(id) = UserID::from_str(login) {
|
||||||
|
return Self::get_by_id(conn, &id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse login as email
|
||||||
|
if let Ok(email) = EmailAddress::from_str(login) {
|
||||||
|
return Self::get_by_email(conn, &email).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user from username
|
||||||
|
Self::get_by_username(conn, login).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue