aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-04 22:32:36 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-04 22:33:38 +0200
commit9bd2662976a75d3b03364cdbe6419e57c80f99a6 (patch)
tree0b5289660f843a8ba7f13aa79d458f53c94b36d9 /server/controllers/api/v1
parente4c556196d7b31111f17596840d2e1d60caa7dcb (diff)
downloadPeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.gz
PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.tar.zst
PeerTube-9bd2662976a75d3b03364cdbe6419e57c80f99a6.zip
Implement user API (create, update, remove, list)
Diffstat (limited to 'server/controllers/api/v1')
-rw-r--r--server/controllers/api/v1/pods.js14
-rw-r--r--server/controllers/api/v1/users.js132
2 files changed, 141 insertions, 5 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index 2bc761fef..f61f2a483 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -8,6 +8,7 @@ const waterfall = require('async/waterfall')
8const logger = require('../../../helpers/logger') 8const logger = require('../../../helpers/logger')
9const friends = require('../../../lib/friends') 9const friends = require('../../../lib/friends')
10const middlewares = require('../../../middlewares') 10const middlewares = require('../../../middlewares')
11const admin = middlewares.admin
11const oAuth = middlewares.oauth 12const oAuth = middlewares.oauth
12const validators = middlewares.validators.pods 13const validators = middlewares.validators.pods
13const signatureValidator = middlewares.validators.remote.signature 14const signatureValidator = middlewares.validators.remote.signature
@@ -18,8 +19,17 @@ const Video = mongoose.model('Video')
18 19
19router.get('/', listPodsUrl) 20router.get('/', listPodsUrl)
20router.post('/', validators.podsAdd, addPods) 21router.post('/', validators.podsAdd, addPods)
21router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) 22router.get('/makefriends',
22router.get('/quitfriends', oAuth.authenticate, quitFriends) 23 oAuth.authenticate,
24 admin.ensureIsAdmin,
25 validators.makeFriends,
26 makeFriends
27)
28router.get('/quitfriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 quitFriends
32)
23// Post because this is a secured request 33// Post because this is a secured request
24router.post('/remove', signatureValidator, removePods) 34router.post('/remove', signatureValidator, removePods)
25 35
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js
index fbbe6e472..e084974ce 100644
--- a/server/controllers/api/v1/users.js
+++ b/server/controllers/api/v1/users.js
@@ -1,18 +1,49 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
3const config = require('config') 4const config = require('config')
4const mongoose = require('mongoose')
5const express = require('express') 5const express = require('express')
6const mongoose = require('mongoose')
7const waterfall = require('async/waterfall')
6 8
7const oAuth = require('../../../middlewares').oauth 9const constants = require('../../../initializers/constants')
10const friends = require('../../../lib/friends')
11const logger = require('../../../helpers/logger')
12const middlewares = require('../../../middlewares')
13const admin = middlewares.admin
14const oAuth = middlewares.oauth
15const validatorsUsers = middlewares.validators.users
8 16
9const Client = mongoose.model('OAuthClient') 17const Client = mongoose.model('OAuthClient')
18const User = mongoose.model('User')
19const Video = mongoose.model('Video')
10 20
11const router = express.Router() 21const router = express.Router()
12 22
23router.get('/', listUsers)
24
25router.post('/',
26 oAuth.authenticate,
27 admin.ensureIsAdmin,
28 validatorsUsers.usersAdd,
29 createUser
30)
31
32router.put('/:id',
33 oAuth.authenticate,
34 validatorsUsers.usersUpdate,
35 updateUser
36)
37
38router.delete('/:username',
39 oAuth.authenticate,
40 admin.ensureIsAdmin,
41 validatorsUsers.usersRemove,
42 removeUser
43)
13router.get('/client', getAngularClient) 44router.get('/client', getAngularClient)
14router.post('/token', oAuth.token, success) 45router.post('/token', oAuth.token, success)
15// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged,, implement revoke token route 46// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
16 47
17// --------------------------------------------------------------------------- 48// ---------------------------------------------------------------------------
18 49
@@ -20,6 +51,20 @@ module.exports = router
20 51
21// --------------------------------------------------------------------------- 52// ---------------------------------------------------------------------------
22 53
54function createUser (req, res, next) {
55 const user = new User({
56 username: req.body.username,
57 password: req.body.password,
58 role: constants.USER_ROLES.USER
59 })
60
61 user.save(function (err, createdUser) {
62 if (err) return next(err)
63
64 return res.type('json').status(204).end()
65 })
66}
67
23function getAngularClient (req, res, next) { 68function getAngularClient (req, res, next) {
24 const serverHost = config.get('webserver.host') 69 const serverHost = config.get('webserver.host')
25 const serverPort = config.get('webserver.port') 70 const serverPort = config.get('webserver.port')
@@ -44,6 +89,87 @@ function getAngularClient (req, res, next) {
44 }) 89 })
45} 90}
46 91
92function listUsers (req, res, next) {
93 User.list(function (err, usersList) {
94 if (err) return next(err)
95
96 res.json(getFormatedUsers(usersList))
97 })
98}
99
100function removeUser (req, res, next) {
101 waterfall([
102 function getUser (callback) {
103 User.loadByUsername(req.params.username, callback)
104 },
105
106 function getVideos (user, callback) {
107 Video.listOwnedByAuthor(user.username, function (err, videos) {
108 return callback(err, user, videos)
109 })
110 },
111
112 function removeVideosFromDB (user, videos, callback) {
113 each(videos, function (video, callbackEach) {
114 video.remove(callbackEach)
115 }, function (err) {
116 return callback(err, user, videos)
117 })
118 },
119
120 function sendInformationToFriends (user, videos, callback) {
121 videos.forEach(function (video) {
122 const params = {
123 name: video.name,
124 magnetUri: video.magnetUri
125 }
126
127 friends.removeVideoToFriends(params)
128 })
129
130 return callback(null, user)
131 },
132
133 function removeUserFromDB (user, callback) {
134 user.remove(callback)
135 }
136 ], function andFinally (err) {
137 if (err) {
138 logger.error('Errors when removed the user.', { error: err })
139 return next(err)
140 }
141
142 return res.type('json').status(204).end()
143 })
144}
145
146function updateUser (req, res, next) {
147 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
148 if (err) return next(err)
149
150 user.password = req.body.password
151 user.save(function (err) {
152 if (err) return next(err)
153
154 return res.json('json').status(204).end()
155 })
156 })
157}
158
47function success (req, res, next) { 159function success (req, res, next) {
48 res.end() 160 res.end()
49} 161}
162
163// ---------------------------------------------------------------------------
164
165function getFormatedUsers (users) {
166 const formatedUsers = []
167
168 users.forEach(function (user) {
169 formatedUsers.push(user.toFormatedJSON())
170 })
171
172 return {
173 data: formatedUsers
174 }
175}