]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user.js
3 const values
= require('lodash/values')
5 const modelUtils
= require('./utils')
6 const constants
= require('../initializers/constants')
7 const peertubeCrypto
= require('../helpers/peertube-crypto')
8 const customUsersValidators
= require('../helpers/custom-validators').users
10 // ---------------------------------------------------------------------------
12 module
.exports = function (sequelize
, DataTypes
) {
13 const User
= sequelize
.define('User',
16 type: DataTypes
.STRING
,
19 passwordValid: function (value
) {
20 const res
= customUsersValidators
.isUserPasswordValid(value
)
21 if (res
=== false) throw new Error('Password not valid.')
26 type: DataTypes
.STRING
,
29 usernameValid: function (value
) {
30 const res
= customUsersValidators
.isUserUsernameValid(value
)
31 if (res
=== false) throw new Error('Username not valid.')
36 type: DataTypes
.ENUM(values(constants
.USER_ROLES
)),
43 fields: [ 'username' ]
61 beforeCreate: beforeCreateOrUpdate
,
62 beforeUpdate: beforeCreateOrUpdate
70 function beforeCreateOrUpdate (user
, options
, next
) {
71 peertubeCrypto
.cryptPassword(user
.password
, function (err
, hash
) {
72 if (err
) return next(err
)
80 // ------------------------------ METHODS ------------------------------
82 function isPasswordMatch (password
, callback
) {
83 return peertubeCrypto
.comparePassword(password
, this.password
, callback
)
86 function toFormatedJSON () {
89 username: this.username
,
91 createdAt: this.createdAt
94 // ------------------------------ STATICS ------------------------------
96 function associate (models
) {
97 this.hasOne(models
.Author
, {
102 this.hasMany(models
.OAuthToken
, {
103 foreignKey: 'userId',
108 function countTotal (callback
) {
109 return this.count().asCallback(callback
)
112 function getByUsername (username
) {
119 return this.findOne(query
)
122 function list (callback
) {
123 return this.find().asCallback(callback
)
126 function listForApi (start
, count
, sort
, callback
) {
130 order: [ modelUtils
.getSort(sort
) ]
133 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
134 if (err
) return callback(err
)
136 return callback(null, result
.rows
, result
.count
)
140 function loadById (id
, callback
) {
141 return this.findById(id
).asCallback(callback
)
144 function loadByUsername (username
, callback
) {
151 return this.findOne(query
).asCallback(callback
)