]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/permissions.js
allow admins to lock/unlock rounds
[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 isParticipant = (user, tournament) =>
13         user && tournament && tournament.participants &&
14         tournament.participants.find(p => p.user && p.user.id == user.id);
15
16 export const isRunner = (user, tournament) => {
17         const p = isParticipant(user, tournament);
18         return p && p.roles && p.roles.includes('runner');
19 };
20
21 export const isTournamentAdmin = (user, tournament) => {
22         const p = isParticipant(user, tournament);
23         return p && p.roles && p.roles.includes('admin');
24 };
25
26 export const hasFinished = (user, round) =>
27         user && round && round.results &&
28         round.results.find(r => r.user_id == user.id && r.has_finished);
29
30 export const mayAddRounds = (user, tournament) =>
31         isAdmin(user) || (!tournament.locked && isParticipant(user, tournament));
32
33 export const mayLockRound = (user, tournament) =>
34         isAdmin(user) || (!tournament.locked && isTournamentAdmin(user, tournament));
35
36 export const maySetSeed = (user, tournament) =>
37         isAdmin(user) || isParticipant(user, tournament);
38
39 export const mayViewProtocol = (user, tournament) =>
40         isAdmin(user) || isTournamentAdmin(user, tournament);
41
42 export const maySeeResults = (user, tournament, round) =>
43         isAdmin(user) || hasFinished(user, round) || Round.isComplete(tournament, round);
44
45 // Users
46
47 export const mayEditStreamLink = (user, subject) =>
48         isAdmin(user) || isSameUser(user, subject);