diff options
Diffstat (limited to 'server/models/user.js')
-rw-r--r-- | server/models/user.js | 132 |
1 files changed, 80 insertions, 52 deletions
diff --git a/server/models/user.js b/server/models/user.js index a19de7072..e50eb96ea 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -1,60 +1,60 @@ | |||
1 | const mongoose = require('mongoose') | ||
2 | |||
3 | const customUsersValidators = require('../helpers/custom-validators').users | ||
4 | const modelUtils = require('./utils') | 1 | const modelUtils = require('./utils') |
5 | const peertubeCrypto = require('../helpers/peertube-crypto') | 2 | const peertubeCrypto = require('../helpers/peertube-crypto') |
6 | 3 | ||
7 | const OAuthToken = mongoose.model('OAuthToken') | ||
8 | |||
9 | // --------------------------------------------------------------------------- | 4 | // --------------------------------------------------------------------------- |
10 | 5 | ||
11 | const UserSchema = mongoose.Schema({ | 6 | module.exports = function (sequelize, DataTypes) { |
12 | createdDate: { | 7 | const User = sequelize.define('User', |
13 | type: Date, | 8 | { |
14 | default: Date.now | 9 | password: { |
15 | }, | 10 | type: DataTypes.STRING |
16 | password: String, | 11 | }, |
17 | username: String, | 12 | username: { |
18 | role: String | 13 | type: DataTypes.STRING |
19 | }) | 14 | }, |
20 | 15 | role: { | |
21 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) | 16 | type: DataTypes.STRING |
22 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | 17 | } |
23 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | 18 | }, |
24 | 19 | { | |
25 | UserSchema.methods = { | 20 | classMethods: { |
26 | isPasswordMatch, | 21 | associate, |
27 | toFormatedJSON | 22 | |
23 | countTotal, | ||
24 | getByUsername, | ||
25 | list, | ||
26 | listForApi, | ||
27 | loadById, | ||
28 | loadByUsername | ||
29 | }, | ||
30 | instanceMethods: { | ||
31 | isPasswordMatch, | ||
32 | toFormatedJSON | ||
33 | }, | ||
34 | hooks: { | ||
35 | beforeCreate: beforeCreateOrUpdate, | ||
36 | beforeUpdate: beforeCreateOrUpdate | ||
37 | } | ||
38 | } | ||
39 | ) | ||
40 | |||
41 | return User | ||
28 | } | 42 | } |
29 | 43 | ||
30 | UserSchema.statics = { | 44 | // TODO: Validation |
31 | countTotal, | 45 | // UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) |
32 | getByUsername, | 46 | // UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) |
33 | list, | 47 | // UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) |
34 | listForApi, | ||
35 | loadById, | ||
36 | loadByUsername | ||
37 | } | ||
38 | 48 | ||
39 | UserSchema.pre('save', function (next) { | 49 | function beforeCreateOrUpdate (user, options, next) { |
40 | const user = this | 50 | peertubeCrypto.cryptPassword(user.password, function (err, hash) { |
41 | |||
42 | peertubeCrypto.cryptPassword(this.password, function (err, hash) { | ||
43 | if (err) return next(err) | 51 | if (err) return next(err) |
44 | 52 | ||
45 | user.password = hash | 53 | user.password = hash |
46 | 54 | ||
47 | return next() | 55 | return next() |
48 | }) | 56 | }) |
49 | }) | 57 | } |
50 | |||
51 | UserSchema.pre('remove', function (next) { | ||
52 | const user = this | ||
53 | |||
54 | OAuthToken.removeByUserId(user._id, next) | ||
55 | }) | ||
56 | |||
57 | mongoose.model('User', UserSchema) | ||
58 | 58 | ||
59 | // ------------------------------ METHODS ------------------------------ | 59 | // ------------------------------ METHODS ------------------------------ |
60 | 60 | ||
@@ -64,35 +64,63 @@ function isPasswordMatch (password, callback) { | |||
64 | 64 | ||
65 | function toFormatedJSON () { | 65 | function toFormatedJSON () { |
66 | return { | 66 | return { |
67 | id: this._id, | 67 | id: this.id, |
68 | username: this.username, | 68 | username: this.username, |
69 | role: this.role, | 69 | role: this.role, |
70 | createdDate: this.createdDate | 70 | createdAt: this.createdAt |
71 | } | 71 | } |
72 | } | 72 | } |
73 | // ------------------------------ STATICS ------------------------------ | 73 | // ------------------------------ STATICS ------------------------------ |
74 | 74 | ||
75 | function associate (models) { | ||
76 | this.hasMany(models.OAuthToken, { | ||
77 | foreignKey: 'userId', | ||
78 | onDelete: 'cascade' | ||
79 | }) | ||
80 | } | ||
81 | |||
75 | function countTotal (callback) { | 82 | function countTotal (callback) { |
76 | return this.count(callback) | 83 | return this.count().asCallback(callback) |
77 | } | 84 | } |
78 | 85 | ||
79 | function getByUsername (username) { | 86 | function getByUsername (username) { |
80 | return this.findOne({ username: username }) | 87 | const query = { |
88 | where: { | ||
89 | username: username | ||
90 | } | ||
91 | } | ||
92 | |||
93 | return this.findOne(query) | ||
81 | } | 94 | } |
82 | 95 | ||
83 | function list (callback) { | 96 | function list (callback) { |
84 | return this.find(callback) | 97 | return this.find().asCallback(callback) |
85 | } | 98 | } |
86 | 99 | ||
87 | function listForApi (start, count, sort, callback) { | 100 | function listForApi (start, count, sort, callback) { |
88 | const query = {} | 101 | const query = { |
89 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | 102 | offset: start, |
103 | limit: count, | ||
104 | order: [ modelUtils.getSort(sort) ] | ||
105 | } | ||
106 | |||
107 | return this.findAndCountAll(query).asCallback(function (err, result) { | ||
108 | if (err) return callback(err) | ||
109 | |||
110 | return callback(null, result.rows, result.count) | ||
111 | }) | ||
90 | } | 112 | } |
91 | 113 | ||
92 | function loadById (id, callback) { | 114 | function loadById (id, callback) { |
93 | return this.findById(id, callback) | 115 | return this.findById(id).asCallback(callback) |
94 | } | 116 | } |
95 | 117 | ||
96 | function loadByUsername (username, callback) { | 118 | function loadByUsername (username, callback) { |
97 | return this.findOne({ username: username }, callback) | 119 | const query = { |
120 | where: { | ||
121 | username: username | ||
122 | } | ||
123 | } | ||
124 | |||
125 | return this.findOne(query).asCallback(callback) | ||
98 | } | 126 | } |