feat: admin general + verify account

This commit is contained in:
Nicolas Giard
2018-12-24 01:03:10 -05:00
parent 2b98a5f27a
commit fcee4c0945
22 changed files with 494 additions and 80 deletions

View File

@@ -40,7 +40,7 @@ module.exports = class Authentication extends Model {
...str,
domainWhitelist: _.get(str.domainWhitelist, 'v', []),
autoEnrollGroups: _.get(str.autoEnrollGroups, 'v', [])
})), ['title'])
})), ['key'])
}
static async refreshStrategiesFromDisk() {

View File

@@ -35,7 +35,7 @@ module.exports = class Setting extends Model {
const settings = await WIKI.models.settings.query()
if (settings.length > 0) {
return _.reduce(settings, (res, val, key) => {
_.set(res, val.key, (val.value.v) ? val.value.v : val.value)
_.set(res, val.key, (_.has(val.value, 'v')) ? val.value.v : val.value)
return res
}, {})
} else {

74
server/models/userKeys.js Normal file
View File

@@ -0,0 +1,74 @@
/* global WIKI */
const _ = require('lodash')
const securityHelper = require('../helpers/security')
const Model = require('objection').Model
const moment = require('moment')
const nanoid = require('nanoid')
/**
* Users model
*/
module.exports = class UserKey extends Model {
static get tableName() { return 'userKeys' }
static get jsonSchema () {
return {
type: 'object',
required: ['kind', 'token', 'validUntil'],
properties: {
id: {type: 'integer'},
kind: {type: 'string'},
token: {type: 'string'},
createdAt: {type: 'string'},
validUntil: {type: 'string'}
}
}
}
static get relationMappings() {
return {
user: {
relation: Model.BelongsToOneRelation,
modelClass: require('./users'),
join: {
from: 'userKeys.userId',
to: 'users.id'
}
}
}
}
async $beforeInsert(context) {
await super.$beforeInsert(context)
this.createdAt = moment.utc().toISOString()
}
static async generateToken ({ userId, kind }, context) {
const token = await nanoid()
await WIKI.models.userKeys.query().insert({
kind,
token,
validUntil: moment.utc().add(1, 'days').toISOString(),
userId
})
return token
}
static async validateToken ({ kind, token }, context) {
const res = await WIKI.models.userKeys.query().findOne({ kind, token }).eager('user')
if (res) {
await WIKI.models.userKeys.query().deleteById(res.id)
if (moment.utc().isAfter(moment.utc(res.validUntil))) {
throw new WIKI.Error.AuthValidationTokenInvalid()
}
return res.user
} else {
throw new WIKI.Error.AuthValidationTokenInvalid()
}
return token
}
}

View File

@@ -345,7 +345,7 @@ module.exports = class User extends Model {
const usr = await WIKI.models.users.query().findOne({ email, providerKey: 'local' })
if (!usr) {
// Create the account
await WIKI.models.users.query().insert({
const newUsr = await WIKI.models.users.query().insert({
provider: 'local',
email,
name,
@@ -358,6 +358,12 @@ module.exports = class User extends Model {
isVerified: false
})
// Create verification token
const verificationToken = await WIKI.models.userKeys.generateToken({
kind: 'verify',
userId: newUsr.id
})
// Send verification email
await WIKI.mail.send({
template: 'accountVerify',
@@ -367,10 +373,10 @@ module.exports = class User extends Model {
preheadertext: 'Verify your account in order to gain access to the wiki.',
title: 'Verify your account',
content: 'Click the button below in order to verify your account and gain access to the wiki.',
buttonLink: 'http://www.google.com',
buttonLink: `${WIKI.config.host}/verify/${verificationToken}`,
buttonText: 'Verify'
},
text: `You must open the following link in your browser to verify your account and gain access to the wiki: http://www.google.com`
text: `You must open the following link in your browser to verify your account and gain access to the wiki: ${WIKI.config.host}/verify/${verificationToken}`
})
return true
} else {