PeerTube/server/models/user.js

177 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-12-28 08:49:23 -06:00
'use strict'
const values = require('lodash/values')
2016-08-16 15:31:45 -05:00
const modelUtils = require('./utils')
2016-12-28 08:49:23 -06:00
const constants = require('../initializers/constants')
2016-08-25 10:57:37 -05:00
const peertubeCrypto = require('../helpers/peertube-crypto')
2016-12-28 08:49:23 -06:00
const customUsersValidators = require('../helpers/custom-validators').users
// ---------------------------------------------------------------------------
2016-12-11 14:50:51 -06:00
module.exports = function (sequelize, DataTypes) {
const User = sequelize.define('User',
{
password: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING,
allowNull: false,
validate: {
passwordValid: function (value) {
const res = customUsersValidators.isUserPasswordValid(value)
if (res === false) throw new Error('Password not valid.')
}
}
2016-12-11 14:50:51 -06:00
},
username: {
2016-12-28 08:49:23 -06:00
type: DataTypes.STRING,
allowNull: false,
validate: {
usernameValid: function (value) {
const res = customUsersValidators.isUserUsernameValid(value)
if (res === false) throw new Error('Username not valid.')
}
}
2016-12-11 14:50:51 -06:00
},
2017-02-18 02:29:59 -06:00
email: {
2017-02-18 04:56:28 -06:00
type: DataTypes.STRING(400),
2017-02-18 02:29:59 -06:00
allowNull: false,
validate: {
isEmail: true
}
},
2016-12-11 14:50:51 -06:00
role: {
2016-12-28 08:49:23 -06:00
type: DataTypes.ENUM(values(constants.USER_ROLES)),
allowNull: false
2016-12-11 14:50:51 -06:00
}
},
{
2016-12-29 02:33:28 -06:00
indexes: [
{
2017-02-16 12:24:34 -06:00
fields: [ 'username' ],
unique: true
2017-02-18 02:29:59 -06:00
},
{
fields: [ 'email' ],
unique: true
2016-12-29 02:33:28 -06:00
}
],
2016-12-11 14:50:51 -06:00
classMethods: {
associate,
countTotal,
getByUsername,
list,
listForApi,
loadById,
2017-02-18 02:29:59 -06:00
loadByUsername,
loadByUsernameOrEmail
2016-12-11 14:50:51 -06:00
},
instanceMethods: {
isPasswordMatch,
toFormatedJSON
},
hooks: {
beforeCreate: beforeCreateOrUpdate,
beforeUpdate: beforeCreateOrUpdate
}
}
)
return User
}
2016-12-11 14:50:51 -06:00
function beforeCreateOrUpdate (user, options, next) {
peertubeCrypto.cryptPassword(user.password, function (err, hash) {
2016-08-25 10:57:37 -05:00
if (err) return next(err)
user.password = hash
return next()
})
2016-12-11 14:50:51 -06:00
}
2016-08-25 10:57:37 -05:00
// ------------------------------ METHODS ------------------------------
function isPasswordMatch (password, callback) {
return peertubeCrypto.comparePassword(password, this.password, callback)
}
function toFormatedJSON () {
return {
2016-12-11 14:50:51 -06:00
id: this.id,
2016-08-25 10:57:37 -05:00
username: this.username,
2017-02-18 02:29:59 -06:00
email: this.email,
role: this.role,
2016-12-11 14:50:51 -06:00
createdAt: this.createdAt
2016-08-25 10:57:37 -05:00
}
}
// ------------------------------ STATICS ------------------------------
2016-12-11 14:50:51 -06:00
function associate (models) {
this.hasOne(models.Author, {
foreignKey: 'userId',
onDelete: 'cascade'
})
2016-12-11 14:50:51 -06:00
this.hasMany(models.OAuthToken, {
foreignKey: 'userId',
onDelete: 'cascade'
})
}
2016-08-16 15:31:45 -05:00
function countTotal (callback) {
2016-12-11 14:50:51 -06:00
return this.count().asCallback(callback)
}
2016-08-25 10:57:37 -05:00
function getByUsername (username) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
username: username
}
}
return this.findOne(query)
}
function list (callback) {
2016-12-11 14:50:51 -06:00
return this.find().asCallback(callback)
}
2016-08-16 15:31:45 -05:00
function listForApi (start, count, sort, callback) {
2016-12-11 14:50:51 -06:00
const query = {
offset: start,
limit: count,
order: [ modelUtils.getSort(sort) ]
}
return this.findAndCountAll(query).asCallback(function (err, result) {
if (err) return callback(err)
return callback(null, result.rows, result.count)
})
}
function loadById (id, callback) {
2016-12-11 14:50:51 -06:00
return this.findById(id).asCallback(callback)
}
function loadByUsername (username, callback) {
2016-12-11 14:50:51 -06:00
const query = {
where: {
username: username
}
}
return this.findOne(query).asCallback(callback)
}
2017-02-18 02:29:59 -06:00
function loadByUsernameOrEmail (username, email, callback) {
const query = {
where: {
$or: [ { username }, { email } ]
}
}
return this.findOne(query).asCallback(callback)
}