diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-04 22:32:36 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-04 22:33:38 +0200 |
commit | 9bd2662976a75d3b03364cdbe6419e57c80f99a6 (patch) | |
tree | 0b5289660f843a8ba7f13aa79d458f53c94b36d9 /server/models | |
parent | e4c556196d7b31111f17596840d2e1d60caa7dcb (diff) | |
download | PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.gz PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.zst PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.zip |
Implement user API (create, update, remove, list)
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/user.js | 33 | ||||
-rw-r--r-- | server/models/video.js | 5 |
2 files changed, 32 insertions, 6 deletions
diff --git a/server/models/user.js b/server/models/user.js index 14ffecbff..0bbd638d4 100644 --- a/server/models/user.js +++ b/server/models/user.js | |||
@@ -1,28 +1,49 @@ | |||
1 | const mongoose = require('mongoose') | 1 | const mongoose = require('mongoose') |
2 | 2 | ||
3 | const customUsersValidators = require('../helpers/custom-validators').users | ||
4 | |||
3 | // --------------------------------------------------------------------------- | 5 | // --------------------------------------------------------------------------- |
4 | 6 | ||
5 | const UserSchema = mongoose.Schema({ | 7 | const UserSchema = mongoose.Schema({ |
6 | password: String, | 8 | password: String, |
7 | username: String | 9 | username: String, |
10 | role: String | ||
8 | }) | 11 | }) |
9 | 12 | ||
10 | UserSchema.path('password').required(true) | 13 | UserSchema.path('password').required(customUsersValidators.isUserPasswordValid) |
11 | UserSchema.path('username').required(true) | 14 | UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) |
15 | UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) | ||
16 | |||
17 | UserSchema.methods = { | ||
18 | toFormatedJSON: toFormatedJSON | ||
19 | } | ||
12 | 20 | ||
13 | UserSchema.statics = { | 21 | UserSchema.statics = { |
14 | getByUsernameAndPassword: getByUsernameAndPassword, | 22 | getByUsernameAndPassword: getByUsernameAndPassword, |
15 | list: list | 23 | list: list, |
24 | loadByUsername: loadByUsername | ||
16 | } | 25 | } |
17 | 26 | ||
18 | mongoose.model('User', UserSchema) | 27 | mongoose.model('User', UserSchema) |
19 | 28 | ||
20 | // --------------------------------------------------------------------------- | 29 | // --------------------------------------------------------------------------- |
21 | 30 | ||
31 | function getByUsernameAndPassword (username, password) { | ||
32 | return this.findOne({ username: username, password: password }) | ||
33 | } | ||
34 | |||
22 | function list (callback) { | 35 | function list (callback) { |
23 | return this.find(callback) | 36 | return this.find(callback) |
24 | } | 37 | } |
25 | 38 | ||
26 | function getByUsernameAndPassword (username, password) { | 39 | function loadByUsername (username, callback) { |
27 | return this.findOne({ username: username, password: password }) | 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 | } | ||
28 | } | 49 | } |
diff --git a/server/models/video.js b/server/models/video.js index acb8353c2..14bc91b16 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -64,6 +64,7 @@ VideoSchema.statics = { | |||
64 | listByUrlAndMagnet: listByUrlAndMagnet, | 64 | listByUrlAndMagnet: listByUrlAndMagnet, |
65 | listByUrls: listByUrls, | 65 | listByUrls: listByUrls, |
66 | listOwned: listOwned, | 66 | listOwned: listOwned, |
67 | listOwnedByAuthor: listOwnedByAuthor, | ||
67 | listRemotes: listRemotes, | 68 | listRemotes: listRemotes, |
68 | load: load, | 69 | load: load, |
69 | search: search, | 70 | search: search, |
@@ -211,6 +212,10 @@ function listOwned (callback) { | |||
211 | this.find({ filename: { $ne: null } }, callback) | 212 | this.find({ filename: { $ne: null } }, callback) |
212 | } | 213 | } |
213 | 214 | ||
215 | function listOwnedByAuthor (author, callback) { | ||
216 | this.find({ filename: { $ne: null }, author: author }, callback) | ||
217 | } | ||
218 | |||
214 | function listRemotes (callback) { | 219 | function listRemotes (callback) { |
215 | this.find({ filename: null }, callback) | 220 | this.find({ filename: null }, callback) |
216 | } | 221 | } |