]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/permissions.js
basic content editing
[alttp.git] / resources / js / helpers / permissions.js
index 21016f1a08f6786fecfdcbe775717ace91b8279b..1bdb2622b6a44bb9115521ba9d8a14f1c5503561 100644 (file)
@@ -1,14 +1,95 @@
 /// NOTE: These permissions are for UI cosmetics only!
 /// They should be in sync with the backend Policies.
 
+import * as Episode from './Episode';
 import Round from './Round';
 
+export const hasGlobalRole = (user, role) =>
+       user && role && user.global_roles && user.global_roles.includes(role);
+
 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);
+
+// Content
+
+export const mayEditContent = user =>
+       user && hasGlobalRole(user, 'content');
+
+// Episodes
+
+export const isCommentator = (user, episode) => {
+       if (!user || !episode || !episode.crew) return false;
+       return !!episode.crew.find(c => c.role === "commentary" && c.user_id === user.id);
+};
+
+export const isTracker = (user, episode) => {
+       if (!user || !episode || !episode.crew) return false;
+       return !!episode.crew.find(c => c.role === "tracking" && c.user_id === user.id);
+};
+
+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 canApplyForEpisode = (user, episode, as) => {
+       if (!user) return false;
+       if (as === 'commentary') return Episode.acceptsComms(episode) && !isCommentator(user, episode);
+       if (as === 'tracking') return Episode.acceptsTrackers(episode) && !isTracker(user, episode);
+       return false;
+};
+
+export const applicableChannels = (user, episode, as) => {
+       if (!user || !episode) return [];
+       const assigned_channels = (episode.crew || [])
+               .filter(c => c.user_id === user.id)
+               .map(c => c.channel_id);
+       const channels = episode.channels || [];
+       if (as === 'commentary') return channels.filter(c =>
+               c.pivot && c.pivot.accept_comms && !assigned_channels.includes(c.id));
+       if (as === 'tracking') return channels.filter(c =>
+               c.pivot && c.pivot.accept_tracker && !assigned_channels.includes(c.id));
+       return [];
+};
+
+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);
@@ -23,23 +104,61 @@ export const isTournamentAdmin = (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) || (!tournament.locked && isParticipant(user, tournament));
+       !tournament.locked &&
+               (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
 
-export const maySetSeed = (user, tournament) =>
-       isAdmin(user) || isParticipant(user, tournament);
+export const mayApply = (user, tournament) =>
+       user && tournament && tournament.accept_applications &&
+               !isRunner(user, tournament) && !isApplicant(user, tournament);
 
-export const mayViewProtocol = (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);
+
+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) =>
-       isAdmin(user) || hasFinished(user, round) || Round.isComplete(tournament, round);
+       round.locked ||
+       hasFinished(user, round) ||
+       isTournamentMonitor(user, tournament) ||
+       Round.isComplete(tournament, round);
 
 // Users
 
+export const mayEditNickname = (user, subject) =>
+       isSameUser(user, subject);
+
 export const mayEditStreamLink = (user, subject) =>
-       isAdmin(user) || isSameUser(user, subject);
+       isSameUser(user, subject);