Fix login process: add permissionLevels

This commit is contained in:
Robbie Antenesse 2020-02-24 10:56:41 -07:00
parent f12f8b987a
commit e3b4809478
5 changed files with 41 additions and 6 deletions

View File

@ -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 => {

View File

@ -127,6 +127,22 @@ export const loginView = (state, emit, i18n) => {
onkeyup=${e => { if (e.key === 'Enter') controller.validateCreateAccount() }}
>
</label>
<label>
<span>${__('login.permissions.label')}</span>
<select name="new_visibility"
onchange=${e => controller.state.fieldValues.createPermission = e.target.value.trim()}
>
<option value="100" ${controller.state.fieldValues.createPermission === 100 ? 'selected' : null}>
${__('login.permissions.public')}
</option>
<option value="33" ${controller.state.fieldValues.createPermission === 33 ? 'selected' : null}>
${__('login.permissions.following')}
</option>
<option value="0" ${controller.state.fieldValues.createPermission === 0 ? 'selected' : null}>
${__('login.permissions.private')}
</option>
</select>
</label>
${
controller.state.createError === ''
? null

View File

@ -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,

View File

@ -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 az, numbers 09, and underscores",
"invalid_username": "The username entered is not valid. Usernames must be at least 2 characters long and can only contain letters az, numbers 09, 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": {

View File

@ -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