]>
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
.STRING(400),
43 type: DataTypes
.ENUM(values(constants
.USER_ROLES
)),
50 fields: [ 'username' ],
74 beforeCreate: beforeCreateOrUpdate
,
75 beforeUpdate: beforeCreateOrUpdate
83 function beforeCreateOrUpdate (user
, options
, next
) {
84 peertubeCrypto
.cryptPassword(user
.password
, function (err
, hash
) {
85 if (err
) return next(err
)
93 // ------------------------------ METHODS ------------------------------
95 function isPasswordMatch (password
, callback
) {
96 return peertubeCrypto
.comparePassword(password
, this.password
, callback
)
99 function toFormatedJSON () {
102 username: this.username
,
105 createdAt: this.createdAt
108 // ------------------------------ STATICS ------------------------------
110 function associate (models
) {
111 this.hasOne(models
.Author
, {
112 foreignKey: 'userId',
116 this.hasMany(models
.OAuthToken
, {
117 foreignKey: 'userId',
122 function countTotal (callback
) {
123 return this.count().asCallback(callback
)
126 function getByUsername (username
) {
133 return this.findOne(query
)
136 function list (callback
) {
137 return this.find().asCallback(callback
)
140 function listForApi (start
, count
, sort
, callback
) {
144 order: [ modelUtils
.getSort(sort
) ]
147 return this.findAndCountAll(query
).asCallback(function (err
, result
) {
148 if (err
) return callback(err
)
150 return callback(null, result
.rows
, result
.count
)
154 function loadById (id
, callback
) {
155 return this.findById(id
).asCallback(callback
)
158 function loadByUsername (username
, callback
) {
165 return this.findOne(query
).asCallback(callback
)
168 function loadByUsernameOrEmail (username
, email
, callback
) {
171 $or: [ { username
}, { email
} ]
175 return this.findOne(query
).asCallback(callback
)