]>
Commit | Line | Data |
---|---|---|
67bf9b96 C |
1 | 'use strict' |
2 | ||
3 | const values = require('lodash/values') | |
4 | ||
5c39adb7 | 5 | const modelUtils = require('./utils') |
67bf9b96 | 6 | const constants = require('../initializers/constants') |
26d7d31b | 7 | const peertubeCrypto = require('../helpers/peertube-crypto') |
67bf9b96 | 8 | const customUsersValidators = require('../helpers/custom-validators').users |
9bd26629 | 9 | |
69b0a27c C |
10 | // --------------------------------------------------------------------------- |
11 | ||
feb4bdfd C |
12 | module.exports = function (sequelize, DataTypes) { |
13 | const User = sequelize.define('User', | |
14 | { | |
15 | password: { | |
67bf9b96 C |
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 | } | |
feb4bdfd C |
24 | }, |
25 | username: { | |
67bf9b96 C |
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 | } | |
feb4bdfd C |
34 | }, |
35 | role: { | |
67bf9b96 C |
36 | type: DataTypes.ENUM(values(constants.USER_ROLES)), |
37 | allowNull: false | |
feb4bdfd C |
38 | } |
39 | }, | |
40 | { | |
319d072e C |
41 | indexes: [ |
42 | { | |
5d67f289 C |
43 | fields: [ 'username' ], |
44 | unique: true | |
319d072e C |
45 | } |
46 | ], | |
feb4bdfd C |
47 | classMethods: { |
48 | associate, | |
49 | ||
50 | countTotal, | |
51 | getByUsername, | |
52 | list, | |
53 | listForApi, | |
54 | loadById, | |
55 | loadByUsername | |
56 | }, | |
57 | instanceMethods: { | |
58 | isPasswordMatch, | |
59 | toFormatedJSON | |
60 | }, | |
61 | hooks: { | |
62 | beforeCreate: beforeCreateOrUpdate, | |
63 | beforeUpdate: beforeCreateOrUpdate | |
64 | } | |
65 | } | |
66 | ) | |
67 | ||
68 | return User | |
9bd26629 | 69 | } |
69b0a27c | 70 | |
feb4bdfd C |
71 | function beforeCreateOrUpdate (user, options, next) { |
72 | peertubeCrypto.cryptPassword(user.password, function (err, hash) { | |
26d7d31b C |
73 | if (err) return next(err) |
74 | ||
75 | user.password = hash | |
76 | ||
77 | return next() | |
78 | }) | |
feb4bdfd | 79 | } |
69b0a27c | 80 | |
26d7d31b C |
81 | // ------------------------------ METHODS ------------------------------ |
82 | ||
83 | function isPasswordMatch (password, callback) { | |
84 | return peertubeCrypto.comparePassword(password, this.password, callback) | |
85 | } | |
86 | ||
87 | function toFormatedJSON () { | |
88 | return { | |
feb4bdfd | 89 | id: this.id, |
26d7d31b | 90 | username: this.username, |
d74a0680 | 91 | role: this.role, |
feb4bdfd | 92 | createdAt: this.createdAt |
26d7d31b C |
93 | } |
94 | } | |
95 | // ------------------------------ STATICS ------------------------------ | |
69b0a27c | 96 | |
feb4bdfd | 97 | function associate (models) { |
4712081f C |
98 | this.hasOne(models.Author, { |
99 | foreignKey: 'userId', | |
100 | onDelete: 'cascade' | |
101 | }) | |
102 | ||
feb4bdfd C |
103 | this.hasMany(models.OAuthToken, { |
104 | foreignKey: 'userId', | |
105 | onDelete: 'cascade' | |
106 | }) | |
107 | } | |
108 | ||
5c39adb7 | 109 | function countTotal (callback) { |
feb4bdfd | 110 | return this.count().asCallback(callback) |
089ff2f2 C |
111 | } |
112 | ||
26d7d31b | 113 | function getByUsername (username) { |
feb4bdfd C |
114 | const query = { |
115 | where: { | |
116 | username: username | |
117 | } | |
118 | } | |
119 | ||
120 | return this.findOne(query) | |
9bd26629 C |
121 | } |
122 | ||
00d6b0dd | 123 | function list (callback) { |
feb4bdfd | 124 | return this.find().asCallback(callback) |
00d6b0dd C |
125 | } |
126 | ||
5c39adb7 | 127 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
128 | const query = { |
129 | offset: start, | |
130 | limit: count, | |
131 | order: [ modelUtils.getSort(sort) ] | |
132 | } | |
133 | ||
134 | return this.findAndCountAll(query).asCallback(function (err, result) { | |
135 | if (err) return callback(err) | |
136 | ||
137 | return callback(null, result.rows, result.count) | |
138 | }) | |
69b0a27c C |
139 | } |
140 | ||
68a3b9f2 | 141 | function loadById (id, callback) { |
feb4bdfd | 142 | return this.findById(id).asCallback(callback) |
68a3b9f2 C |
143 | } |
144 | ||
9bd26629 | 145 | function loadByUsername (username, callback) { |
feb4bdfd C |
146 | const query = { |
147 | where: { | |
148 | username: username | |
149 | } | |
150 | } | |
151 | ||
152 | return this.findOne(query).asCallback(callback) | |
9bd26629 | 153 | } |