diff --git a/README.md b/README.md index c9eff8b..0099f07 100644 --- a/README.md +++ b/README.md @@ -3593,7 +3593,9 @@ not the old one) — never as who they used to be. Without it the principal is the internal role **`ai`**: the lowest privilege in the system, below `parent`. It is not a human role and is never assignable — `"ai"` is rejected by every role input, no account can hold it, and it is never -stored. It appears in the OpenAPI `Role` schema as documentation only. So a +stored. It appears in the OpenAPI *response* schema `Role` as documentation +only — request bodies take `AssignableRole`, the five human roles, so no +generated client or Swagger form offers it. So a read that needs a role answers `403` — through the `ok` envelope, with that status, like any other API refusal. diff --git a/src/web/dto.rs b/src/web/dto.rs index 1a0b241..b89769f 100644 --- a/src/web/dto.rs +++ b/src/web/dto.rs @@ -15,17 +15,20 @@ use crate::domain::subject::Subject; use crate::domain::user::{User, UserId}; use crate::error::AppError; -/// The access roles, lowest to highest privilege (`parent` is a read-only -/// observer of its linked students). The web-facing mirror of -/// [`crate::domain::role::Role`] — it carries the serde + OpenAPI derives (which -/// the domain type deliberately omits), so it renders as a proper `enum` in the -/// docs. Serializes to the same lowercase strings the domain stores. +/// The access roles a response can carry, lowest to highest privilege (`parent` +/// is a read-only observer of its linked students). `ai` is an internal service +/// principal — the identity an out-of-process AI service carries over the QUIC +/// bridge. It is never assignable and never stored on a user row, so it only +/// ever appears in a response; requests take the five human roles +/// (`AssignableRole`), which is also what `GET /limits` lists. +/// +/// The web-facing mirror of [`crate::domain::role::Role`] — it carries the serde +/// + OpenAPI derives (which the domain type deliberately omits), so it renders +/// as a proper `enum` in the docs. Serializes to the same lowercase strings the +/// domain stores. #[derive(Serialize, Clone, Copy, ToSchema)] #[serde(rename_all = "lowercase")] pub enum Role { - /// Internal service principal — never assignable, never stored. It appears - /// here only because the mapping from the domain enum is exhaustive; the - /// assignable set is what `GET /limits` lists, and that omits it. Ai, Parent, Student, @@ -34,6 +37,24 @@ pub enum Role { Admin, } +/// The roles a request may ask for: the five human ones, exactly +/// [`crate::constant::ROLES`]. Documentation only — the handlers still take the +/// role as a string and funnel it through +/// [`crate::domain::role::Role::try_from_str`], so an unknown value is a uniform +/// `400` rather than a deserialization error. It exists because [`Role`] (the +/// response schema) also names `ai`, and a request schema pointed at it offered +/// callers a value the server has always refused. +#[derive(ToSchema)] +#[schema(rename_all = "lowercase")] +#[allow(dead_code)] // Nothing constructs it: the variants exist to be emitted. +pub enum AssignableRole { + Parent, + Student, + Teacher, + Manager, + Admin, +} + impl From for Role { fn from(role: DomainRole) -> Self { match role { diff --git a/src/web/users.rs b/src/web/users.rs index a03c327..6c6a50d 100644 --- a/src/web/users.rs +++ b/src/web/users.rs @@ -25,6 +25,7 @@ use crate::error::{AppError, ErrorResponse, ValidationError}; use crate::state::AppState; use super::courses::visible_courses; +use super::dto::AssignableRole; use super::dto::Role as RoleSchema; use super::{ CurrentUser, Page, PageParams, PersonRef, RequireAdmin, RequireTeacher, UploadFileForm, @@ -63,8 +64,10 @@ pub fn routes() -> OpenApiRouter { #[derive(Deserialize, ToSchema)] struct SetRole { /// The role to assign. Deserialized as a string so an unknown value returns a - /// uniform `400`; documented as the `Role` enum so the docs list the choices. - #[schema(value_type = RoleSchema, example = "teacher")] + /// uniform `400`; documented as the `AssignableRole` enum so the docs list + /// exactly the choices the server accepts (the response-side `Role` also + /// names the `ai` service principal, which this endpoint has always refused). + #[schema(value_type = AssignableRole, example = "teacher")] role: String, } diff --git a/tests/integration.rs b/tests/integration.rs index 8bd492c..ce3c4aa 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -5265,6 +5265,39 @@ async fn role_ai_is_unreachable_over_http() { let roles = roles.as_array().expect("limits user.roles array"); assert_eq!(roles.len(), 5, "{roles:?}"); assert!(!roles.iter().any(|r| r == "ai"), "{roles:?}"); + + // 6. Neither does the OpenAPI document Swagger UI renders its forms from. + // The request schema once `$ref`ed the response-side `Role`, which does + // name `ai` — so the try-it-out form offered a value the funnel above has + // always refused with a 400. Request schemas carry the five; the response + // schema keeps `ai` (a service principal really does read back as one) + // and must say what it is, since utoipa emits only the *type*-level doc + // comment, never a variant's. + let spec = send(&app, "GET", "/api-docs/openapi.json", None, None) + .await + .body; + let schemas = &spec["components"]["schemas"]; + let human = json!(["parent", "student", "teacher", "manager", "admin"]); + assert_eq!(schemas["AssignableRole"]["enum"], human, "{schemas:?}"); + assert_eq!( + spec["paths"]["/users/{id}/role"]["patch"]["requestBody"]["content"] + ["application/json"]["schema"]["$ref"], + "#/components/schemas/SetRole" + ); + assert_eq!( + schemas["SetRole"]["properties"]["role"]["$ref"], + "#/components/schemas/AssignableRole" + ); + let response_role = &schemas["Role"]; + assert_eq!( + response_role["enum"], + json!(["ai", "parent", "student", "teacher", "manager", "admin"]), + "{response_role:?}" + ); + let described = response_role["description"] + .as_str() + .expect("Role schema carries its type-level doc comment"); + assert!(described.contains("never assignable"), "{described:?}"); } // --- users: personal info (profile) ---------------------------------------