permissions.tsx
TLDR
This file contains three custom hooks related to permissions: useHasPermissions
, useHasCurrentTeamPermissions
, and useHasCurrentBoardPermissions
. These hooks are used to check if a user has certain permissions on a team or board.
Methods
useHasPermissions
This hook takes in three parameters: teamId
(string), boardId
(string), and permissions
(an array of Permission
constants). It is used to check if the current user has any of the specified permissions on a particular team and board. It returns a boolean value indicating whether the user has any of the permissions.
useHasCurrentTeamPermissions
This hook takes in two parameters: boardId
(string) and permissions
(an array of Permission
constants). It is used to check if the current user has any of the specified permissions on the current team and a particular board. It internally calls useHasPermissions
with the current team id and the provided board id to perform the permission check. It returns a boolean value indicating whether the user has any of the permissions.
useHasCurrentBoardPermissions
This hook takes in one parameter: permissions
(an array of Permission
constants). It is used to check if the current user has any of the specified permissions on the current board. It internally calls useHasCurrentTeamPermissions
with the current board id and the provided permissions to perform the permission check. It returns a boolean value indicating whether the user has any of the permissions.
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useAppSelector} from '../store/hooks'
import {getMyBoardMembership, getCurrentBoardId, getBoard} from '../store/boards'
import {getCurrentTeam} from '../store/teams'
import {Permission} from '../constants'
import {MemberRole} from '../blocks/board'
export const useHasPermissions = (teamId: string, boardId: string, permissions: Permission[]): boolean => {
if (!boardId || !teamId) {
return false
}
const member = useAppSelector(getMyBoardMembership(boardId))
const board = useAppSelector(getBoard(boardId))
if (!board) {
return false
}
if (!member) {
return false
}
const adminPermissions = [Permission.ManageBoardType, Permission.DeleteBoard, Permission.ShareBoard, Permission.ManageBoardRoles, Permission.DeleteOthersComments]
const editorPermissions = [Permission.ManageBoardCards, Permission.ManageBoardProperties]
const commenterPermissions = [Permission.CommentBoardCards]
const viewerPermissions = [Permission.ViewBoard]
for (const permission of permissions) {
if (adminPermissions.includes(permission) && member.schemeAdmin) {
return true
}
if (editorPermissions.includes(permission) && (member.schemeAdmin || member.schemeEditor || board.minimumRole === MemberRole.Editor)) {
return true
}
if (commenterPermissions.includes(permission) && (member.schemeAdmin || member.schemeEditor || member.schemeCommenter || board.minimumRole === MemberRole.Commenter || board.minimumRole === MemberRole.Editor)) {
return true
}
if (viewerPermissions.includes(permission) && (member.schemeAdmin || member.schemeEditor || member.schemeCommenter || member.schemeViewer || board.minimumRole === MemberRole.Viewer || board.minimumRole === MemberRole.Commenter || board.minimumRole === MemberRole.Editor)) {
return true
}
}
return false
}
export const useHasCurrentTeamPermissions = (boardId: string, permissions: Permission[]): boolean => {
const currentTeam = useAppSelector(getCurrentTeam)
return useHasPermissions(currentTeam?.id || '', boardId, permissions)
}
export const useHasCurrentBoardPermissions = (permissions: Permission[]): boolean => {
const currentBoardId = useAppSelector(getCurrentBoardId)
return useHasCurrentTeamPermissions(currentBoardId || '', permissions)
}