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