Ajoute un administrateur qui gère les comptes

Le premier compte inscrit devient administrateur. La page /admin lui
permet de lister, créer, modifier et supprimer les comptes, même quand
les inscriptions sont fermées.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Romulus21andClaude Opus 5.5 committed 2026-10-11 14:03:47 +02:00
1 parent 2ade50465e
commit f3f5f0cee1
7 files changed
+539 -34

No files matched your search

+68 -13
View File
@@ -3,7 +3,7 @@ use sqlx::{Row, SqliteConnection};
use std::str::FromStr;
use crate::model::{
Category, Icon, Item, ItemInput, ListColor, Member, Quantity, ShoppingList, Suggestion, Unit, User,
Account, Category, Icon, Item, ItemInput, ListColor, Member, Quantity, ShoppingList, Suggestion, Unit, User,
};
pub async fn connect(url: &str) -> Result<SqlitePool, sqlx::Error> {
@@ -61,6 +61,7 @@ fn user_from_row(row: &SqliteRow) -> Result<User, sqlx::Error> {
id: row.try_get("id")?,
name: row.try_get("name")?,
email: row.try_get("email")?,
is_admin: row.try_get("is_admin")?,
})
}
@@ -76,17 +77,20 @@ const STARTER_CATALOG: &[(&str, Icon, Category)] = &[
("Café en grains", Icon::Coffee, Category::Epicerie),
];
/// Creates the account with a first list and starter suggestions.
/// Fails with a unique violation if the email is taken.
/// Creates the account with a first list and starter suggestions; the very
/// first account is an admin. Fails with a unique violation if the email is taken.
pub async fn register(pool: &SqlitePool, email: &str, name: &str, password_hash: &str) -> Result<i64, sqlx::Error> {
let mut tx = pool.begin().await?;
let user_id: i64 = sqlx::query("INSERT INTO users (email, name, password_hash) VALUES (?, ?, ?) RETURNING id")
.bind(email)
.bind(name)
.bind(password_hash)
.fetch_one(&mut *tx)
.await?
.try_get("id")?;
let user_id: i64 = sqlx::query(
"INSERT INTO users (email, name, password_hash, is_admin)
VALUES (?, ?, ?, NOT EXISTS (SELECT 1 FROM users)) RETURNING id",
)
.bind(email)
.bind(name)
.bind(password_hash)
.fetch_one(&mut *tx)
.await?
.try_get("id")?;
// Lists created before accounts existed go to the first user.
let claimed = sqlx::query("UPDATE lists SET owner_id = ? WHERE owner_id IS NULL")
@@ -113,7 +117,7 @@ pub async fn register(pool: &SqlitePool, email: &str, name: &str, password_hash:
/// The user and their password hash.
pub async fn user_by_email(pool: &SqlitePool, email: &str) -> Result<Option<(User, String)>, sqlx::Error> {
sqlx::query("SELECT id, name, email, password_hash FROM users WHERE email = ?")
sqlx::query("SELECT id, name, email, is_admin, password_hash FROM users WHERE email = ?")
.bind(email)
.fetch_optional(pool)
.await?
@@ -136,7 +140,7 @@ pub async fn create_session(pool: &SqlitePool, token_hash: &str, user_id: i64, d
pub async fn user_by_session(pool: &SqlitePool, token_hash: &str) -> Result<Option<User>, sqlx::Error> {
sqlx::query(
"SELECT u.id, u.name, u.email FROM sessions s JOIN users u ON u.id = s.user_id
"SELECT u.id, u.name, u.email, u.is_admin FROM sessions s JOIN users u ON u.id = s.user_id
WHERE s.token_hash = ? AND s.expires_at > datetime('now')",
)
.bind(token_hash)
@@ -155,6 +159,57 @@ pub async fn delete_session(pool: &SqlitePool, token_hash: &str) -> Result<(), s
Ok(())
}
// --- Administration ---------------------------------------------------------
pub async fn has_users(pool: &SqlitePool) -> Result<bool, sqlx::Error> {
sqlx::query("SELECT EXISTS (SELECT 1 FROM users) AS has_users")
.fetch_one(pool)
.await?
.try_get("has_users")
}
pub async fn accounts(pool: &SqlitePool) -> Result<Vec<Account>, sqlx::Error> {
sqlx::query("SELECT id, name, email, is_admin, date(created_at) AS created_at FROM users ORDER BY id")
.fetch_all(pool)
.await?
.iter()
.map(|row| Ok(Account { user: user_from_row(row)?, created_at: row.try_get("created_at")? }))
.collect()
}
/// Fails with a unique violation if the email is taken.
pub async fn update_user(pool: &SqlitePool, id: i64, name: &str, email: &str, is_admin: bool) -> Result<(), sqlx::Error> {
sqlx::query("UPDATE users SET name = ?, email = ?, is_admin = ? WHERE id = ?")
.bind(name)
.bind(email)
.bind(is_admin)
.bind(id)
.execute(pool)
.await?;
Ok(())
}
/// Changes the password and signs the user out everywhere.
pub async fn set_password(pool: &SqlitePool, id: i64, password_hash: &str) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
sqlx::query("UPDATE users SET password_hash = ? WHERE id = ?")
.bind(password_hash)
.bind(id)
.execute(&mut *tx)
.await?;
sqlx::query("DELETE FROM sessions WHERE user_id = ?")
.bind(id)
.execute(&mut *tx)
.await?;
tx.commit().await
}
/// Deletes the account with its sessions, suggestions and the lists it owns.
pub async fn delete_user(pool: &SqlitePool, id: i64) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM users WHERE id = ?").bind(id).execute(pool).await?;
Ok(())
}
// --- Lists & sharing --------------------------------------------------------
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -226,7 +281,7 @@ pub async fn delete_list(pool: &SqlitePool, id: i64) -> Result<(), sqlx::Error>
pub async fn members(pool: &SqlitePool, list_id: i64) -> Result<Vec<Member>, sqlx::Error> {
sqlx::query(
"SELECT u.id, u.name, u.email FROM list_members m JOIN users u ON u.id = m.user_id
"SELECT u.id, u.name, u.email, u.is_admin FROM list_members m JOIN users u ON u.id = m.user_id
WHERE m.list_id = ? ORDER BY u.name",
)
.bind(list_id)