Skip to content

Commit 62c2814

Browse files
committed
editoast: harmonize builtin roles serializations
Signed-off-by: Leo Valais <[email protected]>
1 parent eb41a06 commit 62c2814

File tree

6 files changed

+11
-81
lines changed

6 files changed

+11
-81
lines changed

editoast/editoast_authz/src/builtin_role.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,49 @@
1+
use serde::Deserialize;
12
use serde::Serialize;
23
use strum::AsRefStr;
3-
use strum::Display;
44
use strum::EnumString;
55
use utoipa::ToSchema;
66

77
use crate::roles::BuiltinRoleSet;
88

99
#[derive(
10-
Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, EnumString, AsRefStr, Display, ToSchema,
10+
Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, EnumString, AsRefStr, ToSchema,
1111
)]
12-
#[strum(serialize_all = "snake_case")]
1312
pub enum BuiltinRole {
1413
/// A user with this role short-circuits all role and permission checks
1514
///
1615
/// Alternatively, especially for development, the `EDITOAST_SUPERUSER` environment variable can be set
1716
/// when no user identity header is present. (This is the case when editoast is queried directly and
1817
/// not through the gateway.)
19-
#[strum(serialize = "superuser")]
2018
Superuser,
2119

22-
#[strum(serialize = "operational_studies:write")]
2320
OpsWrite,
24-
#[strum(serialize = "operational_studies:read")]
2521
OpsRead,
2622

27-
#[strum(serialize = "infra:read")]
2823
InfraRead,
29-
#[strum(serialize = "infra:write")]
3024
InfraWrite,
3125

32-
#[strum(serialize = "rolling_stock_collection:read")]
3326
RollingStockCollectionRead,
34-
#[strum(serialize = "rolling_stock_collection:write")]
3527
RollingStockCollectionWrite,
3628

37-
#[strum(serialize = "work_schedule:write")]
3829
WorkScheduleWrite,
39-
#[strum(serialize = "work_schedule:read")]
4030
WorkScheduleRead,
4131

42-
#[strum(serialize = "map:read")]
4332
MapRead,
4433

45-
#[strum(serialize = "stdcm")]
4634
Stdcm,
47-
#[strum(serialize = "stdcm:admin")]
4835
StdcmAdmin,
4936

50-
#[strum(serialize = "timetable:read")]
5137
TimetableRead,
52-
#[strum(serialize = "timetable:write")]
5338
TimetableWrite,
5439

55-
#[strum(serialize = "document:read")]
5640
DocumentRead,
57-
#[strum(serialize = "document:write")]
5841
DocumentWrite,
5942

60-
#[strum(serialize = "subject:read")]
6143
SubjectRead,
62-
#[strum(serialize = "subject:write")]
6344
SubjectWrite,
6445

65-
#[strum(serialize = "role:read")]
6646
RoleRead,
67-
#[strum(serialize = "role:write")]
6847
RoleWrite,
6948
}
7049

editoast/openapi.yaml

+2-22
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ paths:
7575
roles:
7676
type: array
7777
items:
78-
type: string
78+
$ref: '#/components/schemas/BuiltinRole'
7979
required: true
8080
responses:
8181
'204':
@@ -102,7 +102,7 @@ paths:
102102
roles:
103103
type: array
104104
items:
105-
type: string
105+
$ref: '#/components/schemas/BuiltinRole'
106106
required: true
107107
responses:
108108
'204':
@@ -4082,7 +4082,6 @@ components:
40824082
- $ref: '#/components/schemas/EditoastInfraApiErrorNotFound'
40834083
- $ref: '#/components/schemas/EditoastInfraCacheEditoastErrorObjectNotFound'
40844084
- $ref: '#/components/schemas/EditoastInfraStateErrorFetchError'
4085-
- $ref: '#/components/schemas/EditoastInvalidRoleTagInvalid'
40864085
- $ref: '#/components/schemas/EditoastLayersErrorLayerNotFound'
40874086
- $ref: '#/components/schemas/EditoastLayersErrorViewNotFound'
40884087
- $ref: '#/components/schemas/EditoastLinesErrorsLineNotFound'
@@ -4369,25 +4368,6 @@ components:
43694368
type: string
43704369
enum:
43714370
- editoast:infra_state:FetchError
4372-
EditoastInvalidRoleTagInvalid:
4373-
type: object
4374-
required:
4375-
- type
4376-
- status
4377-
- message
4378-
properties:
4379-
context:
4380-
type: object
4381-
message:
4382-
type: string
4383-
status:
4384-
type: integer
4385-
enum:
4386-
- 400
4387-
type:
4388-
type: string
4389-
enum:
4390-
- editoast:authz:role:Invalid
43914371
EditoastLayersErrorLayerNotFound:
43924372
type: object
43934373
required:

