]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/permissions.js
respond to whispers
[alttp.git] / resources / js / helpers / permissions.js
1 /// NOTE: These permissions are for UI cosmetics only!
2 /// They should be in sync with the backend Policies.
3
4 import * as Episode from './Episode';
5 import Round from './Round';
6
7 export const hasGlobalRole = (user, role) =>
8         user && role && user.global_roles && user.global_roles.includes(role);
9
10 export const isAdmin = user => user && user.role === 'admin';
11
12 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
13
14 // Channels
15
16 export const isChannelAdmin = (user, channel) =>
17         user && channel && user.channel_crews &&
18                 user.channel_crews.find(c => c.role === 'admin' && c.channel_id === channel.id);
19
20 export const isAnyChannelAdmin = user =>
21         user && user.channel_crews && user.channel_crews.find(c => c.role === 'admin');
22
23 // Content
24
25 export const mayEditContent = user =>
26         user && hasGlobalRole(user, 'content');
27
28 // Episodes
29
30 export const isCommentator = (user, episode) => {
31         if (!user || !episode || !episode.crew) return false;
32         return !!episode.crew.find(c => c.role === "commentary" && c.user_id === user.id);
33 };
34
35 export const isTracker = (user, episode) => {
36         if (!user || !episode || !episode.crew) return false;
37         return !!episode.crew.find(c => c.role === "tracking" && c.user_id === user.id);
38 };
39
40 export const episodeHasChannel = (episode, channel) =>
41         episode && channel && episode.channels && episode.channels.find(c => c.id === channel.id);
42
43 export const mayRestreamEpisodes = user => isAnyChannelAdmin(user);
44
45 export const mayEditRestream = (user, episode, channel) =>
46         episodeHasChannel(episode, channel) && isChannelAdmin(user, channel);
47
48 export const canApplyForEpisode = (user, episode, as) => {
49         if (!user) return false;
50         if (as === 'commentary') return Episode.acceptsComms(episode) && !isCommentator(user, episode);
51         if (as === 'tracking') return Episode.acceptsTrackers(episode) && !isTracker(user, episode);
52         return false;
53 };
54
55 export const applicableChannels = (user, episode, as) => {
56         if (!user || !episode) return [];
57         const assigned_channels = (episode.crew || [])
58                 .filter(c => c.user_id === user.id)
59                 .map(c => c.channel_id);
60         const channels = episode.channels || [];
61         if (as === 'commentary') return channels.filter(c =>
62                 c.pivot && c.pivot.accept_comms && !assigned_channels.includes(c.id));
63         if (as === 'tracking') return channels.filter(c =>
64                 c.pivot && c.pivot.accept_tracker && !assigned_channels.includes(c.id));
65         return [];
66 };
67
68 export const canRestreamEpisode = (user, episode) => {
69         if (!user || !episode || !mayRestreamEpisodes(user)) return false;
70         const available_channels = user.channel_crews
71                 .filter(c => c.role === 'admin')
72                 .map(c => c.channel_id);
73         const claimed_channels = ((episode && episode.channels) || []).map(c => c.id);
74         const remaining_channels = available_channels.filter(id => !claimed_channels.includes(id));
75         return remaining_channels.length > 0;
76 };
77
78 // Tournaments
79
80 export const isApplicant = (user, tournament) => {
81         if (!user || !tournament || !tournament.applications) {
82                 return false;
83         }
84         return tournament.applications.find(p => p.user && p.user.id == user.id);
85 };
86
87 export const isDeniedApplicant = (user, tournament) => {
88         if (!user || !tournament || !tournament.applications) {
89                 return false;
90         }
91         const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
92         return applicant && applicant.denied;
93 };
94
95 export const isParticipant = (user, tournament) =>
96         user && tournament && tournament.participants &&
97         tournament.participants.find(p => p.user && p.user.id == user.id);
98
99 export const isRunner = (user, tournament) => {
100         const p = isParticipant(user, tournament);
101         return p && p.roles && p.roles.includes('runner');
102 };
103
104 export const isTournamentAdmin = (user, tournament) => {
105         const p = isParticipant(user, tournament);
106         return p && p.roles && p.roles.includes('admin');
107 };
108
109 export const isTournamentCrew = (user, tournament) =>
110         isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
111
112 export const isTournamentMonitor = (user, tournament) => {
113         const p = isParticipant(user, tournament);
114         return p && p.roles && p.roles.includes('monitor');
115 };
116
117 export const hasFinished = (user, round) =>
118         user && round && round.results &&
119         round.results.find(r => r.user_id == user.id && r.has_finished);
120
121 export const mayAddRounds = (user, tournament) =>
122         !tournament.locked &&
123                 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
124
125 export const mayApply = (user, tournament) =>
126         user && tournament && tournament.accept_applications &&
127                 !isRunner(user, tournament) && !isApplicant(user, tournament);
128
129 export const mayHandleApplications = (user, tournament) =>
130         tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
131
132 export const mayReportResult = (user, tournament) => {
133         if (!user || !tournament) return false;
134         if (tournament.type === 'open-async') return true;
135         return isRunner(user, tournament);
136 };
137
138 export const mayEditRound = (user, tournament) =>
139         !tournament.locked && isTournamentAdmin(user, tournament);
140
141 export const mayLockRound = (user, tournament) =>
142         !tournament.locked && isTournamentAdmin(user, tournament);
143
144 export const maySetSeed = (user, tournament, round) =>
145         !round.locked &&
146                 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
147
148 export const mayUpdateTournament = (user, tournament) =>
149         isAdmin(user) || isTournamentAdmin(user, tournament);
150
151 export const mayViewProtocol = (user, tournament) =>
152         isTournamentCrew(user, tournament);
153
154 export const maySeeResults = (user, tournament, round) =>
155         round.locked ||
156         hasFinished(user, round) ||
157         isTournamentMonitor(user, tournament) ||
158         Round.isComplete(tournament, round);
159
160 // Twitch
161
162 export const mayManageTwitchBot = user => isAnyChannelAdmin(user);
163
164 // Users
165
166 export const mayEditNickname = (user, subject) =>
167         isSameUser(user, subject);
168
169 export const mayEditStreamLink = (user, subject) =>
170         isSameUser(user, subject);