]>
Commit | Line | Data |
---|---|---|
5c39adb7 | 1 | const modelUtils = require('./utils') |
26d7d31b | 2 | const peertubeCrypto = require('../helpers/peertube-crypto') |
9bd26629 | 3 | |
69b0a27c C |
4 | // --------------------------------------------------------------------------- |
5 | ||
feb4bdfd C |
6 | module.exports = function (sequelize, DataTypes) { |
7 | const User = sequelize.define('User', | |
8 | { | |
9 | password: { | |
10 | type: DataTypes.STRING | |
11 | }, | |
12 | username: { | |
13 | type: DataTypes.STRING | |
14 | }, | |
15 | role: { | |
16 | type: DataTypes.STRING | |
17 | } | |
18 | }, | |
19 | { | |
20 | classMethods: { | |
21 | associate, | |
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 | |
9bd26629 | 42 | } |
69b0a27c | 43 | |
feb4bdfd C |
44 | // TODO: Validation |
45 | // UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) | |
46 | // UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) | |
47 | // UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | |
69b0a27c | 48 | |
feb4bdfd C |
49 | function beforeCreateOrUpdate (user, options, next) { |
50 | peertubeCrypto.cryptPassword(user.password, function (err, hash) { | |
26d7d31b C |
51 | if (err) return next(err) |
52 | ||
53 | user.password = hash | |
54 | ||
55 | return next() | |
56 | }) | |
feb4bdfd | 57 | } |
69b0a27c | 58 | |
26d7d31b C |
59 | // ------------------------------ METHODS ------------------------------ |
60 | ||
61 | function isPasswordMatch (password, callback) { | |
62 | return peertubeCrypto.comparePassword(password, this.password, callback) | |
63 | } | |
64 | ||
65 | function toFormatedJSON () { | |
66 | return { | |
feb4bdfd | 67 | id: this.id, |
26d7d31b | 68 | username: this.username, |
d74a0680 | 69 | role: this.role, |
feb4bdfd | 70 | createdAt: this.createdAt |
26d7d31b C |
71 | } |
72 | } | |
73 | // ------------------------------ STATICS ------------------------------ | |
69b0a27c | 74 | |
feb4bdfd C |
75 | function associate (models) { |
76 | this.hasMany(models.OAuthToken, { | |
77 | foreignKey: 'userId', | |
78 | onDelete: 'cascade' | |
79 | }) | |
80 | } | |
81 | ||
5c39adb7 | 82 | function countTotal (callback) { |
feb4bdfd | 83 | return this.count().asCallback(callback) |
089ff2f2 C |
84 | } |
85 | ||
26d7d31b | 86 | function getByUsername (username) { |
feb4bdfd C |
87 | const query = { |
88 | where: { | |
89 | username: username | |
90 | } | |
91 | } | |
92 | ||
93 | return this.findOne(query) | |
9bd26629 C |
94 | } |
95 | ||
00d6b0dd | 96 | function list (callback) { |
feb4bdfd | 97 | return this.find().asCallback(callback) |
00d6b0dd C |
98 | } |
99 | ||
5c39adb7 | 100 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
101 | const query = { |
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 | }) | |
69b0a27c C |
112 | } |
113 | ||
68a3b9f2 | 114 | function loadById (id, callback) { |
feb4bdfd | 115 | return this.findById(id).asCallback(callback) |
68a3b9f2 C |
116 | } |
117 | ||
9bd26629 | 118 | function loadByUsername (username, callback) { |
feb4bdfd C |
119 | const query = { |
120 | where: { | |
121 | username: username | |
122 | } | |
123 | } | |
124 | ||
125 | return this.findOne(query).asCallback(callback) | |
9bd26629 | 126 | } |