PeerTube/server/models/user.js

148 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-12-28 15:49:23 +01:00
'use strict'
const values = require('lodash/values')
2016-08-16 22:31:45 +02:00
const modelUtils = require('./utils')
2016-12-28 15:49:23 +01:00
const constants = require('../initializers/constants')
2016-08-25 17:57:37 +02:00
const peertubeCrypto = require('../helpers/peertube-crypto')
2016-12-28 15:49:23 +01:00
const customUsersValidators = require('../helpers/custom-validators').users
// ---------------------------------------------------------------------------
2016-12-11 21:50:51 +01:00
module.exports = function (sequelize, DataTypes) {
const User = sequelize.define('User',
{
password: {
2016-12-28 15:49:23 +01: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 21:50:51 +01:00
},
username: {
2016-12-28 15:49:23 +01: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 21:50:51 +01:00
},
role: {
2016-12-28 15:49:23 +01:00
type: DataTypes.ENUM(values(constants.USER_ROLES)),
allowNull: false
2016-12-11 21:50:51 +01:00
}
},
{
2016-12-29 09:33:28 +01:00
indexes: [
{
fields: [ 'username' ]
}
],
2016-12-11 21:50:51 +01:00
classMethods: {
associate,
countTotal,
getByUsername,
list,
listForApi,
loadById,
loadByUsername
},
instanceMethods: {
isPasswordMatch,
toFormatedJSON
},
hooks: {
beforeCreate: beforeCreateOrUpdate,
beforeUpdate: beforeCreateOrUpdate
}
}
)
return User
}
2016-12-11 21:50:51 +01:00
function beforeCreateOrUpdate (user, options, next) {
peertubeCrypto.cryptPassword(user.password, function (err, hash) {
2016-08-25 17:57:37 +02:00
if (err) return next(err)
user.password = hash
return next()
})
2016-12-11 21:50:51 +01:00
}
2016-08-25 17:57:37 +02:00
// ------------------------------ METHODS ------------------------------
function isPasswordMatch (password, callback) {
return peertubeCrypto.comparePassword(password, this.password, callback)
}
function toFormatedJSON () {
return {
2016-12-11 21:50:51 +01:00
id: this.id,
2016-08-25 17:57:37 +02:00
username: this.username,
role: this.role,
2016-12-11 21:50:51 +01:00
createdAt: this.createdAt
2016-08-25 17:57:37 +02:00
}
}
// ------------------------------ STATICS ------------------------------
2016-12-11 21:50:51 +01:00
function associate (models) {
this.hasMany(models.OAuthToken, {
foreignKey: 'userId',
onDelete: 'cascade'
})
}
2016-08-16 22:31:45 +02:00
function countTotal (callback) {
2016-12-11 21:50:51 +01:00
return this.count().asCallback(callback)
}
2016-08-25 17:57:37 +02:00
function getByUsername (username) {
2016-12-11 21:50:51 +01:00
const query = {
where: {
username: username
}
}
return this.findOne(query)
}
function list (callback) {
2016-12-11 21:50:51 +01:00
return this.find().asCallback(callback)
}
2016-08-16 22:31:45 +02:00
function listForApi (start, count, sort, callback) {
2016-12-11 21:50:51 +01: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 21:50:51 +01:00
return this.findById(id).asCallback(callback)
}
function loadByUsername (username, callback) {
2016-12-11 21:50:51 +01:00
const query = {
where: {
username: username
}
}
return this.findOne(query).asCallback(callback)
}