1 /// NOTE: These permissions are for UI cosmetics only!
2 /// They should be in sync with the backend Policies.
4 import Round from './Round';
6 export const isAdmin = user => user && user.role === 'admin';
8 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
12 export const isChannelAdmin = (user, channel) =>
13 user && channel && user.channel_crews &&
14 user.channel_crews.find(c => c.role === 'admin' && c.channel_id === channel.id);
18 export const episodeHasChannel = (episode, channel) =>
19 episode && channel && episode.channels && episode.channels.find(c => c.id === channel.id);
21 export const mayRestreamEpisodes = user =>
22 user && user.channel_crews && user.channel_crews.find(c => c.role === 'admin');
24 export const mayEditRestream = (user, episode, channel) =>
25 episodeHasChannel(episode, channel) && isChannelAdmin(user, channel);
27 export const canRestreamEpisode = (user, episode) => {
28 if (!user || !episode || !mayRestreamEpisodes(user)) return false;
29 const available_channels = user.channel_crews
30 .filter(c => c.role === 'admin')
31 .map(c => c.channel_id);
32 const claimed_channels = ((episode && episode.channels) || []).map(c => c.id);
33 const remaining_channels = available_channels.filter(id => !claimed_channels.includes(id));
34 return remaining_channels.length > 0;
39 export const isApplicant = (user, tournament) => {
40 if (!user || !tournament || !tournament.applications) {
43 return tournament.applications.find(p => p.user && p.user.id == user.id);
46 export const isDeniedApplicant = (user, tournament) => {
47 if (!user || !tournament || !tournament.applications) {
50 const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
51 return applicant && applicant.denied;
54 export const isParticipant = (user, tournament) =>
55 user && tournament && tournament.participants &&
56 tournament.participants.find(p => p.user && p.user.id == user.id);
58 export const isRunner = (user, tournament) => {
59 const p = isParticipant(user, tournament);
60 return p && p.roles && p.roles.includes('runner');
63 export const isTournamentAdmin = (user, tournament) => {
64 const p = isParticipant(user, tournament);
65 return p && p.roles && p.roles.includes('admin');
68 export const isTournamentCrew = (user, tournament) =>
69 isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
71 export const isTournamentMonitor = (user, tournament) => {
72 const p = isParticipant(user, tournament);
73 return p && p.roles && p.roles.includes('monitor');
76 export const hasFinished = (user, round) =>
77 user && round && round.results &&
78 round.results.find(r => r.user_id == user.id && r.has_finished);
80 export const mayAddRounds = (user, tournament) =>
82 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
84 export const mayApply = (user, tournament) =>
85 user && tournament && tournament.accept_applications &&
86 !isRunner(user, tournament) && !isApplicant(user, tournament);
88 export const mayHandleApplications = (user, tournament) =>
89 tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
91 export const mayReportResult = (user, tournament) => {
92 if (!user || !tournament) return false;
93 if (tournament.type === 'open-async') return true;
94 return isRunner(user, tournament);
97 export const mayEditRound = (user, tournament) =>
98 !tournament.locked && isTournamentAdmin(user, tournament);
100 export const mayLockRound = (user, tournament) =>
101 !tournament.locked && isTournamentAdmin(user, tournament);
103 export const maySetSeed = (user, tournament, round) =>
105 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
107 export const mayUpdateTournament = (user, tournament) =>
108 isAdmin(user) || isTournamentAdmin(user, tournament);
110 export const mayViewProtocol = (user, tournament) =>
111 isTournamentCrew(user, tournament);
113 export const maySeeResults = (user, tournament, round) =>
115 hasFinished(user, round) ||
116 isTournamentMonitor(user, tournament) ||
117 Round.isComplete(tournament, round);
121 export const mayEditNickname = (user, subject) =>
122 isSameUser(user, subject);
124 export const mayEditStreamLink = (user, subject) =>
125 isSameUser(user, subject);