diff options
Diffstat (limited to 'server/models/user.js')
-rw-r--r-- | server/models/user.js | 194 |
1 files changed, 0 insertions, 194 deletions
diff --git a/server/models/user.js b/server/models/user.js deleted file mode 100644 index 8f9c2bf65..000000000 --- a/server/models/user.js +++ /dev/null | |||
@@ -1,194 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const values = require('lodash/values') | ||
4 | |||
5 | const modelUtils = require('./utils') | ||
6 | const constants = require('../initializers/constants') | ||
7 | const peertubeCrypto = require('../helpers/peertube-crypto') | ||
8 | const customUsersValidators = require('../helpers/custom-validators').users | ||
9 | |||
10 | // --------------------------------------------------------------------------- | ||
11 | |||
12 | module.exports = function (sequelize, DataTypes) { | ||
13 | const User = sequelize.define('User', | ||
14 | { | ||
15 | password: { | ||
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 | } | ||
24 | }, | ||
25 | username: { | ||
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 | } | ||
34 | }, | ||
35 | email: { | ||
36 | type: DataTypes.STRING(400), | ||
37 | allowNull: false, | ||
38 | validate: { | ||
39 | isEmail: true | ||
40 | } | ||
41 | }, | ||
42 | displayNSFW: { | ||
43 | type: DataTypes.BOOLEAN, | ||
44 | allowNull: false, | ||
45 | defaultValue: false, | ||
46 | validate: { | ||
47 | nsfwValid: function (value) { | ||
48 | const res = customUsersValidators.isUserDisplayNSFWValid(value) | ||
49 | if (res === false) throw new Error('Display NSFW is not valid.') | ||
50 | } | ||
51 | } | ||
52 | }, | ||
53 | role: { | ||
54 | type: DataTypes.ENUM(values(constants.USER_ROLES)), | ||
55 | allowNull: false | ||
56 | } | ||
57 | }, | ||
58 | { | ||
59 | indexes: [ | ||
60 | { | ||
61 | fields: [ 'username' ], | ||
62 | unique: true | ||
63 | }, | ||
64 | { | ||
65 | fields: [ 'email' ], | ||
66 | unique: true | ||
67 | } | ||
68 | ], | ||
69 | classMethods: { | ||
70 | associate, | ||
71 | |||
72 | countTotal, | ||
73 | getByUsername, | ||
74 | list, | ||
75 | listForApi, | ||
76 | loadById, | ||
77 | loadByUsername, | ||
78 | loadByUsernameOrEmail | ||
79 | }, | ||
80 | instanceMethods: { | ||
81 | isPasswordMatch, | ||
82 | toFormatedJSON, | ||
83 | isAdmin | ||
84 | }, | ||
85 | hooks: { | ||
86 | beforeCreate: beforeCreateOrUpdate, | ||
87 | beforeUpdate: beforeCreateOrUpdate | ||
88 | } | ||
89 | } | ||
90 | ) | ||
91 | |||
92 | return User | ||
93 | } | ||
94 | |||
95 | function beforeCreateOrUpdate (user, options, next) { | ||
96 | peertubeCrypto.cryptPassword(user.password, function (err, hash) { | ||
97 | if (err) return next(err) | ||
98 | |||
99 | user.password = hash | ||
100 | |||
101 | return next() | ||
102 | }) | ||
103 | } | ||
104 | |||
105 | // ------------------------------ METHODS ------------------------------ | ||
106 | |||
107 | function isPasswordMatch (password, callback) { | ||
108 | return peertubeCrypto.comparePassword(password, this.password, callback) | ||
109 | } | ||
110 | |||
111 | function toFormatedJSON () { | ||
112 | return { | ||
113 | id: this.id, | ||
114 | username: this.username, | ||
115 | email: this.email, | ||
116 | displayNSFW: this.displayNSFW, | ||
117 | role: this.role, | ||
118 | createdAt: this.createdAt | ||
119 | } | ||
120 | } | ||
121 | |||
122 | function isAdmin () { | ||
123 | return this.role === constants.USER_ROLES.ADMIN | ||
124 | } | ||
125 | |||
126 | // ------------------------------ STATICS ------------------------------ | ||
127 | |||
128 | function associate (models) { | ||
129 | this.hasOne(models.Author, { | ||
130 | foreignKey: 'userId', | ||
131 | onDelete: 'cascade' | ||
132 | }) | ||
133 | |||
134 | this.hasMany(models.OAuthToken, { | ||
135 | foreignKey: 'userId', | ||
136 | onDelete: 'cascade' | ||
137 | }) | ||
138 | } | ||
139 | |||
140 | function countTotal (callback) { | ||
141 | return this.count().asCallback(callback) | ||
142 | } | ||
143 | |||
144 | function getByUsername (username) { | ||
145 | const query = { | ||
146 | where: { | ||
147 | username: username | ||
148 | } | ||
149 | } | ||
150 | |||
151 | return this.findOne(query) | ||
152 | } | ||
153 | |||
154 | function list (callback) { | ||
155 | return this.find().asCallback(callback) | ||
156 | } | ||
157 | |||
158 | function listForApi (start, count, sort, callback) { | ||
159 | const query = { | ||
160 | offset: start, | ||
161 | limit: count, | ||
162 | order: [ modelUtils.getSort(sort) ] | ||
163 | } | ||
164 | |||
165 | return this.findAndCountAll(query).asCallback(function (err, result) { | ||
166 | if (err) return callback(err) | ||
167 | |||
168 | return callback(null, result.rows, result.count) | ||
169 | }) | ||
170 | } | ||
171 | |||
172 | function loadById (id, callback) { | ||
173 | return this.findById(id).asCallback(callback) | ||
174 | } | ||
175 | |||
176 | function loadByUsername (username, callback) { | ||
177 | const query = { | ||
178 | where: { | ||
179 | username: username | ||
180 | } | ||
181 | } | ||
182 | |||
183 | return this.findOne(query).asCallback(callback) | ||
184 | } | ||
185 | |||
186 | function loadByUsernameOrEmail (username, email, callback) { | ||
187 | const query = { | ||
188 | where: { | ||
189 | $or: [ { username }, { email } ] | ||
190 | } | ||
191 | } | ||
192 | |||
193 | return this.findOne(query).asCallback(callback) | ||
194 | } | ||