editoast/src/views/authz.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::HashSet;
2-
use std::str::FromStr;
32

43
use crate::error::Result;
54
use crate::models::auth::{AuthDriverError, PgAuthDriver};
@@ -120,15 +119,7 @@ async fn list_user_roles(
120119

121120
#[derive(serde::Deserialize, utoipa::ToSchema)]
122121
struct RoleListBody {
123-
roles: Vec<String>,
124-
}
125-
126-
#[derive(Debug, thiserror::Error, EditoastError)]
127-
#[editoast_error(base_id = "authz:role")]
128-
enum InvalidRoleTag {
129-
#[error("Invalid role tag: {0}")]
130-
#[editoast_error(status = 400)]
131-
Invalid(String),
122+
roles: Vec<BuiltinRole>,
132123
}
133124

134125
#[utoipa::path(
@@ -155,15 +146,8 @@ async fn grant_roles(
155146

156147
check_user_exists(user_id, &authorizer).await?;
157148

158-
let roles = roles
159-
.iter()
160-
.map(|role| {
161-
BuiltinRole::from_str(role.as_str())
162-
.map_err(|_| InvalidRoleTag::Invalid(role.to_owned()))
163-
})
164-
.collect::<Result<_, _>>()?;
165149
authorizer
166-
.grant_roles(user_id, roles)
150+
.grant_roles(user_id, HashSet::from_iter(roles))
167151
.await
168152
.map_err(AuthzError::from)?;
169153
Ok(axum::http::StatusCode::NO_CONTENT)
@@ -193,15 +177,8 @@ async fn strip_roles(
193177

194178
check_user_exists(user_id, &authorizer).await?;
195179

196-
let roles = roles
197-
.iter()
198-
.map(|role| {
199-
BuiltinRole::from_str(role.as_str())
200-
.map_err(|_| InvalidRoleTag::Invalid(role.to_owned()))
201-
})
202-
.collect::<Result<_, _>>()?;
203180
authorizer
204-
.strip_roles(user_id, roles)
181+
.strip_roles(user_id, HashSet::from_iter(roles))
205182
.await
206183
.map_err(AuthzError::from)?;
207184
Ok(axum::http::StatusCode::NO_CONTENT)

front/public/locales/en/errors.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
"Driver": "Authentication/authorization internal error, try again or contact support",
2424
"NoSuchUser": "Unknown user",
2525
"Unauthenticated": "Unauthenticated user",
26-
"Unauthorized": "Access denied",
27-
"role": {
28-
"Invalid": "Invalid role tag"
29-
}
26+
"Unauthorized": "Access denied"
3027
},
3128
"auto_fixes": {
3229
"ConflictingFixesOnSameObject": "Conflicting fixes for the same object on the same fix-iteration",

front/public/locales/fr/errors.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
"Driver": "Erreur interne d'authentification ou d'autorisation, veuillez réessayer ou contacter le support",
2424
"NoSuchUser": "Utilisateur inconnu",
2525
"Unauthenticated": "Utilisateur non authentifié",
26-
"Unauthorized": "Accès refusé",
27-
"role": {
28-
"Invalid": "Libellé de rôle inconnu"
29-
}
26+
"Unauthorized": "Accès refusé"
3027
},
3128
"auto_fixes": {
3229
"ConflictingFixesOnSameObject": "Correctifs conflictuels pour le même objet sur la même itération de correctif",

front/src/common/api/generatedEditoastApi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,15 @@ export type PostAuthzRolesByUserIdApiArg = {
889889
/** A user ID (not to be mistaken for its identity, cf. editoast user model documentation) */
890890
userId: number;
891891
body: {
892-
roles: string[];
892+
roles: BuiltinRole[];
893893
};
894894
};
895895
export type DeleteAuthzRolesByUserIdApiResponse = unknown;
896896
export type DeleteAuthzRolesByUserIdApiArg = {
897897
/** A user ID (not to be mistaken for its identity, cf. editoast user model documentation) */
898898
userId: number;
899899
body: {
900-
roles: string[];
900+
roles: BuiltinRole[];
901901
};
902902
};
903903
export type PostDocumentsApiResponse =

0 commit comments

Comments
 (0)