diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-12 15:20:03 +0100 |
commit | 99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch) | |
tree | c9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/models/user.js | |
parent | fcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff) | |
parent | 91cc839af88730ba55f84997c56b85ea100070a7 (diff) | |
download | PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip |
Merge branch 'postgresql'
Diffstat (limited to 'server/models/user.js')
-rw-r--r-- | server/models/user.js | 158 |
1 files changed, 106 insertions, 52 deletions
diff --git a/server/models/user.js b/server/models/user.js index a19de7072..36ed723cc 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -1,60 +1,81 @@ | |||
1 | const mongoose = require('mongoose') | 1 | 'use strict' |
2 | |||
3 | const values = require('lodash/values') | ||
2 | 4 | ||
3 | const customUsersValidators = require('../helpers/custom-validators').users | ||
4 | const modelUtils = require('./utils') | 5 | const modelUtils = require('./utils') |
6 | const constants = require('../initializers/constants') | ||
5 | const peertubeCrypto = require('../helpers/peertube-crypto') | 7 | const peertubeCrypto = require('../helpers/peertube-crypto') |
6 | 8 | const customUsersValidators = require('../helpers/custom-validators').users | |
7 | const OAuthToken = mongoose.model('OAuthToken') | ||
8 | 9 | ||
9 | // --------------------------------------------------------------------------- | 10 | // --------------------------------------------------------------------------- |
10 | 11 | ||
11 | const UserSchema = mongoose.Schema({ | 12 | module.exports = function (sequelize, DataTypes) { |
12 | createdDate: { | 13 | const User = sequelize.define('User', |
13 | type: Date, | 14 | { |
14 | default: Date.now | 15 | password: { |
15 | }, | 16 | type: DataTypes.STRING, |
16 | password: String, | 17 | allowNull: false, |
17 | username: String, | 18 | validate: { |
18 | role: String | 19 | passwordValid: function (value) { |
19 | }) | 20 | const res = customUsersValidators.isUserPasswordValid(value) |
20 | 21 | if (res === false) throw new Error('Password not valid.') | |
21 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) | 22 | } |
22 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | 23 | } |
23 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | 24 | }, |
24 | 25 | username: { | |
25 | UserSchema.methods = { | 26 | type: DataTypes.STRING, |
26 | isPasswordMatch, | 27 | allowNull: false, |
27 | toFormatedJSON | 28 | validate: { |
28 | } | 29 | usernameValid: function (value) { |
29 | 30 | const res = customUsersValidators.isUserUsernameValid(value) | |
30 | UserSchema.statics = { | 31 | if (res === false) throw new Error('Username not valid.') |
31 | countTotal, | 32 | } |
32 | getByUsername, | 33 | } |
33 | list, | 34 | }, |
34 | listForApi, | 35 | role: { |
35 | loadById, | 36 | type: DataTypes.ENUM(values(constants.USER_ROLES)), |
36 | loadByUsername | 37 | allowNull: false |
38 | } | ||
39 | }, | ||
40 | { | ||
41 | indexes: [ | ||
42 | { | ||
43 | fields: [ 'username' ] | ||
44 | } | ||
45 | ], | ||
46 | classMethods: { | ||
47 | associate, | ||
48 | |||
49 | countTotal, | ||
50 | getByUsername, | ||
51 | list, | ||
52 | listForApi, | ||
53 | loadById, | ||
54 | loadByUsername | ||
55 | }, | ||
56 | instanceMethods: { | ||
57 | isPasswordMatch, | ||
58 | toFormatedJSON | ||
59 | }, | ||
60 | hooks: { | ||
61 | beforeCreate: beforeCreateOrUpdate, | ||
62 | beforeUpdate: beforeCreateOrUpdate | ||
63 | } | ||
64 | } | ||
65 | ) | ||
66 | |||
67 | return User | ||
37 | } | 68 | } |
38 | 69 | ||
39 | UserSchema.pre('save', function (next) { | 70 | function beforeCreateOrUpdate (user, options, next) { |
40 | const user = this | 71 | peertubeCrypto.cryptPassword(user.password, function (err, hash) { |
41 | |||
42 | peertubeCrypto.cryptPassword(this.password, function (err, hash) { | ||
43 | if (err) return next(err) | 72 | if (err) return next(err) |
44 | 73 | ||
45 | user.password = hash | 74 | user.password = hash |
46 | 75 | ||
47 | return next() | 76 | return next() |
48 | }) | 77 | }) |
49 | }) | 78 | } |
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 | 79 | ||
59 | // ------------------------------ METHODS ------------------------------ | 80 | // ------------------------------ METHODS ------------------------------ |
60 | 81 | ||
@@ -64,35 +85,68 @@ function isPasswordMatch (password, callback) { | |||
64 | 85 | ||
65 | function toFormatedJSON () { | 86 | function toFormatedJSON () { |
66 | return { | 87 | return { |
67 | id: this._id, | 88 | id: this.id, |
68 | username: this.username, | 89 | username: this.username, |
69 | role: this.role, | 90 | role: this.role, |
70 | createdDate: this.createdDate | 91 | createdAt: this.createdAt |
71 | } | 92 | } |
72 | } | 93 | } |
73 | // ------------------------------ STATICS ------------------------------ | 94 | // ------------------------------ STATICS ------------------------------ |
74 | 95 | ||
96 | function associate (models) { | ||
97 | this.hasOne(models.Author, { | ||
98 | foreignKey: 'userId', | ||
99 | onDelete: 'cascade' | ||
100 | }) | ||
101 | |||
102 | this.hasMany(models.OAuthToken, { | ||
103 | foreignKey: 'userId', | ||
104 | onDelete: 'cascade' | ||
105 | }) | ||
106 | } | ||
107 | |||
75 | function countTotal (callback) { | 108 | function countTotal (callback) { |
76 | return this.count(callback) | 109 | return this.count().asCallback(callback) |
77 | } | 110 | } |
78 | 111 | ||
79 | function getByUsername (username) { | 112 | function getByUsername (username) { |
80 | return this.findOne({ username: username }) | 113 | const query = { |
114 | where: { | ||
115 | username: username | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return this.findOne(query) | ||
81 | } | 120 | } |
82 | 121 | ||
83 | function list (callback) { | 122 | function list (callback) { |
84 | return this.find(callback) | 123 | return this.find().asCallback(callback) |
85 | } | 124 | } |
86 | 125 | ||
87 | function listForApi (start, count, sort, callback) { | 126 | function listForApi (start, count, sort, callback) { |
88 | const query = {} | 127 | const query = { |
89 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | 128 | offset: start, |
129 | limit: count, | ||
130 | order: [ modelUtils.getSort(sort) ] | ||
131 | } | ||
132 | |||
133 | return this.findAndCountAll(query).asCallback(function (err, result) { | ||
134 | if (err) return callback(err) | ||
135 | |||
136 | return callback(null, result.rows, result.count) | ||
137 | }) | ||
90 | } | 138 | } |
91 | 139 | ||
92 | function loadById (id, callback) { | 140 | function loadById (id, callback) { |
93 | return this.findById(id, callback) | 141 | return this.findById(id).asCallback(callback) |
94 | } | 142 | } |
95 | 143 | ||
96 | function loadByUsername (username, callback) { | 144 | function loadByUsername (username, callback) { |
97 | return this.findOne({ username: username }, callback) | 145 | const query = { |
146 | where: { | ||
147 | username: username | ||
148 | } | ||
149 | } | ||
150 | |||
151 | return this.findOne(query).asCallback(callback) | ||
98 | } | 152 | } |