]>
Commit | Line | Data |
---|---|---|
69b0a27c C |
1 | const mongoose = require('mongoose') |
2 | ||
9bd26629 | 3 | const customUsersValidators = require('../helpers/custom-validators').users |
5c39adb7 | 4 | const modelUtils = require('./utils') |
26d7d31b | 5 | const peertubeCrypto = require('../helpers/peertube-crypto') |
9bd26629 | 6 | |
69b0a27c C |
7 | // --------------------------------------------------------------------------- |
8 | ||
9 | const UserSchema = mongoose.Schema({ | |
5c39adb7 C |
10 | createdDate: { |
11 | type: Date, | |
12 | default: Date.now | |
13 | }, | |
69b0a27c | 14 | password: String, |
9bd26629 C |
15 | username: String, |
16 | role: String | |
69b0a27c C |
17 | }) |
18 | ||
9bd26629 C |
19 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) |
20 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | |
21 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | |
22 | ||
23 | UserSchema.methods = { | |
26d7d31b | 24 | isPasswordMatch: isPasswordMatch, |
9bd26629 C |
25 | toFormatedJSON: toFormatedJSON |
26 | } | |
69b0a27c C |
27 | |
28 | UserSchema.statics = { | |
5c39adb7 | 29 | countTotal: countTotal, |
26d7d31b | 30 | getByUsername: getByUsername, |
5c39adb7 | 31 | listForApi: listForApi, |
68a3b9f2 | 32 | loadById: loadById, |
9bd26629 | 33 | loadByUsername: loadByUsername |
69b0a27c C |
34 | } |
35 | ||
26d7d31b C |
36 | UserSchema.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 | ||
69b0a27c C |
48 | mongoose.model('User', UserSchema) |
49 | ||
26d7d31b C |
50 | // ------------------------------ METHODS ------------------------------ |
51 | ||
52 | function isPasswordMatch (password, callback) { | |
53 | return peertubeCrypto.comparePassword(password, this.password, callback) | |
54 | } | |
55 | ||
56 | function toFormatedJSON () { | |
57 | return { | |
58 | id: this._id, | |
59 | username: this.username, | |
d74a0680 C |
60 | role: this.role, |
61 | createdDate: this.createdDate | |
26d7d31b C |
62 | } |
63 | } | |
64 | // ------------------------------ STATICS ------------------------------ | |
69b0a27c | 65 | |
5c39adb7 | 66 | function countTotal (callback) { |
089ff2f2 C |
67 | return this.count(callback) |
68 | } | |
69 | ||
26d7d31b C |
70 | function getByUsername (username) { |
71 | return this.findOne({ username: username }) | |
9bd26629 C |
72 | } |
73 | ||
5c39adb7 C |
74 | function listForApi (start, count, sort, callback) { |
75 | const query = {} | |
76 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | |
69b0a27c C |
77 | } |
78 | ||
68a3b9f2 C |
79 | function loadById (id, callback) { |
80 | return this.findById(id, callback) | |
81 | } | |
82 | ||
9bd26629 C |
83 | function loadByUsername (username, callback) { |
84 | return this.findOne({ username: username }, callback) | |
85 | } |