From 87771ea8d07df68311d294e8041a9ab78db64f53 Mon Sep 17 00:00:00 2001 From: Thomas Taylor Date: Mon, 20 Mar 2023 15:41:03 +0000 Subject: [PATCH] Enable eslint and standard ruleset --- .eslintignore | 1 + .eslintrc | 14 ++++++ index.js | 4 +- lib/RolesModule.js | 121 +++++++++++++++++++++++---------------------- package.json | 4 ++ 5 files changed, 84 insertions(+), 60 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +node_modules diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..b56d919 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,14 @@ +{ + "env": { + "browser": false, + "node": true, + "commonjs": false, + "es2020": true + }, + "extends": [ + "standard" + ], + "parserOptions": { + "ecmaVersion": 2020 + } +} diff --git a/index.js b/index.js index 2e68e74..dbf291d 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -/** +/** * User role functionality * @namespace roles */ -export { default } from './lib/RolesModule.js'; +export { default } from './lib/RolesModule.js' diff --git a/lib/RolesModule.js b/lib/RolesModule.js index 7ed51ff..e7426bd 100644 --- a/lib/RolesModule.js +++ b/lib/RolesModule.js @@ -1,4 +1,4 @@ -import AbstractApiModule from 'adapt-authoring-api'; +import AbstractApiModule from 'adapt-authoring-api' /** * Module which handles user roles * @memberof roles @@ -6,106 +6,111 @@ import AbstractApiModule from 'adapt-authoring-api'; */ class RolesModule extends AbstractApiModule { /** @override */ - async init() { - await super.init(); + async init () { + await super.init() try { - await this.initConfigRoles(); + await this.initConfigRoles() - const hasRoles = this.getConfig('defaultRolesForAuthTypes').length || this.getConfig('defaultRoles').length; - if(hasRoles) await this.initDefaultRoles(); - } catch(e) { - this.log('error', e); + const hasRoles = this.getConfig('defaultRolesForAuthTypes').length || this.getConfig('defaultRoles').length + if (hasRoles) await this.initDefaultRoles() + } catch (e) { + this.log('error', e) } - const [localauth, users] = await this.app.waitForModule('localauth', 'users'); - localauth.registerHook.tap(this.onUpdateRoles.bind(this)); - users.requestHook.tap(this.onUpdateRoles.bind(this)); + const [localauth, users] = await this.app.waitForModule('localauth', 'users') + localauth.registerHook.tap(this.onUpdateRoles.bind(this)) + users.requestHook.tap(this.onUpdateRoles.bind(this)) } + /** * Adds any role definitions from the current config file to the database * @return {Promise} */ - async initConfigRoles() { - const mongodb = await this.app.waitForModule('mongodb'); + async initConfigRoles () { + const mongodb = await this.app.waitForModule('mongodb') return Promise.allSettled(this.getConfig('roleDefinitions').map(async r => { - const [doc] = await this.find({ shortName: r.shortName }); - if(doc) { + const [doc] = await this.find({ shortName: r.shortName }) + if (doc) { try { - await mongodb.replace(this.collectionName, { _id: doc._id }, r); - this.log('debug', 'REPLACE', this.schemaName, r.shortName); - } catch(e) { - if(e.code !== 11000) this.log('warn', `failed to update '${r.shortName}' role, ${e.message}`); + await mongodb.replace(this.collectionName, { _id: doc._id }, r) + this.log('debug', 'REPLACE', this.schemaName, r.shortName) + } catch (e) { + if (e.code !== 11000) this.log('warn', `failed to update '${r.shortName}' role, ${e.message}`) } - return; + return } try { - await this.insert(r); - this.log('debug', 'INSERT', this.schemaName, r.shortName); - } catch(e) { - if(e.code !== 11000) this.log('warn', `failed to add '${r.shortName}' role, ${e.message}`); + await this.insert(r) + this.log('debug', 'INSERT', this.schemaName, r.shortName) + } catch (e) { + if (e.code !== 11000) this.log('warn', `failed to add '${r.shortName}' role, ${e.message}`) } - })); + })) } + /** * Adds the specified default roles during new user creation * @return {Promise} */ - async shortNamesToIds(roles) { + async shortNamesToIds (roles) { return Promise.all(roles.map(async r => { - let role = this.roleCache[r]; - if(!role) { - [role] = await this.find({ shortName: r }); - this.roleCache[r] = role; + let role = this.roleCache[r] + if (!role) { + [role] = await this.find({ shortName: r }) + this.roleCache[r] = role } - return role._id.toString(); - })); + return role._id.toString() + })) } + /** * Handles setting defined default roles when new users are added * @return {Promise} */ - async initDefaultRoles() { + async initDefaultRoles () { /** * Local store of roles * @type {Object} */ - this.roleCache = {}; + this.roleCache = {} - const rolesforAll = await this.shortNamesToIds(this.getConfig('defaultRoles')); - const rolesForAuth = Object.entries(this.getConfig('defaultRolesForAuthTypes')).reduce((m,[k,v]) => { - return { [m[k]]: this.shortNamesToIds(v) }; - }, {}); - const users = await this.app.waitForModule('users'); + const rolesforAll = await this.shortNamesToIds(this.getConfig('defaultRoles')) + const rolesForAuth = Object.entries(this.getConfig('defaultRolesForAuthTypes')).reduce((m, [k, v]) => { + return { [m[k]]: this.shortNamesToIds(v) } + }, {}) + const users = await this.app.waitForModule('users') users.preInsertHook.tap(data => { - if(!data.roles || !data.roles.length) { - data.roles = rolesForAuth[data.authType] || rolesforAll || []; + if (!data.roles || !data.roles.length) { + data.roles = rolesForAuth[data.authType] || rolesforAll || [] } - }); + }) } + /** @override */ - async setValues() { - /** @ignore */ this.root = 'roles'; - /** @ignore */ this.schemaName = 'role'; - /** @ignore */ this.collectionName = 'roles'; - this.useDefaultRouteConfig(); + async setValues () { + /** @ignore */ this.root = 'roles' + /** @ignore */ this.schemaName = 'role' + /** @ignore */ this.collectionName = 'roles' + this.useDefaultRouteConfig() } + /** * Handler for requests which attempt to update roles - * @param {external:ExpressRequest} req + * @param {external:ExpressRequest} req * @returns {Promise} */ - async onUpdateRoles(req) { - if(!req.body.roles || req.method === 'GET' || req.method === 'DELETE') { - return; + async onUpdateRoles (req) { + if (!req.body.roles || req.method === 'GET' || req.method === 'DELETE') { + return } - if(!req.auth.isSuper && !req.auth.scopes.includes('assign:roles')) { - this.log('error', 'User does not have the correct permissions to assign user roles'); - throw this.app.errors.UNAUTHORISED; + if (!req.auth.isSuper && !req.auth.scopes.includes('assign:roles')) { + this.log('error', 'User does not have the correct permissions to assign user roles') + throw this.app.errors.UNAUTHORISED } - if(req.method !== 'POST') { - const auth = await this.app.waitForModule('auth'); - await auth.authentication.disavowUser({ userId: req.params._id || req.body._id }); + if (req.method !== 'POST') { + const auth = await this.app.waitForModule('auth') + await auth.authentication.disavowUser({ userId: req.params._id || req.body._id }) } } } -export default RolesModule; \ No newline at end of file +export default RolesModule diff --git a/package.json b/package.json index ecc8f48..b18a1b3 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,9 @@ "peerDependencies": { "adapt-authoring-api": "github:adapt-security/adapt-authoring-api", "adapt-authoring-core": "github:adapt-security/adapt-authoring-core" + }, + "devDependencies": { + "eslint": "^8.36.0", + "standard": "^17.0.0" } }