]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user.js
Client: add user management
[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 getByUsernameAndPassword: getByUsernameAndPassword,
23 list: list,
24 loadById: loadById,
25 loadByUsername: loadByUsername
26 }
27
28 mongoose.model('User', UserSchema)
29
30 // ---------------------------------------------------------------------------
31
32 function getByUsernameAndPassword (username, password) {
33 return this.findOne({ username: username, password: password })
34 }
35
36 function list (callback) {
37 return this.find(callback)
38 }
39
40 function loadById (id, callback) {
41 return this.findById(id, callback)
42 }
43
44 function loadByUsername (username, callback) {
45 return this.findOne({ username: username }, callback)
46 }
47
48 function toFormatedJSON () {
49 return {
50 id: this._id,
51 username: this.username,
52 role: this.role
53 }
54 }