From d9aa1a48909dba0680ba145dd7d151979519daef Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 22 Feb 2021 12:29:49 +0000 Subject: [PATCH] Switching user activity state to an options field rather than boolean (more extensible). --- .../DataTable/modals/CreateEditUser.svelte | 8 ++++---- packages/server/src/api/controllers/auth.js | 6 +++--- packages/server/src/api/controllers/user.js | 5 +++-- packages/server/src/constants/index.js | 16 +++++++++++----- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte index 0b2841d836..e49964c78b 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditUser.svelte @@ -29,7 +29,7 @@ let customSchema = { ...schema } delete customSchema["email"] delete customSchema["roleId"] - delete customSchema["active"] + delete customSchema["status"] return Object.entries(customSchema) } @@ -81,9 +81,9 @@ {/each} + meta={{ name: 'status', type: 'options', constraints: {inclusion: ['active', 'inactive']} }} + bind:value={row.status} + defaultValue={'active'} /> {#each customSchemaKeys as [key, meta]} {#if !meta.autocolumn} diff --git a/packages/server/src/api/controllers/auth.js b/packages/server/src/api/controllers/auth.js index 2d6e279b92..1cc6db3185 100644 --- a/packages/server/src/api/controllers/auth.js +++ b/packages/server/src/api/controllers/auth.js @@ -7,6 +7,7 @@ const { generateUserID } = require("../../db/utils") const { setCookie } = require("../../utilities") const { outputProcessing } = require("../../utilities/rowProcessor") const { ViewNames } = require("../../db/utils") +const { UserStatus } = require("../../constants") const INVALID_ERR = "Invalid Credentials" @@ -32,9 +33,8 @@ exports.authenticate = async ctx => { ctx.throw(401, INVALID_ERR) } - // check that the user is currently active, make sure its a boolean false - // so that older users which don't have this set are handled - if (typeof dbUser.active === "boolean" && !dbUser.active) { + // check that the user is currently inactive, if this is the case throw invalid + if (dbUser.status === UserStatus.INACTIVE) { ctx.throw(401, INVALID_ERR) } diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index 403bcdbb86..c100f43d88 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -2,6 +2,7 @@ const CouchDB = require("../../db") const bcrypt = require("../../utilities/bcrypt") const { generateUserID, getUserParams, ViewNames } = require("../../db/utils") const { getRole } = require("../../utilities/security/roles") +const { UserStatus } = require("../../constants") exports.fetch = async function(ctx) { const database = new CouchDB(ctx.user.appId) @@ -43,8 +44,8 @@ exports.create = async function(ctx) { tableId: ViewNames.USERS, } // add the active status to a user if its not provided - if (typeof user.active !== "boolean") { - user.active = true + if (user.status == null) { + user.status = UserStatus.ACTIVE } try { diff --git a/packages/server/src/constants/index.js b/packages/server/src/constants/index.js index 905a44b0a4..ff2e6f19f0 100644 --- a/packages/server/src/constants/index.js +++ b/packages/server/src/constants/index.js @@ -18,6 +18,11 @@ exports.AuthTypes = { EXTERNAL: "external", } +exports.UserStatus = { + ACTIVE: "active", + INACTIVE: "inactive", +} + exports.USERS_TABLE_SCHEMA = { _id: "ta_users", type: "table", @@ -47,13 +52,14 @@ exports.USERS_TABLE_SCHEMA = { inclusion: Object.values(BUILTIN_ROLE_IDS), }, }, - active: { - fieldName: "active", - name: "active", - type: exports.FieldTypes.BOOLEAN, + status: { + fieldName: "status", + name: "status", + type: exports.FieldTypes.OPTIONS, constraints: { - type: exports.FieldTypes.BOOLEAN, + type: exports.FieldTypes.STRING, presence: false, + inclusion: Object.values(exports.UserStatus), }, }, },