]>
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 | |
b81929a0 C |
7 | const OAuthToken = mongoose.model('OAuthToken') |
8 | ||
69b0a27c C |
9 | // --------------------------------------------------------------------------- |
10 | ||
11 | const UserSchema = mongoose.Schema({ | |
5c39adb7 C |
12 | createdDate: { |
13 | type: Date, | |
14 | default: Date.now | |
15 | }, | |
69b0a27c | 16 | password: String, |
9bd26629 C |
17 | username: String, |
18 | role: String | |
69b0a27c C |
19 | }) |
20 | ||
9bd26629 C |
21 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) |
22 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | |
23 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | |
24 | ||
25 | UserSchema.methods = { | |
c4403b29 C |
26 | isPasswordMatch, |
27 | toFormatedJSON | |
9bd26629 | 28 | } |
69b0a27c C |
29 | |
30 | UserSchema.statics = { | |
c4403b29 C |
31 | countTotal, |
32 | getByUsername, | |
33 | list, | |
34 | listForApi, | |
35 | loadById, | |
36 | loadByUsername | |
69b0a27c C |
37 | } |
38 | ||
26d7d31b C |
39 | UserSchema.pre('save', function (next) { |
40 | const user = this | |
41 | ||
42 | peertubeCrypto.cryptPassword(this.password, function (err, hash) { | |
43 | if (err) return next(err) | |
44 | ||
45 | user.password = hash | |
46 | ||
47 | return next() | |
48 | }) | |
49 | }) | |
50 | ||
b81929a0 C |
51 | UserSchema.pre('remove', function (next) { |
52 | const user = this | |
53 | ||
54 | OAuthToken.removeByUserId(user._id, next) | |
55 | }) | |
56 | ||
69b0a27c C |
57 | mongoose.model('User', UserSchema) |
58 | ||
26d7d31b C |
59 | // ------------------------------ METHODS ------------------------------ |
60 | ||
61 | function isPasswordMatch (password, callback) { | |
62 | return peertubeCrypto.comparePassword(password, this.password, callback) | |
63 | } | |
64 | ||
65 | function toFormatedJSON () { | |
66 | return { | |
67 | id: this._id, | |
68 | username: this.username, | |
d74a0680 C |
69 | role: this.role, |
70 | createdDate: this.createdDate | |
26d7d31b C |
71 | } |
72 | } | |
73 | // ------------------------------ STATICS ------------------------------ | |
69b0a27c | 74 | |
5c39adb7 | 75 | function countTotal (callback) { |
089ff2f2 C |
76 | return this.count(callback) |
77 | } | |
78 | ||
26d7d31b C |
79 | function getByUsername (username) { |
80 | return this.findOne({ username: username }) | |
9bd26629 C |
81 | } |
82 | ||
00d6b0dd C |
83 | function list (callback) { |
84 | return this.find(callback) | |
85 | } | |
86 | ||
5c39adb7 C |
87 | function listForApi (start, count, sort, callback) { |
88 | const query = {} | |
89 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | |
69b0a27c C |
90 | } |
91 | ||
68a3b9f2 C |
92 | function loadById (id, callback) { |
93 | return this.findById(id, callback) | |
94 | } | |
95 | ||
9bd26629 C |
96 | function loadByUsername (username, callback) { |
97 | return this.findOne({ username: username }, callback) | |
98 | } |