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.js32
1 files changed, 24 insertions, 8 deletions
diff --git a/server/models/user.js b/server/models/user.js
index e50eb96ea..944986a44 100644
--- a/server/models/user.js
+++ b/server/models/user.js
@@ -1,5 +1,11 @@
1'use strict'
2
3const values = require('lodash/values')
4
1const modelUtils = require('./utils') 5const modelUtils = require('./utils')
6const constants = require('../initializers/constants')
2const peertubeCrypto = require('../helpers/peertube-crypto') 7const peertubeCrypto = require('../helpers/peertube-crypto')
8const customUsersValidators = require('../helpers/custom-validators').users
3 9
4// --------------------------------------------------------------------------- 10// ---------------------------------------------------------------------------
5 11
@@ -7,13 +13,28 @@ module.exports = function (sequelize, DataTypes) {
7 const User = sequelize.define('User', 13 const User = sequelize.define('User',
8 { 14 {
9 password: { 15 password: {
10 type: DataTypes.STRING 16 type: DataTypes.STRING,
17 allowNull: false,
18 validate: {
19 passwordValid: function (value) {
20 const res = customUsersValidators.isUserPasswordValid(value)
21 if (res === false) throw new Error('Password not valid.')
22 }
23 }
11 }, 24 },
12 username: { 25 username: {
13 type: DataTypes.STRING 26 type: DataTypes.STRING,
27 allowNull: false,
28 validate: {
29 usernameValid: function (value) {
30 const res = customUsersValidators.isUserUsernameValid(value)
31 if (res === false) throw new Error('Username not valid.')
32 }
33 }
14 }, 34 },
15 role: { 35 role: {
16 type: DataTypes.STRING 36 type: DataTypes.ENUM(values(constants.USER_ROLES)),
37 allowNull: false
17 } 38 }
18 }, 39 },
19 { 40 {
@@ -41,11 +62,6 @@ module.exports = function (sequelize, DataTypes) {
41 return User 62 return User
42} 63}
43 64
44// TODO: Validation
45// UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
46// UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
47// UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
48
49function beforeCreateOrUpdate (user, options, next) { 65function beforeCreateOrUpdate (user, options, next) {
50 peertubeCrypto.cryptPassword(user.password, function (err, hash) { 66 peertubeCrypto.cryptPassword(user.password, function (err, hash) {
51 if (err) return next(err) 67 if (err) return next(err)