2016-08-16 22:31:45 +02:00
|
|
|
const modelUtils = require('./utils')
|
2016-08-25 17:57:37 +02:00
|
|
|
const peertubeCrypto = require('../helpers/peertube-crypto')
|
2016-08-04 22:32:36 +02:00
|
|
|
|
2016-07-01 16:03:53 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const User = sequelize.define('User',
|
|
|
|
{
|
|
|
|
password: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
},
|
|
|
|
username: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
},
|
|
|
|
role: {
|
|
|
|
type: DataTypes.STRING
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
getByUsername,
|
|
|
|
list,
|
|
|
|
listForApi,
|
|
|
|
loadById,
|
|
|
|
loadByUsername
|
|
|
|
},
|
|
|
|
instanceMethods: {
|
|
|
|
isPasswordMatch,
|
|
|
|
toFormatedJSON
|
|
|
|
},
|
|
|
|
hooks: {
|
|
|
|
beforeCreate: beforeCreateOrUpdate,
|
|
|
|
beforeUpdate: beforeCreateOrUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return User
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|
2016-07-01 16:03:53 +02:00
|
|
|
|
2016-12-11 21:50:51 +01:00
|
|
|
// TODO: Validation
|
|
|
|
// UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
|
|
|
|
// UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
|
|
|
|
// UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
|
2016-07-01 16:03:53 +02:00
|
|
|
|
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-07-01 16:03:53 +02: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,
|
2016-09-23 17:19:57 +02:00
|
|
|
role: this.role,
|
2016-12-11 21:50:51 +01:00
|
|
|
createdAt: this.createdAt
|
2016-08-25 17:57:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
2016-07-01 16:03:53 +02:00
|
|
|
|
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-16 21:51:35 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 22:36:36 +02:00
|
|
|
function list (callback) {
|
2016-12-11 21:50:51 +01:00
|
|
|
return this.find().asCallback(callback)
|
2016-09-26 22:36:36 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
})
|
2016-07-01 16:03:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 21:44:45 +02:00
|
|
|
function loadById (id, callback) {
|
2016-12-11 21:50:51 +01:00
|
|
|
return this.findById(id).asCallback(callback)
|
2016-08-09 21:44:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 22:32:36 +02:00
|
|
|
function loadByUsername (username, callback) {
|
2016-12-11 21:50:51 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username: username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findOne(query).asCallback(callback)
|
2016-08-04 22:32:36 +02:00
|
|
|
}
|