use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::core::SqlValue;
use crate::sql::sqlx::PgPool;
use axum::routing::{get, post};
use axum::Router;
use super::errors::AdminError;
use super::views;
pub type AdminActionFuture<'a> = Pin<Box<dyn Future<Output = Result<(), AdminError>> + Send + 'a>>;
pub type AdminActionFn =
Arc<dyn for<'a> Fn(&'a PgPool, &'a [SqlValue]) -> AdminActionFuture<'a> + Send + Sync>;
pub(crate) type AdminActionRegistry = HashMap<&'static str, HashMap<&'static str, AdminActionFn>>;
pub fn router(pool: PgPool) -> Router {
Builder::new(pool).build()
}
#[must_use]
pub struct Builder {
pool: PgPool,
config: Config,
}
#[derive(Clone, Default)]
pub(crate) struct Config {
pub(crate) title: Option<String>,
pub(crate) subtitle: Option<String>,
pub(crate) brand_name: Option<String>,
pub(crate) brand_tagline: Option<String>,
pub(crate) brand_logo_url: Option<String>,
pub(crate) theme_mode: Option<String>,
pub(crate) tenant_brand_css: Option<String>,
pub(crate) allowed_tables: Option<HashSet<String>>,
pub(crate) read_only_tables: HashSet<String>,
pub(crate) read_only_all: bool,
pub(crate) actions: AdminActionRegistry,
pub(crate) user_perms: Option<HashSet<String>>,
pub(crate) tenant_mode: bool,
}
impl Builder {
pub fn new(pool: PgPool) -> Self {
Self {
pool,
config: Config::default(),
}
}
pub fn show_only<I, S>(mut self, tables: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.config.allowed_tables = Some(tables.into_iter().map(Into::into).collect());
self
}
pub fn read_only<I, S>(mut self, tables: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.config
.read_only_tables
.extend(tables.into_iter().map(Into::into));
self
}
pub fn read_only_all(mut self) -> Self {
self.config.read_only_all = true;
self
}
#[must_use]
pub fn tenant_mode(mut self) -> Self {
self.config.tenant_mode = true;
self
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.config.title = Some(title.into());
self
}
pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
self.config.subtitle = Some(subtitle.into());
self
}
#[must_use]
pub fn brand_name(mut self, name: impl Into<String>) -> Self {
self.config.brand_name = Some(name.into());
self
}
#[must_use]
pub fn brand_tagline(mut self, tagline: impl Into<String>) -> Self {
self.config.brand_tagline = Some(tagline.into());
self
}
#[must_use]
pub fn brand_logo_url(mut self, url: impl Into<String>) -> Self {
self.config.brand_logo_url = Some(url.into());
self
}
#[must_use]
pub fn theme_mode(mut self, mode: impl Into<String>) -> Self {
self.config.theme_mode = Some(mode.into());
self
}
#[must_use]
pub fn tenant_brand_css(mut self, css: impl Into<String>) -> Self {
self.config.tenant_brand_css = Some(css.into());
self
}
pub fn with_user_perms<I: IntoIterator<Item = String>>(mut self, perms: I) -> Self {
self.config.user_perms = Some(perms.into_iter().collect());
self
}
pub fn register_action<F>(
mut self,
model_table: &'static str,
action_name: &'static str,
handler: F,
) -> Self
where
F: for<'a> Fn(&'a PgPool, &'a [SqlValue]) -> AdminActionFuture<'a> + Send + Sync + 'static,
{
self.config
.actions
.entry(model_table)
.or_default()
.insert(action_name, Arc::new(handler));
self
}
pub fn build(self) -> Router {
Router::new()
.route("/", get(views::index))
.route("/__audit", get(super::audit::audit_log_view))
.route("/__audit/cleanup", post(super::audit::audit_cleanup_submit))
.route(
"/{table}",
get(views::table_view).post(views::create_submit),
)
.route("/{table}/new", get(views::create_form))
.route("/{table}/__action", post(views::action_submit))
.route(
"/{table}/{pk}",
get(views::detail_view).post(views::update_submit),
)
.route("/{table}/{pk}/edit", get(views::edit_form))
.route("/{table}/{pk}/delete", post(views::delete_submit))
.with_state(AppState {
pool: self.pool,
config: Arc::new(self.config),
})
}
}
#[derive(Clone)]
pub(crate) struct AppState {
pub(crate) pool: PgPool,
pub(crate) config: Arc<Config>,
}
impl AppState {
pub(crate) fn is_visible(&self, table: &str) -> bool {
let allowlist_ok = self
.config
.allowed_tables
.as_ref()
.is_none_or(|allowed| allowed.contains(table));
if !allowlist_ok {
return false;
}
if let Some(perms) = &self.config.user_perms {
return perms.contains(&format!("{table}.view"));
}
true
}
pub(crate) fn scope_visible(&self, scope: crate::core::ModelScope) -> bool {
if !self.config.tenant_mode {
return true;
}
scope == crate::core::ModelScope::Tenant
}
pub(crate) fn is_read_only(&self, table: &str) -> bool {
if self.config.read_only_all || self.config.read_only_tables.contains(table) {
return true;
}
if let Some(perms) = &self.config.user_perms {
return !perms.contains(&format!("{table}.change"));
}
false
}
pub(crate) fn can_add(&self, table: &str) -> bool {
if self.config.read_only_all || self.config.read_only_tables.contains(table) {
return false;
}
if let Some(perms) = &self.config.user_perms {
return perms.contains(&format!("{table}.add"));
}
true
}
pub(crate) fn can_delete(&self, table: &str) -> bool {
if self.config.read_only_all || self.config.read_only_tables.contains(table) {
return false;
}
if let Some(perms) = &self.config.user_perms {
return perms.contains(&format!("{table}.delete"));
}
true
}
pub(crate) fn action_handler(&self, table: &str, action: &str) -> Option<AdminActionFn> {
self.config
.actions
.get(table)
.and_then(|m| m.get(action))
.cloned()
}
}
#[cfg(test)]
mod scope_filter_tests {
use super::*;
use crate::core::ModelScope;
use std::sync::Arc;
fn state_with(tenant_mode: bool) -> AppState {
let mut cfg = Config::default();
cfg.tenant_mode = tenant_mode;
AppState {
pool: PgPool::connect_lazy("postgres://_:_@127.0.0.1:1/_unused")
.expect("connect_lazy never fails"),
config: Arc::new(cfg),
}
}
#[tokio::test]
async fn standalone_admin_sees_every_scope() {
let state = state_with(false);
assert!(state.scope_visible(ModelScope::Tenant));
assert!(state.scope_visible(ModelScope::Registry));
}
#[tokio::test]
async fn tenant_admin_hides_registry_scoped_models() {
let state = state_with(true);
assert!(state.scope_visible(ModelScope::Tenant));
assert!(!state.scope_visible(ModelScope::Registry));
}
#[tokio::test]
async fn tenant_mode_setter_flips_flag() {
let pool = PgPool::connect_lazy("postgres://_:_@127.0.0.1:1/_unused")
.expect("connect_lazy never fails");
let builder = Builder::new(pool).tenant_mode();
assert!(builder.config.tenant_mode);
}
}