aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/user.js')
-rw-r--r--server/models/user.js132
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 @@
1const mongoose = require('mongoose')
2
3const customUsersValidators = require('../helpers/custom-validators').users
4const modelUtils = require('./utils') 1const modelUtils = require('./utils')
5const peertubeCrypto = require('../helpers/peertube-crypto') 2const peertubeCrypto = require('../helpers/peertube-crypto')
6 3
7const OAuthToken = mongoose.model('OAuthToken')
8
9// --------------------------------------------------------------------------- 4// ---------------------------------------------------------------------------
10 5
11const UserSchema = mongoose.Schema({ 6module.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: {
21UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) 16 type: DataTypes.STRING
22UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) 17 }
23UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) 18 },
24 19 {
25UserSchema.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
30UserSchema.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
39UserSchema.pre('save', function (next) { 49function 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
51UserSchema.pre('remove', function (next) {
52 const user = this
53
54 OAuthToken.removeByUserId(user._id, next)
55})
56
57mongoose.model('User', UserSchema)
58 58
59// ------------------------------ METHODS ------------------------------ 59// ------------------------------ METHODS ------------------------------
60 60
@@ -64,35 +64,63 @@ function isPasswordMatch (password, callback) {
64 64
65function toFormatedJSON () { 65function 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
75function associate (models) {
76 this.hasMany(models.OAuthToken, {
77 foreignKey: 'userId',
78 onDelete: 'cascade'
79 })
80}
81
75function countTotal (callback) { 82function countTotal (callback) {
76 return this.count(callback) 83 return this.count().asCallback(callback)
77} 84}
78 85
79function getByUsername (username) { 86function 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
83function list (callback) { 96function list (callback) {
84 return this.find(callback) 97 return this.find().asCallback(callback)
85} 98}
86 99
87function listForApi (start, count, sort, callback) { 100function 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
92function loadById (id, callback) { 114function loadById (id, callback) {
93 return this.findById(id, callback) 115 return this.findById(id).asCallback(callback)
94} 116}
95 117
96function loadByUsername (username, callback) { 118function 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}