]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user.js
1 const mongoose
= require('mongoose')
3 const customUsersValidators
= require('../helpers/custom-validators').users
4 const modelUtils
= require('./utils')
5 const peertubeCrypto
= require('../helpers/peertube-crypto')
7 // ---------------------------------------------------------------------------
9 const UserSchema
= mongoose
.Schema({
19 UserSchema
.path('password').required(customUsersValidators
.isUserPasswordValid
)
20 UserSchema
.path('username').required(customUsersValidators
.isUserUsernameValid
)
21 UserSchema
.path('role').validate(customUsersValidators
.isUserRoleValid
)
23 UserSchema
.methods
= {
24 isPasswordMatch: isPasswordMatch
,
25 toFormatedJSON: toFormatedJSON
28 UserSchema
.statics
= {
29 countTotal: countTotal
,
30 getByUsername: getByUsername
,
31 listForApi: listForApi
,
33 loadByUsername: loadByUsername
36 UserSchema
.pre('save', function (next
) {
39 peertubeCrypto
.cryptPassword(this.password
, function (err
, hash
) {
40 if (err
) return next(err
)
48 mongoose
.model('User', UserSchema
)
50 // ------------------------------ METHODS ------------------------------
52 function isPasswordMatch (password
, callback
) {
53 return peertubeCrypto
.comparePassword(password
, this.password
, callback
)
56 function toFormatedJSON () {
59 username: this.username
,
61 createdDate: this.createdDate
64 // ------------------------------ STATICS ------------------------------
66 function countTotal (callback
) {
67 return this.count(callback
)
70 function getByUsername (username
) {
71 return this.findOne({ username: username
})
74 function listForApi (start
, count
, sort
, callback
) {
76 return modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
79 function loadById (id
, callback
) {
80 return this.findById(id
, callback
)
83 function loadByUsername (username
, callback
) {
84 return this.findOne({ username: username
}, callback
)