Phase 1: app, Kanidm login, people, passports, addresses, audit log
visa.buildfor.life keeps the immigration records of the foreigners B4L looks after. This first phase is the base the deadlines and extension packs build on (docs/PLAN.md, phases 2 to 7). - One Rust binary (axum 0.8, minijinja, sqlx on PostgreSQL) with its templates, static files and migrations compiled in. - Login through Kanidm (OpenID Connect with PKCE and a nonce, ES256), client visa-web, group visa; sessions in PostgreSQL keyed by the SHA-256 of the cookie. VISA_INSECURE_NO_LOGIN=1 for loopback development only. - Same-site check and form token on every POST; a CSP without inline script or style; security headers. - People: list with filter, page, create, edit, archive, restore. - Passports from the machine-readable zone with check digits (ICAO 9303 TD3); expiry warnings at 12 and 6 months. - Addresses in Thailand in the order Thai forms use, with the house master and the TM.30 receipt. - Append-only audit log of every view and change, with the names of the changed fields but never their values (PDPA); the Activity page. - Invented demo people (visa seed-demo, refused when login is on). - 40 tests: unit tests, pages with demo data, the forms, the audit log and the whole login against a fake OpenID provider. - CI: gitleaks over the history, fmt, clippy and the tests against PostgreSQL 18; the pre-commit secret scan.
This commit is contained in:
commit
2e009700a9
56 files changed
+11193
No files matched your search
+164
@@ -0,0 +1,164 @@
|
||||
//! The audit log (PDPA): who viewed or changed which record. It holds the
|
||||
//! names of changed fields, never their values, and the table refuses
|
||||
//! updates and deletes (migrations/0001_init.sql).
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use sqlx::{Executor, PgPool, Postgres};
|
||||
|
||||
use crate::dates;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Action {
|
||||
View,
|
||||
Create,
|
||||
Update,
|
||||
Delete,
|
||||
Archive,
|
||||
Restore,
|
||||
SignIn,
|
||||
SignOut,
|
||||
}
|
||||
|
||||
impl Action {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Action::View => "view",
|
||||
Action::Create => "create",
|
||||
Action::Update => "update",
|
||||
Action::Delete => "delete",
|
||||
Action::Archive => "archive",
|
||||
Action::Restore => "restore",
|
||||
Action::SignIn => "sign-in",
|
||||
Action::SignOut => "sign-out",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Entity {
|
||||
Person,
|
||||
Passport,
|
||||
Address,
|
||||
Session,
|
||||
}
|
||||
|
||||
impl Entity {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Entity::Person => "person",
|
||||
Entity::Passport => "passport",
|
||||
Entity::Address => "address",
|
||||
Entity::Session => "session",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Entry<'a> {
|
||||
pub actor: &'a str,
|
||||
pub action: Action,
|
||||
pub entity: Entity,
|
||||
pub entity_id: Option<i64>,
|
||||
pub person_id: Option<i64>,
|
||||
pub fields: &'a [String],
|
||||
}
|
||||
|
||||
/// Writes one entry; pass the transaction (`&mut *tx`) when the change it
|
||||
/// records is in one, so both commit or roll back together.
|
||||
pub async fn record<'e, E>(ex: E, e: &Entry<'_>) -> Result<()>
|
||||
where
|
||||
E: Executor<'e, Database = Postgres>,
|
||||
{
|
||||
sqlx::query(
|
||||
"INSERT INTO audit_log (actor, action, entity, entity_id, person_id, fields) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
)
|
||||
.bind(e.actor)
|
||||
.bind(e.action.as_str())
|
||||
.bind(e.entity.as_str())
|
||||
.bind(e.entity_id)
|
||||
.bind(e.person_id)
|
||||
.bind(e.fields)
|
||||
.execute(ex)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A view of a person's record.
|
||||
pub async fn viewed(db: &PgPool, actor: &str, entity: Entity, id: i64, person: i64) -> Result<()> {
|
||||
record(
|
||||
db,
|
||||
&Entry {
|
||||
actor,
|
||||
action: Action::View,
|
||||
entity,
|
||||
entity_id: Some(id),
|
||||
person_id: Some(person),
|
||||
fields: &[],
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
struct Row {
|
||||
id: i64,
|
||||
at: DateTime<Utc>,
|
||||
actor: String,
|
||||
action: String,
|
||||
entity: String,
|
||||
entity_id: Option<i64>,
|
||||
person_id: Option<i64>,
|
||||
fields: Vec<String>,
|
||||
person_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Item {
|
||||
pub id: i64,
|
||||
pub at: String,
|
||||
pub actor: String,
|
||||
pub action: String,
|
||||
pub what: String,
|
||||
pub person_id: Option<i64>,
|
||||
pub person_name: Option<String>,
|
||||
pub fields: String,
|
||||
}
|
||||
|
||||
/// The newest entries first, for one person or for everyone.
|
||||
pub async fn recent(db: &PgPool, person: Option<i64>, limit: i64) -> Result<Vec<Item>> {
|
||||
let rows: Vec<Row> = sqlx::query_as(
|
||||
"SELECT a.id, a.at, a.actor, a.action, a.entity, a.entity_id, a.person_id, a.fields, \
|
||||
CASE WHEN p.id IS NULL THEN NULL \
|
||||
ELSE upper(p.surname) || ', ' || trim(p.first_name || ' ' || p.middle_name) \
|
||||
END AS person_name \
|
||||
FROM audit_log a LEFT JOIN people p ON p.id = a.person_id \
|
||||
WHERE ($1::bigint IS NULL OR a.person_id = $1) \
|
||||
ORDER BY a.id DESC LIMIT $2",
|
||||
)
|
||||
.bind(person)
|
||||
.bind(limit)
|
||||
.fetch_all(db)
|
||||
.await?;
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|r| {
|
||||
let what = match (r.entity.as_str(), r.entity_id) {
|
||||
("session", _) => String::new(),
|
||||
(e, Some(id)) => format!("{e} {id}"),
|
||||
(e, None) => e.to_string(),
|
||||
};
|
||||
Item {
|
||||
id: r.id,
|
||||
at: dates::fmt_datetime(r.at),
|
||||
actor: r.actor,
|
||||
action: r.action,
|
||||
what,
|
||||
person_id: r.person_id,
|
||||
person_name: r.person_name,
|
||||
fields: r.fields.join(", ").replace('_', " "),
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
Reference in new issue
Block a user