]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user.js
c9c35b3e2dffc0c06cbe6d35f379135998aae583
[github/Chocobozzz/PeerTube.git] / server / models / user.js
1 const mongoose = require('mongoose')
2
3 const customUsersValidators = require('../helpers/custom-validators').users
4 const modelUtils = require('./utils')
5
6 // ---------------------------------------------------------------------------
7
8 const UserSchema = mongoose.Schema({
9 createdDate: {
10 type: Date,
11 default: Date.now
12 },
13 password: String,
14 username: String,
15 role: String
16 })
17
18 UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
19 UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
20 UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
21
22 UserSchema.methods = {
23 toFormatedJSON: toFormatedJSON
24 }
25
26 UserSchema.statics = {
27 countTotal: countTotal,
28 getByUsernameAndPassword: getByUsernameAndPassword,
29 listForApi: listForApi,
30 loadById: loadById,
31 loadByUsername: loadByUsername
32 }
33
34 mongoose.model('User', UserSchema)
35
36 // ---------------------------------------------------------------------------
37
38 function countTotal (callback) {
39 return this.count(callback)
40 }
41
42 function getByUsernameAndPassword (username, password) {
43 return this.findOne({ username: username, password: password })
44 }
45
46 function listForApi (start, count, sort, callback) {
47 const query = {}
48 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
49 }
50
51 function loadById (id, callback) {
52 return this.findById(id, callback)
53 }
54
55 function loadByUsername (username, callback) {
56 return this.findOne({ username: username }, callback)
57 }
58
59 function toFormatedJSON () {
60 return {
61 id: this._id,
62 username: this.username,
63 role: this.role
64 }
65 }