]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users.js
Server: move remote routes in their own directory
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.js
CommitLineData
9457bf88
C
1'use strict'
2
0c1cbbfe 3const express = require('express')
9bd26629 4const waterfall = require('async/waterfall')
fc51fde0 5
f253b1c1 6const constants = require('../../initializers/constants')
feb4bdfd 7const db = require('../../initializers/database')
f253b1c1
C
8const logger = require('../../helpers/logger')
9const middlewares = require('../../middlewares')
9bd26629
C
10const admin = middlewares.admin
11const oAuth = middlewares.oauth
5c39adb7
C
12const pagination = middlewares.pagination
13const sort = middlewares.sort
14const validatorsPagination = middlewares.validators.pagination
15const validatorsSort = middlewares.validators.sort
9bd26629 16const validatorsUsers = middlewares.validators.users
9457bf88 17
9457bf88
C
18const router = express.Router()
19
99a64bfe 20router.get('/me', oAuth.authenticate, getUserInformation)
9bd26629 21
5c39adb7
C
22router.get('/',
23 validatorsPagination.pagination,
24 validatorsSort.usersSort,
25 sort.setUsersSort,
26 pagination.setPagination,
27 listUsers
28)
29
9bd26629
C
30router.post('/',
31 oAuth.authenticate,
32 admin.ensureIsAdmin,
33 validatorsUsers.usersAdd,
34 createUser
35)
36
37router.put('/:id',
38 oAuth.authenticate,
39 validatorsUsers.usersUpdate,
40 updateUser
41)
42
68a3b9f2 43router.delete('/:id',
9bd26629
C
44 oAuth.authenticate,
45 admin.ensureIsAdmin,
46 validatorsUsers.usersRemove,
47 removeUser
48)
6606150c 49
69b0a27c 50router.post('/token', oAuth.token, success)
9bd26629 51// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
9457bf88
C
52
53// ---------------------------------------------------------------------------
54
55module.exports = router
56
57// ---------------------------------------------------------------------------
58
9bd26629 59function createUser (req, res, next) {
feb4bdfd 60 const user = db.User.build({
9bd26629
C
61 username: req.body.username,
62 password: req.body.password,
63 role: constants.USER_ROLES.USER
64 })
65
feb4bdfd 66 user.save().asCallback(function (err, createdUser) {
9bd26629
C
67 if (err) return next(err)
68
69 return res.type('json').status(204).end()
70 })
71}
72
99a64bfe 73function getUserInformation (req, res, next) {
feb4bdfd 74 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
99a64bfe
C
75 if (err) return next(err)
76
77 return res.json(user.toFormatedJSON())
78 })
79}
80
9bd26629 81function listUsers (req, res, next) {
feb4bdfd 82 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
9bd26629
C
83 if (err) return next(err)
84
5c39adb7 85 res.json(getFormatedUsers(usersList, usersTotal))
9bd26629
C
86 })
87}
88
89function removeUser (req, res, next) {
90 waterfall([
98ac898a 91 function loadUser (callback) {
feb4bdfd 92 db.User.loadById(req.params.id, callback)
9bd26629
C
93 },
94
98ac898a 95 function deleteUser (user, callback) {
feb4bdfd 96 user.destroy().asCallback(callback)
9bd26629
C
97 }
98 ], function andFinally (err) {
99 if (err) {
100 logger.error('Errors when removed the user.', { error: err })
101 return next(err)
102 }
103
10431358 104 return res.sendStatus(204)
9bd26629
C
105 })
106}
107
108function updateUser (req, res, next) {
feb4bdfd 109 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
9bd26629
C
110 if (err) return next(err)
111
112 user.password = req.body.password
feb4bdfd 113 user.save().asCallback(function (err) {
9bd26629
C
114 if (err) return next(err)
115
10431358 116 return res.sendStatus(204)
9bd26629
C
117 })
118 })
119}
120
9457bf88
C
121function success (req, res, next) {
122 res.end()
123}
9bd26629
C
124
125// ---------------------------------------------------------------------------
126
5c39adb7 127function getFormatedUsers (users, usersTotal) {
9bd26629
C
128 const formatedUsers = []
129
130 users.forEach(function (user) {
131 formatedUsers.push(user.toFormatedJSON())
132 })
133
134 return {
5c39adb7 135 total: usersTotal,
9bd26629
C
136 data: formatedUsers
137 }
138}