From f342cc480abf55d521197db412f784ea86185a88 Mon Sep 17 00:00:00 2001 From: rbisson <remi.bisson@inrae.fr> Date: Mon, 4 Mar 2024 12:59:48 +0100 Subject: [PATCH] [userService.js]: replaced strings by env variables --- app/dal/userService.js | 45 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/app/dal/userService.js b/app/dal/userService.js index 9514e34..e03b4ce 100644 --- a/app/dal/userService.js +++ b/app/dal/userService.js @@ -29,7 +29,6 @@ const keycloakAdmin = new KcAdminClient({ }) async function auth() { - await keycloakAdmin.auth({ username: process.env.KEYCLOAK_USERNAME, password: process.env.KEYCLOAK_PASSWORD, @@ -74,7 +73,7 @@ const UserService = { await auth(); const users = await keycloakAdmin.users.find({ email: ctx.email, - realm: 'in-sylva', + realm: process.env.KEYCLOAK_REALM, }); if (users.length > 0) { const user = users[0]; @@ -99,7 +98,7 @@ const UserService = { type: 'password', value: password, }, - realm: 'in-sylva', + realm: process.env.KEYCLOAK_REALM, }); this.sendMail({ ...ctx, subject: "Password reset", message: `Your new password is ${password}` }); t.commit(); @@ -126,7 +125,7 @@ const UserService = { try { await auth() const users = await keycloakAdmin.users.find({ - realm: 'in-sylva' + realm: process.env.KEYCLOAK_REALM }) return users } @@ -142,7 +141,7 @@ const UserService = { await auth() const userId = ctx.id keycloakAdmin.setConfig({ - realmName: 'in-sylva' + realmName: process.env.KEYCLOAK_REALM }) await keycloakAdmin.users.update( @@ -156,7 +155,7 @@ const UserService = { }) const user = await keycloakAdmin.users.findOne({ - id: userId, realm: 'in-sylva', + id: userId, realm: process.env.KEYCLOAK_REALM, }) await db('users').where('kc_id', userId) @@ -203,10 +202,9 @@ const UserService = { async user(ctx) { try { await auth() - const user = await keycloakAdmin.users.findOne({ - id: ctx.id, realm: 'in-sylva', + return await keycloakAdmin.users.findOne({ + id: ctx.id, realm: process.env.KEYCLOAK_REALM, }) - return user } catch (err) { ctx.status = err.status || 500 ctx.body = err.message @@ -219,7 +217,6 @@ const UserService = { if (!ctx) { throw Error("The request body is empty!") } - if (!ctx.email) { throw Error("The email field is empty!") } @@ -230,9 +227,7 @@ const UserService = { kc_id: kc_id } }) - return { kcId: kc_id, role: role } - } catch (err) { ctx.status = err.status || 500 ctx.body = err.message @@ -245,13 +240,10 @@ const UserService = { if (!ctx) { throw Error("The request body is empty!") } - const user = await db.select("*").from('users').where('kc_id', ctx.kcId).timeout(1000, { cancel: true }).then((result) => result[0]) - return user // To-do // Return user details such as roles and policies - } catch (error) { ctx.status = error.status || 500 ctx.body = error.message @@ -263,11 +255,9 @@ const UserService = { const t = await postgresSeq.transaction() try { await auth() - if (!ctx) { throw Error("The request body is empty!") } - if (ctx.username && ctx.email && ctx.password) { const checkUser = await User.findAndCountAll({ where: { @@ -277,33 +267,26 @@ const UserService = { ] } }) - if (checkUser.count > 0) return { user: null, status: 409, message: `"${ctx.email}" or "${ctx.username}" already exist, please try again with different credentials.` } - const roleId = (ctx.roleId == 0 || ctx.roleId == undefined) ? 3 : ctx.roleId - if (!!roleId && roleId == 0) throw Error("The roleId is empty!") - // Insert this new user into insylva db. const newUser = await User.create({ username: ctx.username, email: ctx.email, password: ctx.password }) - if (newUser.id) { - // Insert new user into keycloak db const kcUser = await keycloakAdmin.users.create({ username: ctx.username, email: ctx.email, emailVerified: true, enabled: true, - realm: "in-sylva", + realm: process.env.KEYCLOAK_REALM, }) - if (kcUser.id) { // Set new user's password await keycloakAdmin.users.resetPassword({ @@ -313,9 +296,8 @@ const UserService = { type: 'password', value: ctx.password }, - realm: "in-sylva", + realm: process.env.KEYCLOAK_REALM, }) - const update = await User.update({ kc_id: kcUser.id }, { @@ -323,7 +305,6 @@ const UserService = { id: newUser.id } }) - if (update) { // User Role allocation, default user is normal-user await RoleUser.create({ @@ -336,15 +317,12 @@ const UserService = { } else { await t.rollback() } - await t.commit() const date = new Date().toLocaleDateString() const message = `[INFO]:[User ${ctx.username} is created with this email address: ${ctx.email} successfully.]:[${date}]` const subject = "In-Sylva New User Added" - await mailService.send(process.env.IN_SYLVA_EMAIL_FROM, process.env.IN_SYLVA_EMAIL_TO, subject, "", message) await botService.message(message) - return { user: newUser, status: 201 } } } catch (error) { @@ -358,7 +336,6 @@ const UserService = { if (!ctx) { throw Error("The request body is empty!") } - console.log(ctx) if (ctx.subject && ctx.message) { const subject = ctx.subject const message = ctx.message @@ -421,7 +398,7 @@ const UserService = { email: process.env.IN_SYLVA_ADMIN_USERNAME, emailVerified: true, enabled: true, - realm: "in-sylva", + realm: process.env.KEYCLOAK_REALM, }) // Set new user's password if (kc_user) { @@ -432,7 +409,7 @@ const UserService = { type: 'password', value: process.env.IN_SYLVA_ADMIN_PASSWORD }, - realm: "in-sylva", + realm: process.env.KEYCLOAK_REALM, }) } -- GitLab