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