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