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