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 isParticipant = (user, tournament) =>
13 user && tournament && tournament.participants &&
14 tournament.participants.find(p => p.user && p.user.id == user.id);
16 export const isRunner = (user, tournament) => {
17 const p = isParticipant(user, tournament);
18 return p && p.roles && p.roles.includes('runner');
21 export const isTournamentAdmin = (user, tournament) => {
22 const p = isParticipant(user, tournament);
23 return p && p.roles && p.roles.includes('admin');
26 export const isTournamentCrew = (user, tournament) =>
27 isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
29 export const isTournamentMonitor = (user, tournament) => {
30 const p = isParticipant(user, tournament);
31 return p && p.roles && p.roles.includes('monitor');
34 export const hasFinished = (user, round) =>
35 user && round && round.results &&
36 round.results.find(r => r.user_id == user.id && r.has_finished);
38 export const mayAddRounds = (user, tournament) =>
39 isAdmin(user) || (!tournament.locked &&
40 (isRunner(user, tournament) || isTournamentAdmin(user, tournament)));
42 export const mayLockRound = (user, tournament) =>
43 isAdmin(user) || (!tournament.locked && isTournamentAdmin(user, tournament));
45 export const maySetSeed = (user, tournament, round) =>
46 isAdmin(user) || (!round.locked &&
47 (isRunner(user, tournament) || isTournamentAdmin(user, tournament)));
49 export const mayViewProtocol = (user, tournament) =>
50 isAdmin(user) || isTournamentCrew(user, tournament);
52 export const maySeeResults = (user, tournament, round) =>
53 isAdmin(user) || hasFinished(user, round) ||
54 isTournamentMonitor(user, tournament) ||
55 (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) ||
56 Round.isComplete(tournament, round);
60 export const mayEditNickname = (user, subject) =>
61 isAdmin(user) || isSameUser(user, subject);
63 export const mayEditStreamLink = (user, subject) =>
64 isAdmin(user) || isSameUser(user, subject);