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