aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/user.js43
1 files changed, 31 insertions, 12 deletions
diff --git a/server/models/user.js b/server/models/user.js
index c9c35b3e2..e76aab2ce 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -2,6 +2,7 @@ const mongoose = require('mongoose')
2 2
3const customUsersValidators = require('../helpers/custom-validators').users 3const customUsersValidators = require('../helpers/custom-validators').users
4const modelUtils = require('./utils') 4const modelUtils = require('./utils')
5const peertubeCrypto = require('../helpers/peertube-crypto')
5 6
6// --------------------------------------------------------------------------- 7// ---------------------------------------------------------------------------
7 8
@@ -20,27 +21,53 @@ UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
20UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) 21UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
21 22
22UserSchema.methods = { 23UserSchema.methods = {
24 isPasswordMatch: isPasswordMatch,
23 toFormatedJSON: toFormatedJSON 25 toFormatedJSON: toFormatedJSON
24} 26}
25 27
26UserSchema.statics = { 28UserSchema.statics = {
27 countTotal: countTotal, 29 countTotal: countTotal,
28 getByUsernameAndPassword: getByUsernameAndPassword, 30 getByUsername: getByUsername,
29 listForApi: listForApi, 31 listForApi: listForApi,
30 loadById: loadById, 32 loadById: loadById,
31 loadByUsername: loadByUsername 33 loadByUsername: loadByUsername
32} 34}
33 35
36UserSchema.pre('save', function (next) {
37 const user = this
38
39 peertubeCrypto.cryptPassword(this.password, function (err, hash) {
40 if (err) return next(err)
41
42 user.password = hash
43
44 return next()
45 })
46})
47
34mongoose.model('User', UserSchema) 48mongoose.model('User', UserSchema)
35 49
36// --------------------------------------------------------------------------- 50// ------------------------------ METHODS ------------------------------
51
52function isPasswordMatch (password, callback) {
53 return peertubeCrypto.comparePassword(password, this.password, callback)
54}
55
56function toFormatedJSON () {
57 return {
58 id: this._id,
59 username: this.username,
60 role: this.role
61 }
62}
63// ------------------------------ STATICS ------------------------------
37 64
38function countTotal (callback) { 65function countTotal (callback) {
39 return this.count(callback) 66 return this.count(callback)
40} 67}
41 68
42function getByUsernameAndPassword (username, password) { 69function getByUsername (username) {
43 return this.findOne({ username: username, password: password }) 70 return this.findOne({ username: username })
44} 71}
45 72
46function listForApi (start, count, sort, callback) { 73function listForApi (start, count, sort, callback) {
@@ -55,11 +82,3 @@ function loadById (id, callback) {
55function loadByUsername (username, callback) { 82function loadByUsername (username, callback) {
56 return this.findOne({ username: username }, callback) 83 return this.findOne({ username: username }, callback)
57} 84}
58
59function toFormatedJSON () {
60 return {
61 id: this._id,
62 username: this.username,
63 role: this.role
64 }
65}