From 02ccb279f7a908febb53496dd1e357f17f756ce0 Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Fri, 26 May 2023 10:58:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=83=20SSO=20users=20sync=20api=20(#60)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../applications-api/web/controllers/index.ts | 50 +++++++++++++++++++ .../services/applications-api/web/routes.ts | 9 ++++ 2 files changed, 59 insertions(+) diff --git a/tdrive/backend/node/src/services/applications-api/web/controllers/index.ts b/tdrive/backend/node/src/services/applications-api/web/controllers/index.ts index 709925db..22daf1fd 100644 --- a/tdrive/backend/node/src/services/applications-api/web/controllers/index.ts +++ b/tdrive/backend/node/src/services/applications-api/web/controllers/index.ts @@ -8,6 +8,7 @@ import { RealtimeBaseBusEvent, } from "../../../../core/platform/services/realtime/types"; import { ResourceGetResponse } from "../../../../utils/types"; +import { getInstance } from "../../../user/entities/user"; import { ApplicationObject, getApplicationObject, @@ -163,6 +164,55 @@ export class ApplicationsApiController { }, ); } + + async syncUsers( + request: FastifyRequest<{ + Body: { + email: string; + first_name: string; + last_name: string; + application_id: string; + company_id: string; + }; + }>, + ): Promise { + const email = request.body.email.trim().toLocaleLowerCase(); + const checkApplication = gr.services.applications.companyApps.get({ + application_id: request.body.application_id, + company_id: request.body.company_id, + }); + + if (!checkApplication) { + throw new Error("Application is not allowed to sync users for this company."); + } + + if (await gr.services.users.getByEmail(email)) { + throw new Error("This email is already used"); + } + + try { + const newUser = getInstance({ + first_name: request.body.first_name, + last_name: request.body.last_name, + email_canonical: email, + username_canonical: (email.replace("@", ".") || "").toLocaleLowerCase(), + phone: "", + identity_provider: "console", + identity_provider_id: email, + mail_verified: true, + }); + const user = await gr.services.users.create(newUser); + + await gr.services.companies.setUserRole(request.body.company_id, user.entity.id, "admin"); + + await gr.services.users.save(user.entity, { + user: { id: user.entity.id, server_request: true }, + }); + } catch (err) { + throw new Error("An unknown error occured"); + } + return {}; + } } function getExecutionContext(request: FastifyRequest): ApplicationApiExecutionContext { diff --git a/tdrive/backend/node/src/services/applications-api/web/routes.ts b/tdrive/backend/node/src/services/applications-api/web/routes.ts index 3af81dbb..7d47c76e 100644 --- a/tdrive/backend/node/src/services/applications-api/web/routes.ts +++ b/tdrive/backend/node/src/services/applications-api/web/routes.ts @@ -48,6 +48,15 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next) handler: (request, reply) => controller.proxy.bind(controller)(request, reply, fastify), }); + // Sync SSO users + fastify.route({ + method: "POST", + url: "/sync", + preValidation: [fastify.authenticate], + preHandler: [checkApplication], + handler: controller.syncUsers.bind(controller), + }); + next(); };