X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhelpers%2Fpermissions.js;h=835af24787585f620f935a4c23834166d1e430a9;hb=16bf08bb5cd6118704346f642a491d37c998ef54;hp=8b8c596100270d6c32c4e2b2ff08d2220e96ba08;hpb=eca3f0074e6f9e882b91893e554ce249e25338de;p=alttp.git diff --git a/resources/js/helpers/permissions.js b/resources/js/helpers/permissions.js index 8b8c596..835af24 100644 --- a/resources/js/helpers/permissions.js +++ b/resources/js/helpers/permissions.js @@ -7,8 +7,50 @@ export const isAdmin = user => user && user.role === 'admin'; export const isSameUser = (user, subject) => user && subject && user.id === subject.id; +// Channels + +export const isChannelAdmin = (user, channel) => + user && channel && user.channel_crews && + user.channel_crews.find(c => c.role === 'admin' && c.channel_id === channel.id); + +// Episodes + +export const episodeHasChannel = (episode, channel) => + episode && channel && episode.channels && episode.channels.find(c => c.id === channel.id); + +export const mayRestreamEpisodes = user => + user && user.channel_crews && user.channel_crews.find(c => c.role === 'admin'); + +export const mayEditRestream = (user, episode, channel) => + episodeHasChannel(episode, channel) && isChannelAdmin(user, channel); + +export const canRestreamEpisode = (user, episode) => { + if (!user || !episode || !mayRestreamEpisodes(user)) return false; + const available_channels = user.channel_crews + .filter(c => c.role === 'admin') + .map(c => c.channel_id); + const claimed_channels = ((episode && episode.channels) || []).map(c => c.id); + const remaining_channels = available_channels.filter(id => !claimed_channels.includes(id)); + return remaining_channels.length > 0; +}; + // 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); @@ -39,6 +81,22 @@ export const mayAddRounds = (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 mayReportResult = (user, tournament) => { + if (!user || !tournament) return false; + if (tournament.type === 'open-async') return true; + return isRunner(user, tournament); +}; + +export const mayEditRound = (user, tournament) => + !tournament.locked && isTournamentAdmin(user, tournament); + export const mayLockRound = (user, tournament) => !tournament.locked && isTournamentAdmin(user, tournament); @@ -46,6 +104,9 @@ 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); @@ -53,7 +114,6 @@ 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