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