X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhelpers%2Fpermissions.js;h=4ce6935d83054ebb8bcb4e127166c53a0f181698;hb=43da6b2ec78774e7b045a09c68af39717b5f5dbc;hp=ecc1e620e2796fdef58275cab0ed57bed8131fb8;hpb=edd0e97bfdc544114f30bf4c13a929631c44a555;p=alttp.git diff --git a/resources/js/helpers/permissions.js b/resources/js/helpers/permissions.js index ecc1e62..4ce6935 100644 --- a/resources/js/helpers/permissions.js +++ b/resources/js/helpers/permissions.js @@ -1,15 +1,90 @@ /// NOTE: These permissions are for UI cosmetics only! /// They should be in sync with the backend Policies. +import Round from './Round'; + export const isAdmin = user => user && user.role === 'admin'; export const isSameUser = (user, subject) => user && subject && user.id === subject.id; // Tournaments +export const isApplicant = (user, tournament) => { + if (!user || !tournament || !tournament.applications) { + return false; + } + return tournament.applications.find(p => p.user && p.user.id == user.id); +}; + +export const isDeniedApplicant = (user, tournament) => { + if (!user || !tournament || !tournament.applications) { + return false; + } + const applicant = tournament.applications.find(p => p.user && p.user.id == user.id); + return applicant && applicant.denied; +}; + export const isParticipant = (user, tournament) => user && tournament && tournament.participants && tournament.participants.find(p => p.user && p.user.id == user.id); +export const isRunner = (user, tournament) => { + const p = isParticipant(user, tournament); + return p && p.roles && p.roles.includes('runner'); +}; + +export const isTournamentAdmin = (user, tournament) => { + const p = isParticipant(user, tournament); + return p && p.roles && p.roles.includes('admin'); +}; + +export const isTournamentCrew = (user, tournament) => + isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament); + +export const isTournamentMonitor = (user, tournament) => { + const p = isParticipant(user, tournament); + return p && p.roles && p.roles.includes('monitor'); +}; + +export const hasFinished = (user, round) => + user && round && round.results && + round.results.find(r => r.user_id == user.id && r.has_finished); + export const mayAddRounds = (user, tournament) => - isAdmin(user) || isParticipant(user, tournament); + !tournament.locked && + (isRunner(user, tournament) || isTournamentAdmin(user, tournament)); + +export const mayApply = (user, tournament) => + user && tournament && tournament.accept_applications && + !isRunner(user, tournament) && !isApplicant(user, tournament); + +export const mayHandleApplications = (user, tournament) => + tournament && tournament.accept_applications && isTournamentAdmin(user, tournament); + +export const mayLockRound = (user, tournament) => + !tournament.locked && isTournamentAdmin(user, tournament); + +export const maySetSeed = (user, tournament, round) => + !round.locked && + (isRunner(user, tournament) || isTournamentAdmin(user, tournament)); + +export const mayUpdateTournament = (user, tournament) => + isAdmin(user) || isTournamentAdmin(user, tournament); + +export const mayViewProtocol = (user, tournament) => + isTournamentCrew(user, tournament); + +export const maySeeResults = (user, tournament, round) => + round.locked || + hasFinished(user, round) || + isTournamentMonitor(user, tournament) || + (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) || + Round.isComplete(tournament, round); + +// Users + +export const mayEditNickname = (user, subject) => + isSameUser(user, subject); + +export const mayEditStreamLink = (user, subject) => + isSameUser(user, subject);