]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users.ts
Add ability to limit user registrations
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
9457bf88 2
e02643f3 3import { database as db } from '../../initializers/database'
6fcd19ba 4import { USER_ROLES } from '../../initializers'
65fcc311
C
5import { logger, getFormatedObjects } from '../../helpers'
6import {
7 authenticate,
8 ensureIsAdmin,
291e8d3e 9 ensureUserRegistrationAllowed,
65fcc311
C
10 usersAddValidator,
11 usersUpdateValidator,
12 usersRemoveValidator,
13 usersVideoRatingValidator,
14 paginationValidator,
15 setPagination,
16 usersSortValidator,
17 setUsersSort,
18 token
19} from '../../middlewares'
4771e000 20import { UserVideoRate as FormatedUserVideoRate, UserCreate, UserUpdate } from '../../../shared'
65fcc311
C
21
22const usersRouter = express.Router()
23
24usersRouter.get('/me',
25 authenticate,
d38b8281
C
26 getUserInformation
27)
28
65fcc311
C
29usersRouter.get('/me/videos/:videoId/rating',
30 authenticate,
31 usersVideoRatingValidator,
d38b8281
C
32 getUserVideoRating
33)
9bd26629 34
65fcc311
C
35usersRouter.get('/',
36 paginationValidator,
37 usersSortValidator,
38 setUsersSort,
39 setPagination,
5c39adb7
C
40 listUsers
41)
42
65fcc311
C
43usersRouter.post('/',
44 authenticate,
45 ensureIsAdmin,
46 usersAddValidator,
9bd26629
C
47 createUser
48)
49
65fcc311 50usersRouter.post('/register',
291e8d3e 51 ensureUserRegistrationAllowed,
65fcc311 52 usersAddValidator,
2c2e9092
C
53 createUser
54)
55
65fcc311
C
56usersRouter.put('/:id',
57 authenticate,
58 usersUpdateValidator,
9bd26629
C
59 updateUser
60)
61
65fcc311
C
62usersRouter.delete('/:id',
63 authenticate,
64 ensureIsAdmin,
65 usersRemoveValidator,
9bd26629
C
66 removeUser
67)
6606150c 68
65fcc311 69usersRouter.post('/token', token, success)
9bd26629 70// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
9457bf88
C
71
72// ---------------------------------------------------------------------------
73
65fcc311
C
74export {
75 usersRouter
76}
9457bf88
C
77
78// ---------------------------------------------------------------------------
79
69818c93 80function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
4771e000
C
81 const body: UserCreate = req.body
82
feb4bdfd 83 const user = db.User.build({
4771e000
C
84 username: body.username,
85 password: body.password,
86 email: body.email,
1d49e1e2 87 displayNSFW: false,
65fcc311 88 role: USER_ROLES.USER
9bd26629
C
89 })
90
6fcd19ba
C
91 user.save()
92 .then(() => res.type('json').status(204).end())
93 .catch(err => next(err))
9bd26629
C
94}
95
69818c93 96function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
97 db.User.loadByUsername(res.locals.oauth.token.user.username)
98 .then(user => res.json(user.toFormatedJSON()))
99 .catch(err => next(err))
99a64bfe
C
100}
101
69818c93 102function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
0a6658fd 103 const videoId = +req.params.videoId
69818c93 104 const userId = +res.locals.oauth.token.User.id
d38b8281 105
6fcd19ba
C
106 db.UserVideoRate.load(userId, videoId, null)
107 .then(ratingObj => {
108 const rating = ratingObj ? ratingObj.type : 'none'
109 const json: FormatedUserVideoRate = {
110 videoId,
111 rating
112 }
113 res.json(json)
114 })
115 .catch(err => next(err))
d38b8281
C
116}
117
69818c93 118function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
119 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
120 .then(resultList => {
121 res.json(getFormatedObjects(resultList.data, resultList.total))
122 })
123 .catch(err => next(err))
9bd26629
C
124}
125
69818c93 126function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
127 db.User.loadById(req.params.id)
128 .then(user => user.destroy())
129 .then(() => res.sendStatus(204))
130 .catch(err => {
ad0997ad 131 logger.error('Errors when removed the user.', err)
9bd26629 132 return next(err)
6fcd19ba 133 })
9bd26629
C
134}
135
69818c93 136function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
4771e000
C
137 const body: UserUpdate = req.body
138
6fcd19ba
C
139 db.User.loadByUsername(res.locals.oauth.token.user.username)
140 .then(user => {
4771e000
C
141 if (body.password) user.password = body.password
142 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
1d49e1e2 143
6fcd19ba 144 return user.save()
9bd26629 145 })
6fcd19ba
C
146 .then(() => res.sendStatus(204))
147 .catch(err => next(err))
9bd26629
C
148}
149
69818c93 150function success (req: express.Request, res: express.Response, next: express.NextFunction) {
9457bf88
C
151 res.end()
152}