Skip to content

Commit

Permalink
REMOVE_ANY_VIDEO_CHANNEL > MANAGE_ANY_VIDEO_CHANNEL
Browse files Browse the repository at this point in the history
Merge REMOVE_ANY_VIDEO_CHANNEL and MANY_VIDEO_CHANNELS to
MANAGE_ANY_VIDEO_CHANNEL.
  • Loading branch information
kontrollanten committed Dec 9, 2021
1 parent 5e6f8f5 commit 8b40dec
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 39 deletions.
2 changes: 1 addition & 1 deletion client/src/app/+video-channels/video-channels.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class VideoChannelsComponent implements OnInit, OnDestroy {
isManageable () {
if (!this.isUserLoggedIn()) return false

return this.isOwner() || this.authService.getUser().hasRight(UserRight.MANAGE_VIDEO_CHANNELS)
return this.isOwner() || this.authService.getUser().hasRight(UserRight.MANAGE_ANY_VIDEO_CHANNEL)
}

activateCopiedMessage () {
Expand Down
11 changes: 6 additions & 5 deletions server/controllers/api/video-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ videoChannelRouter.post('/:nameWithHost/avatar/pick',
authenticate,
reqAvatarFile,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureUserCanManageChannel,
asyncMiddleware(ensureUserCanManageChannel),
updateAvatarValidator,
asyncMiddleware(updateVideoChannelAvatar)
)
Expand All @@ -84,36 +84,37 @@ videoChannelRouter.post('/:nameWithHost/banner/pick',
authenticate,
reqBannerFile,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureUserCanManageChannel,
asyncMiddleware(ensureUserCanManageChannel),
updateBannerValidator,
asyncMiddleware(updateVideoChannelBanner)
)

videoChannelRouter.delete('/:nameWithHost/avatar',
authenticate,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureUserCanManageChannel,
asyncMiddleware(ensureUserCanManageChannel),
asyncMiddleware(deleteVideoChannelAvatar)
)

videoChannelRouter.delete('/:nameWithHost/banner',
authenticate,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureUserCanManageChannel,
asyncMiddleware(ensureUserCanManageChannel),
asyncMiddleware(deleteVideoChannelBanner)
)

videoChannelRouter.put('/:nameWithHost',
authenticate,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureUserCanManageChannel,
asyncMiddleware(ensureUserCanManageChannel),
videoChannelsUpdateValidator,
asyncRetryTransactionMiddleware(updateVideoChannel)
)

videoChannelRouter.delete('/:nameWithHost',
authenticate,
asyncMiddleware(videoChannelsRemoveValidator),
asyncMiddleware(ensureUserCanManageChannel),
asyncRetryTransactionMiddleware(removeVideoChannel)
)

Expand Down
4 changes: 2 additions & 2 deletions server/middlewares/user-right.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function ensureUserHasRight (userRight: UserRight) {
}
}

function ensureUserCanManageChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
async function ensureUserCanManageChannel (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = res.locals.oauth.token.user
const isUserOwner = res.locals.videoChannel.Account.userId !== user.id

if (isUserOwner && user.hasRight(UserRight.MANAGE_VIDEO_CHANNELS) === false) {
if (isUserOwner && user.hasRight(UserRight.MANAGE_ANY_VIDEO_CHANNEL) === false) {
const message = `User ${user.username} does not have right to manage channel ${req.params.nameWithHost}.`
logger.info(message)

Expand Down
27 changes: 1 addition & 26 deletions server/middlewares/validators/videos/video-channels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express from 'express'
import { body, param, query } from 'express-validator'
import { MChannelAccountDefault, MUser } from '@server/types/models'
import { UserRight } from '../../../../shared'
import { MChannelAccountDefault } from '@server/types/models'
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
import {
Expand Down Expand Up @@ -79,7 +78,6 @@ const videoChannelsRemoveValidator = [
if (areValidationErrors(req, res)) return
if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return

if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return

return next()
Expand Down Expand Up @@ -151,29 +149,6 @@ export {

// ---------------------------------------------------------------------------

function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
if (videoChannel.Actor.isOwned() === false) {
res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot remove video channel of another server.'
})
return false
}

// Check if the user can delete the video channel
// The user can delete it if s/he is an admin
// Or if s/he is the video channel's account
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Cannot remove video channel of another user'
})
return false
}

return true
}

async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)

Expand Down
2 changes: 1 addition & 1 deletion shared/core-utils/users/user-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const userRoleRights: { [ id in UserRole ]: UserRight[] } = {
[UserRole.MODERATOR]: [
UserRight.MANAGE_VIDEO_BLACKLIST,
UserRight.MANAGE_ABUSES,
UserRight.MANAGE_ANY_VIDEO_CHANNEL,
UserRight.REMOVE_ANY_VIDEO,
UserRight.REMOVE_ANY_VIDEO_CHANNEL,
UserRight.REMOVE_ANY_VIDEO_PLAYLIST,
UserRight.REMOVE_ANY_VIDEO_COMMENT,
UserRight.UPDATE_ANY_VIDEO,
Expand Down
6 changes: 2 additions & 4 deletions shared/models/users/user-right.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const enum UserRight {
MANAGE_SERVERS_BLOCKLIST,

MANAGE_VIDEO_BLACKLIST,
MANAGE_ANY_VIDEO_CHANNEL,

REMOVE_ANY_VIDEO,
REMOVE_ANY_VIDEO_CHANNEL,
REMOVE_ANY_VIDEO_PLAYLIST,
REMOVE_ANY_VIDEO_COMMENT,

Expand All @@ -41,7 +41,5 @@ export const enum UserRight {
MANAGE_VIDEOS_REDUNDANCIES,

MANAGE_VIDEO_FILES,
RUN_VIDEO_TRANSCODING,

MANAGE_VIDEO_CHANNELS
RUN_VIDEO_TRANSCODING
}

0 comments on commit 8b40dec

Please sign in to comment.