From c5f9660542612ba10ebf1b1adffeaaa90c7963a0 Mon Sep 17 00:00:00 2001 From: AbegaM Date: Wed, 13 Mar 2024 17:32:51 +0300 Subject: [PATCH] Create a role for the initial user --- src/controllers/auth.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/controllers/auth.js b/src/controllers/auth.js index 0315b2f..c8c6be0 100644 --- a/src/controllers/auth.js +++ b/src/controllers/auth.js @@ -666,7 +666,7 @@ const createInitialUser = async () => { const { hashedPassword, salt } = await hashPassword(password, 10); // create the initial user - rowService.save({ + const { lastInsertRowid: userId } = rowService.save({ tableName: USER_TABLE, fields: { username, @@ -676,6 +676,28 @@ const createInitialUser = async () => { }, }); + // get the default role from the DB + const roles = rowService.get({ + tableName: ROLE_TABLE, + whereString: 'WHERE name=?', + whereStringValues: [constantRoles.DEFAULT_ROLE], + }); + + if (roles.length <= 0) { + console.log( + 'Default role not found, please restart soul so a default role can be created', + ); + process.exit(1); + } + + const defaultRoleId = roles[0].id; + + // create a _users_role for the initial user + rowService.save({ + tableName: USERS_ROLES_TABLE, + fields: { user_id: userId, role_id: defaultRoleId }, + }); + console.log('Initial user created'); } else { console.log('Initial user is already created');