Skip to main content

brep_plm/api/
settings.rs

1//! The administrator's policy settings. Every signed-in user reads them — the
2//! page needs to know whether "New revision" is open while a draft is — and
3//! only an administrator changes them.
4
5use axum::extract::State;
6use axum::http::HeaderMap;
7use axum::Json;
8
9use super::{require_admin, require_user, Shared};
10use crate::model::Settings;
11use crate::Error;
12
13pub async fn get(State(db): State<Shared>, headers: HeaderMap) -> Result<Json<serde_json::Value>, Error> {
14    require_user(&db, &headers)?;
15    Ok(Json(with_server_facts(&db, db.settings())))
16}
17
18/// The settings, plus what the server was started with that bears on them:
19/// whether `--lock-script-editor` overrides the editor setting.
20fn with_server_facts(db: &crate::db::Db, settings: Settings) -> serde_json::Value {
21    let mut value = serde_json::to_value(settings).unwrap_or_default();
22    if let Some(map) = value.as_object_mut() {
23        map.insert(
24            "script_editor_locked".into(),
25            db.security().config.lock_script_editor.into(),
26        );
27    }
28    value
29}
30
31/// Merge the given settings; unknown keys and non-boolean values are refused
32/// ([`crate::db::Db::update_settings`]).
33pub async fn update(
34    State(db): State<Shared>,
35    headers: HeaderMap,
36    Json(body): Json<serde_json::Value>,
37) -> Result<Json<serde_json::Value>, Error> {
38    require_admin(&db, &headers)?;
39    let settings = db.update_settings(&body)?;
40    Ok(Json(with_server_facts(&db, settings)))
41}