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 isApplicant = (user, tournament) => {
13 if (!user || !tournament || !tournament.applications) {
16 return tournament.applications.find(p => p.user && p.user.id == user.id);
19 export const isDeniedApplicant = (user, tournament) => {
20 if (!user || !tournament || !tournament.applications) {
23 const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
24 return applicant && applicant.denied;
27 export const isParticipant = (user, tournament) =>
28 user && tournament && tournament.participants &&
29 tournament.participants.find(p => p.user && p.user.id == user.id);
31 export const isRunner = (user, tournament) => {
32 const p = isParticipant(user, tournament);
33 return p && p.roles && p.roles.includes('runner');
36 export const isTournamentAdmin = (user, tournament) => {
37 const p = isParticipant(user, tournament);
38 return p && p.roles && p.roles.includes('admin');
41 export const isTournamentCrew = (user, tournament) =>
42 isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
44 export const isTournamentMonitor = (user, tournament) => {
45 const p = isParticipant(user, tournament);
46 return p && p.roles && p.roles.includes('monitor');
49 export const hasFinished = (user, round) =>
50 user && round && round.results &&
51 round.results.find(r => r.user_id == user.id && r.has_finished);
53 export const mayAddRounds = (user, tournament) =>
55 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
57 export const mayApply = (user, tournament) =>
58 user && tournament && tournament.accept_applications &&
59 !isRunner(user, tournament) && !isApplicant(user, tournament);
61 export const mayHandleApplications = (user, tournament) =>
62 tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
64 export const mayLockRound = (user, tournament) =>
65 !tournament.locked && isTournamentAdmin(user, tournament);
67 export const maySetSeed = (user, tournament, round) =>
69 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
71 export const mayViewProtocol = (user, tournament) =>
72 isTournamentCrew(user, tournament);
74 export const maySeeResults = (user, tournament, round) =>
76 hasFinished(user, round) ||
77 isTournamentMonitor(user, tournament) ||
78 (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) ||
79 Round.isComplete(tournament, round);
83 export const mayEditNickname = (user, subject) =>
84 isSameUser(user, subject);
86 export const mayEditStreamLink = (user, subject) =>
87 isSameUser(user, subject);