]>
Commit | Line | Data |
---|---|---|
72c7248b | 1 | import * as express from 'express' |
d4f1e94c C |
2 | import { body, param } from 'express-validator/check' |
3 | import { UserRight } from '../../../shared' | |
4e50b6a1 C |
4 | import { isIdValid } from '../../helpers/custom-validators/misc' |
5 | import { | |
4e50b6a1 | 6 | isVideoChannelDescriptionValid, |
a2431b7d | 7 | isVideoChannelExist, |
4e50b6a1 C |
8 | isVideoChannelNameValid |
9 | } from '../../helpers/custom-validators/video-channels' | |
10 | import { isIdOrUUIDValid } from '../../helpers/index' | |
d4f1e94c | 11 | import { logger } from '../../helpers/logger' |
72c7248b | 12 | import { database as db } from '../../initializers' |
954605a8 | 13 | import { UserInstance } from '../../models' |
a2431b7d C |
14 | import { areValidationErrors } from './utils' |
15 | import { isAccountIdExist } from '../../helpers/custom-validators/accounts' | |
16 | import { VideoChannelInstance } from '../../models/video/video-channel-interface' | |
72c7248b | 17 | |
38fa2065 C |
18 | const listVideoAccountChannelsValidator = [ |
19 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), | |
72c7248b | 20 | |
a2431b7d | 21 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
38fa2065 | 22 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
72c7248b | 23 | |
a2431b7d C |
24 | if (areValidationErrors(req, res)) return |
25 | if (!await isAccountIdExist(req.params.accountId, res)) return | |
26 | ||
27 | return next() | |
72c7248b C |
28 | } |
29 | ] | |
30 | ||
31 | const videoChannelsAddValidator = [ | |
32 | body('name').custom(isVideoChannelNameValid).withMessage('Should have a valid name'), | |
33 | body('description').custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
34 | ||
35 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
36 | logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body }) | |
37 | ||
a2431b7d C |
38 | if (areValidationErrors(req, res)) return |
39 | ||
40 | return next() | |
72c7248b C |
41 | } |
42 | ] | |
43 | ||
44 | const videoChannelsUpdateValidator = [ | |
45 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
46 | body('name').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid name'), | |
47 | body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'), | |
48 | ||
a2431b7d | 49 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
50 | logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body }) |
51 | ||
a2431b7d C |
52 | if (areValidationErrors(req, res)) return |
53 | if (!await isVideoChannelExist(req.params.id, res)) return | |
54 | ||
55 | // We need to make additional checks | |
56 | if (res.locals.videoChannel.isOwned() === false) { | |
57 | return res.status(403) | |
58 | .json({ error: 'Cannot update video channel of another server' }) | |
59 | .end() | |
60 | } | |
61 | ||
62 | if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) { | |
63 | return res.status(403) | |
64 | .json({ error: 'Cannot update video channel of another user' }) | |
65 | .end() | |
66 | } | |
67 | ||
68 | return next() | |
72c7248b C |
69 | } |
70 | ] | |
71 | ||
72 | const videoChannelsRemoveValidator = [ | |
73 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
74 | ||
a2431b7d | 75 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
76 | logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params }) |
77 | ||
a2431b7d C |
78 | if (areValidationErrors(req, res)) return |
79 | if (!await isVideoChannelExist(req.params.id, res)) return | |
80 | ||
81 | // Check if the user who did the request is able to delete the video | |
82 | if (!checkUserCanDeleteVideoChannel(res.locals.user, res.locals.videoChannel, res)) return | |
83 | if (!await checkVideoChannelIsNotTheLastOne(res)) return | |
84 | ||
85 | return next() | |
72c7248b C |
86 | } |
87 | ] | |
88 | ||
20494f12 | 89 | const videoChannelsGetValidator = [ |
72c7248b C |
90 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
91 | ||
a2431b7d | 92 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
72c7248b C |
93 | logger.debug('Checking videoChannelsGet parameters', { parameters: req.params }) |
94 | ||
a2431b7d C |
95 | if (areValidationErrors(req, res)) return |
96 | if (!await isVideoChannelExist(req.params.id, res)) return | |
97 | ||
98 | return next() | |
72c7248b C |
99 | } |
100 | ] | |
101 | ||
4e50b6a1 C |
102 | const videoChannelsShareValidator = [ |
103 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
104 | param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), | |
105 | ||
106 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
107 | logger.debug('Checking videoChannelShare parameters', { parameters: req.params }) | |
108 | ||
109 | if (areValidationErrors(req, res)) return | |
a2431b7d | 110 | if (!await isVideoChannelExist(req.params.id, res)) return |
4e50b6a1 | 111 | |
98ec8b8e | 112 | const share = await db.VideoChannelShare.load(res.locals.video.id, req.params.accountId, undefined) |
4e50b6a1 C |
113 | if (!share) { |
114 | return res.status(404) | |
115 | .end() | |
116 | } | |
117 | ||
118 | res.locals.videoChannelShare = share | |
119 | ||
120 | return next() | |
121 | } | |
122 | ] | |
123 | ||
72c7248b C |
124 | // --------------------------------------------------------------------------- |
125 | ||
126 | export { | |
38fa2065 | 127 | listVideoAccountChannelsValidator, |
72c7248b C |
128 | videoChannelsAddValidator, |
129 | videoChannelsUpdateValidator, | |
130 | videoChannelsRemoveValidator, | |
4e50b6a1 C |
131 | videoChannelsGetValidator, |
132 | videoChannelsShareValidator | |
72c7248b C |
133 | } |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | ||
a2431b7d | 137 | function checkUserCanDeleteVideoChannel (user: UserInstance, videoChannel: VideoChannelInstance, res: express.Response) { |
72c7248b | 138 | // Retrieve the user who did the request |
a2431b7d C |
139 | if (videoChannel.isOwned() === false) { |
140 | res.status(403) | |
60862425 | 141 | .json({ error: 'Cannot remove video channel of another server.' }) |
72c7248b | 142 | .end() |
a2431b7d C |
143 | |
144 | return false | |
72c7248b C |
145 | } |
146 | ||
147 | // Check if the user can delete the video channel | |
148 | // The user can delete it if s/he is an admin | |
38fa2065 | 149 | // Or if s/he is the video channel's account |
a2431b7d C |
150 | if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) { |
151 | res.status(403) | |
72c7248b C |
152 | .json({ error: 'Cannot remove video channel of another user' }) |
153 | .end() | |
a2431b7d C |
154 | |
155 | return false | |
72c7248b C |
156 | } |
157 | ||
a2431b7d | 158 | return true |
72c7248b C |
159 | } |
160 | ||
a2431b7d C |
161 | async function checkVideoChannelIsNotTheLastOne (res: express.Response) { |
162 | const count = await db.VideoChannel.countByAccount(res.locals.oauth.token.User.Account.id) | |
163 | ||
164 | if (count <= 1) { | |
165 | res.status(409) | |
166 | .json({ error: 'Cannot remove the last channel of this user' }) | |
167 | .end() | |
168 | ||
169 | return false | |
170 | } | |
171 | ||
172 | return true | |
72c7248b | 173 | } |