From d5c1361e64a207973ff82e92811c884d1fa3ed7c Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Sun, 24 Apr 2022 05:02:02 -0400 Subject: [PATCH] Fixed issue where roles were not properly initialized --- backend/authentication/auth.js | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/backend/authentication/auth.js b/backend/authentication/auth.js index de54a0b..7607c9b 100644 --- a/backend/authentication/auth.js +++ b/backend/authentication/auth.js @@ -18,10 +18,19 @@ let JWT_EXPIRATION = null; let opts = null; let saltRounds = null; -exports.initialize = function() { +exports.initialize = function () { /************************* * Authentication module ************************/ + + if (db_api.database_initialized) { + setupRoles(); + } else { + db_api.database_initialized_bs.subscribe(init => { + if (init) setupRoles(); + }); + } + saltRounds = 10; JWT_EXPIRATION = config_api.getConfigItem('ytdl_jwt_expiration'); @@ -49,6 +58,41 @@ exports.initialize = function() { })); } +const setupRoles = async () => { + const required_roles = { + admin: { + permissions: [ + 'filemanager', + 'settings', + 'subscriptions', + 'sharing', + 'advanced_download', + 'downloads_manager' + ] + }, + user: { + permissions: [ + 'filemanager', + 'subscriptions', + 'sharing' + ] + } + } + + const role_keys = Object.keys(required_roles); + for (let i = 0; i < role_keys.length; i++) { + const role_key = role_keys[i]; + const role_in_db = await db_api.getRecord('roles', {key: role_key}); + if (!role_in_db) { + // insert task metadata into table if missing + await db_api.insertRecordIntoTable('roles', { + key: role_key, + permissions: required_roles[role_key]['permissions'] + }); + } + } +} + exports.passport = require('passport'); exports.passport.serializeUser(function(user, done) {