diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-28 15:49:23 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-28 15:49:23 +0100 |
commit | 67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677 (patch) | |
tree | bae6a9b0c3133c9cc38a2972222b5991f0cf614e /server/models/user.js | |
parent | 552cc9d646e78edae8b0fe61564d4e49db0b6206 (diff) | |
download | PeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.tar.gz PeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.tar.zst PeerTube-67bf9b96bbcd92b069fe86d9223fe0f8b9c6e677.zip |
Server: add database field validations
Diffstat (limited to 'server/models/user.js')
-rw-r--r-- | server/models/user.js | 32 |
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 | |||
3 | const values = require('lodash/values') | ||
4 | |||
1 | const modelUtils = require('./utils') | 5 | const modelUtils = require('./utils') |
6 | const constants = require('../initializers/constants') | ||
2 | const peertubeCrypto = require('../helpers/peertube-crypto') | 7 | const peertubeCrypto = require('../helpers/peertube-crypto') |
8 | const 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 | |||
49 | function beforeCreateOrUpdate (user, options, next) { | 65 | function 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) |