From e3b48094783480cfac760d0a545382b60bba55bf Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 24 Feb 2020 10:56:41 -0700 Subject: [PATCH] Fix login process: add permissionLevels --- app/views/login/controller.js | 5 ++++- app/views/login/index.js | 16 ++++++++++++++++ server/controllers/account.js | 11 ++++++++++- server/i18n/locales/en/ui.json | 9 ++++++++- server/routes/account.js | 6 +++--- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/views/login/controller.js b/app/views/login/controller.js index 8f84c4f..e64e5aa 100644 --- a/app/views/login/controller.js +++ b/app/views/login/controller.js @@ -13,6 +13,7 @@ export class LoginController extends ViewController { createDisplayName: '', createPassword: '', createConfirm: '', + createPermission: 100, }, loginError: '', createError: '', @@ -146,7 +147,8 @@ export class LoginController extends ViewController { createEmail, createUsername, createDisplayName, - createPassword + createPassword, + createPermission } = this.state.fieldValues; fetch('/api/account/create', { @@ -159,6 +161,7 @@ export class LoginController extends ViewController { username: createUsername, displayName: createDisplayName, password: createPassword, + permissionLevel: createPermission, }), }).then(response => response.json()) .then(response => { diff --git a/app/views/login/index.js b/app/views/login/index.js index 88e07f3..fc6ac2b 100644 --- a/app/views/login/index.js +++ b/app/views/login/index.js @@ -127,6 +127,22 @@ export const loginView = (state, emit, i18n) => { onkeyup=${e => { if (e.key === 'Enter') controller.validateCreateAccount() }} > + ${ controller.state.createError === '' ? null diff --git a/server/controllers/account.js b/server/controllers/account.js index 688ae5c..ed81374 100644 --- a/server/controllers/account.js +++ b/server/controllers/account.js @@ -27,6 +27,7 @@ class AccountController { if (typeof createAccountData.email === 'undefined' || typeof createAccountData.username === 'undefined' || typeof createAccountData.password === 'undefined' + || typeof createAccountData.permissionLevel === 'undefined' || createAccountData.password === '') { return { error: true, @@ -45,6 +46,12 @@ class AccountController { message: 'api.account.create.invalid_username', }; } + if (![100, 33, 0].includes(createAccountData.permissionLevel)) { + return { + error: true, + message: 'api.account.create.invalid_permissionLevel', + }; + } return true; } @@ -75,6 +82,7 @@ class AccountController { username: formData.username.toString().trim(), displayName: displayName.length > 0 ? displayName : 'A Bee', password: formData.password, + permissionLevel: formData.permissionLevel, } } @@ -130,7 +138,7 @@ class AccountController { return true; } - async createUser (email, username, displayName, password, needsConfirmation) { + async createUser (email, username, displayName, permissionLevel, password, needsConfirmation) { const hashData = AccountController.hashPassword(password); // The data should already have gone through AccountController.cleanCreateAccountFormData() try { @@ -138,6 +146,7 @@ class AccountController { email, username, displayName, + permissionLevel, passwordHash: hashData.hash, passwordSalt: hashData.salt, accountConfirm: needsConfirmation ? crypto.randomBytes(32).toString('hex') : null, diff --git a/server/i18n/locales/en/ui.json b/server/i18n/locales/en/ui.json index 7d451e4..e75fb77 100644 --- a/server/i18n/locales/en/ui.json +++ b/server/i18n/locales/en/ui.json @@ -41,6 +41,12 @@ "confirm_password": "Confirm Password", "username": "Username", "display_name": "Display Name", + "permissions": { + "label": "Profile Visibility", + "public": "Public (anyone can see you)", + "following": "Following (only accounts you follow can see you)", + "private": "Private (nobody can see you)" + }, "create_account_button": "Create Account!", "login_required_field_blank": "You must enter both a valid email address and password.", "create_required_field_blank": "You must complete all required fields.", @@ -93,7 +99,8 @@ "fail": "Something went wrong and the account could not be created. Please try again later.", "required_data_missing": "Could not create account because required data is missing.", "invalid_email": "The email address entered is not valid.", - "invalid_username": "The username entered is not valid. Usernames must be at least 2 characters long and can only contain letters a–z, numbers 0–9, and underscores", + "invalid_username": "The username entered is not valid. Usernames must be at least 2 characters long and can only contain letters a–z, numbers 0–9, and underscores.", + "invalid_permissionLevel": "The permissionLevel entered is not valid. PermissionLevel must be one 100, 33, or 0. Contact the front-end developer and ask them to fix their permission selector.", "success": "Account created successfully! You may now log in using the email address and password you provided." }, "confirm": { diff --git a/server/routes/account.js b/server/routes/account.js index 4504e3e..e8ab4e1 100644 --- a/server/routes/account.js +++ b/server/routes/account.js @@ -30,14 +30,14 @@ async function routes(fastify, options) { return reply.code(400).send(canCreateUser); } - const newUser = await account.createUser(formData.email, formData.username, formData.displayName, formData.password, fastify.canEmail); - + const newUser = await account.createUser(formData.email, formData.username, formData.displayName, formData.permissionLevel, formData.password, fastify.canEmail); + if (typeof newUser.error !== 'undefined' && newUser.error !== false) { newUser.message = 'api.account.create.fail'; return reply.code(400).send(newUser); } - const shelf = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem); + const shelf = new ShelfController(fastify.models, null); const defaultShelvesCreated = await shelf.createDefaultShelves(newUser); // If some of the default shelves are not created successfully, delete the user and send an error