Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserSupervisorRole } from "@domifa/common";
import { USER_SUPERVISOR_ROLES } from "./USER_SUPERVISOR_ROLES.const";

// `super-admin-domifa` is assigned out-of-band (SQL migration), never via UI.
// Roles reachable by editing an existing supervisor. `super-admin-domifa` is
// only granted at creation, where the email domain is checked.
export const USER_SUPERVISOR_ASSIGNABLE_ROLES: UserSupervisorRole[] =
USER_SUPERVISOR_ROLES.filter((r) => r !== "super-admin-domifa");
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// @index('./*.ts', f => `export * from '${f.path}'`)
export * from "./isSuperAdminEmailDecorator";
export * from "./isValidGeographicDecorator";
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isSuperAdminEmailDomain } from "@domifa/common";
import {
registerDecorator,
ValidationOptions,
ValidationArguments,
} from "class-validator";

// A `super-admin-domifa` account must use one of SUPER_ADMIN_EMAIL_DOMAINS.
// Other roles are not restricted.
export function IsValidSuperAdminEmail(validationOptions?: ValidationOptions) {
return function (object: object, propertyName: string) {
registerDecorator({
name: "isValidSuperAdminEmail",
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate(value: unknown, args: ValidationArguments) {
const role = (args.object as { role?: string }).role;
if (role !== "super-admin-domifa") {
return true;
}
return typeof value === "string" && isSuperAdminEmailDomain(value);
},
defaultMessage() {
return "Cet email ne peut pas être utilisé pour un compte super-admin-domifa";
},
},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
MinLength,
} from "class-validator";

import { IsValidGeographicRole } from "../decorators";
import { IsValidGeographicRole, IsValidSuperAdminEmail } from "../decorators";
import { UserSupervisorRole } from "@domifa/common";
import {
LowerCaseTransform,
StripTagsTransform,
TERRITORY_CODES,
} from "../../../_common/decorators";
import { USER_SUPERVISOR_ASSIGNABLE_ROLES } from "../../../_common/model/users/user-supervisor";
import { USER_SUPERVISOR_ROLES } from "../../../_common/model/users/user-supervisor";

export class RegisterUserSupervisorDto {
@MinLength(2)
Expand All @@ -37,10 +37,11 @@ export class RegisterUserSupervisorDto {
@IsNotEmpty()
@IsEmail()
@LowerCaseTransform()
@IsValidSuperAdminEmail()
public email!: string;

@IsNotEmpty()
@IsIn(USER_SUPERVISOR_ASSIGNABLE_ROLES)
@IsIn(USER_SUPERVISOR_ROLES)
public role!: UserSupervisorRole;

@IsArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
import { RegisterUserSupervisorDto } from "../register-user-supervisor.dto";

// Le rôle `super-admin-domifa` est volontairement absent du DTO. Sa
// création/promotion se fait hors UI (migration SQL).
// `super-admin-domifa` can be created only with an email from
// SUPER_ADMIN_EMAIL_DOMAINS.
describe("RegisterUserSupervisorDto — super-admin-domifa role assignment", () => {
const createBaseDto = (): RegisterUserSupervisorDto => {
const dto = new RegisterUserSupervisorDto();
Expand All @@ -14,6 +14,8 @@ describe("RegisterUserSupervisorDto — super-admin-domifa role assignment", ()
dto.territories = [];
return dto;
};
const emailErrors = async (dto: RegisterUserSupervisorDto) =>
(await validate(dto)).filter((e) => e.property === "email");

it("accepts a valid `national` role", async () => {
const dto = createBaseDto();
Expand All @@ -34,26 +36,45 @@ describe("RegisterUserSupervisorDto — super-admin-domifa role assignment", ()
}
});

it("rejects `super-admin-domifa` even with a valid social.gouv email", async () => {
it("does not restrict the email domain for other roles", async () => {
expect(await emailErrors(createBaseDto())).toHaveLength(0);
});

it.each([
"john.smith@fabrique.social.gouv.fr",
"john.smith@externes.sg.social.gouv.fr",
"john.smith@social.gouv.fr",
])("accepts `super-admin-domifa` with %s", async (email) => {
const dto = createBaseDto();
dto.email = "john.smith@fabrique.social.gouv.fr";
dto.email = email;
dto.role = "super-admin-domifa";

const errors = await validate(dto);
const roleError = errors.find((e) => e.property === "role");
expect(await validate(dto)).toHaveLength(0);
});

expect(roleError).toBeDefined();
expect(roleError?.constraints?.isIn).toBeDefined();
it("accepts an allowed domain regardless of case", async () => {
const dto = plainToInstance(RegisterUserSupervisorDto, {
...createBaseDto(),
email: "John.Smith@Social.Gouv.FR",
role: "super-admin-domifa",
});

expect(await validate(dto)).toHaveLength(0);
});

it("rejects `super-admin-domifa` with any email", async () => {
it.each([
"john.smith@example.com",
"john.smith@sg.social.gouv.fr",
"john.smith@social.gouv.fr.evil.com",
"john.smith@fakesocial.gouv.fr",
])("rejects `super-admin-domifa` with %s", async (email) => {
const dto = createBaseDto();
dto.email = email;
dto.role = "super-admin-domifa";

const errors = await validate(dto);
const roleError = errors.find((e) => e.property === "role");

expect(roleError).toBeDefined();
const errors = await emailErrors(dto);
expect(errors).toHaveLength(1);
expect(errors[0].constraints?.isValidSuperAdminEmail).toBeDefined();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Only these email domains may hold the `super-admin-domifa` role.
export const SUPER_ADMIN_EMAIL_DOMAINS = [
"fabrique.social.gouv.fr",
"externes.sg.social.gouv.fr",
"social.gouv.fr",
];

export const isSuperAdminEmailDomain = (email: string): boolean => {
const lowerEmail = email.toLowerCase();
return SUPER_ADMIN_EMAIL_DOMAINS.some((domain) =>
lowerEmail.endsWith(`@${domain}`)
);
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// @index('./*', f => `export * from '${f.path}'`)
export * from "./USER_SUPERVISOR_ROLES_LABELS.const";
export * from "./SUPER_ADMIN_EMAIL_DOMAINS.const";
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import { CustomToastService } from "../../../shared/services";

import {
DEPARTEMENTS_LISTE,
isSuperAdminEmailDomain,
REGIONS_LISTE,
RegionsLabels,
SUPER_ADMIN_EMAIL_DOMAINS,
USER_SUPERVISOR_ROLES_LABELS,
UserSupervisor,
UserSupervisorRole,
Expand All @@ -40,11 +42,6 @@ import { ManageUsersService } from "../../services/manage-users.service";
import { ButtonComponent } from "../../../shared/components/button/button.component";
import { SortArrayPipe } from "../../../shared/pipes";

const SUPER_ADMIN_EMAIL_DOMAINS = [
"fabrique.social.gouv.fr",
"externes.sg.social.gouv.fr",
];

@Component({
animations: [fadeInOut],
selector: "app-register-user-supervisor",
Expand Down Expand Up @@ -187,10 +184,7 @@ export class RegisterUserSupervisorComponent implements OnInit, OnDestroy {
const role = roleControl.value;

if (role === "super-admin-domifa" && email) {
const isAllowedDomain = SUPER_ADMIN_EMAIL_DOMAINS.some((domain) =>
email.toLowerCase().endsWith(`@${domain}`)
);
if (!isAllowedDomain) {
if (!isSuperAdminEmailDomain(email)) {
return { invalidSuperAdminEmail: true };
}
}
Expand Down
Loading