]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/permissions.js
round titles
[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 Round from './Round';
5
6 export const isAdmin = user => user && user.role === 'admin';
7
8 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
9
10 // Tournaments
11
12 export const isApplicant = (user, tournament) => {
13         if (!user || !tournament || !tournament.applications) {
14                 return false;
15         }
16         return tournament.applications.find(p => p.user && p.user.id == user.id);
17 };
18
19 export const isDeniedApplicant = (user, tournament) => {
20         if (!user || !tournament || !tournament.applications) {
21                 return false;
22         }
23         const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
24         return applicant && applicant.denied;
25 };
26
27 export const isParticipant = (user, tournament) =>
28         user && tournament && tournament.participants &&
29         tournament.participants.find(p => p.user && p.user.id == user.id);
30
31 export const isRunner = (user, tournament) => {
32         const p = isParticipant(user, tournament);
33         return p && p.roles && p.roles.includes('runner');
34 };
35
36 export const isTournamentAdmin = (user, tournament) => {
37         const p = isParticipant(user, tournament);
38         return p && p.roles && p.roles.includes('admin');
39 };
40
41 export const isTournamentCrew = (user, tournament) =>
42         isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
43
44 export const isTournamentMonitor = (user, tournament) => {
45         const p = isParticipant(user, tournament);
46         return p && p.roles && p.roles.includes('monitor');
47 };
48
49 export const hasFinished = (user, round) =>
50         user && round && round.results &&
51         round.results.find(r => r.user_id == user.id && r.has_finished);
52
53 export const mayAddRounds = (user, tournament) =>
54         !tournament.locked &&
55                 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
56
57 export const mayApply = (user, tournament) =>
58         user && tournament && tournament.accept_applications &&
59                 !isRunner(user, tournament) && !isApplicant(user, tournament);
60
61 export const mayHandleApplications = (user, tournament) =>
62         tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
63
64 export const mayReportResult = (user, tournament) => {
65         if (!user || !tournament) return false;
66         if (tournament.type === 'open-async') return true;
67         return isRunner(user, tournament);
68 };
69
70 export const mayEditRound = (user, tournament) =>
71         !tournament.locked && isTournamentAdmin(user, tournament);
72
73 export const mayLockRound = (user, tournament) =>
74         !tournament.locked && isTournamentAdmin(user, tournament);
75
76 export const maySetSeed = (user, tournament, round) =>
77         !round.locked &&
78                 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
79
80 export const mayUpdateTournament = (user, tournament) =>
81         isAdmin(user) || isTournamentAdmin(user, tournament);
82
83 export const mayViewProtocol = (user, tournament) =>
84         isTournamentCrew(user, tournament);
85
86 export const maySeeResults = (user, tournament, round) =>
87         round.locked ||
88         hasFinished(user, round) ||
89         isTournamentMonitor(user, tournament) ||
90         Round.isComplete(tournament, round);
91
92 // Users
93
94 export const mayEditNickname = (user, subject) =>
95         isSameUser(user, subject);
96
97 export const mayEditStreamLink = (user, subject) =>
98         isSameUser(user, subject);