]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/permissions.js
remove absurd admin permissions
[alttp.git] / resources / js / helpers / permissions.js
index 891803110ace4d65b34a7acb8d514a9f6676cd6e..8b8c596100270d6c32c4e2b2ff08d2220e96ba08 100644 (file)
@@ -1,6 +1,8 @@
 /// NOTE: These permissions are for UI cosmetics only!
 /// They should be in sync with the backend Policies.
 
+import Round from './Round';
+
 export const isAdmin = user => user && user.role === 'admin';
 
 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
@@ -11,9 +13,53 @@ export const isParticipant = (user, tournament) =>
        user && tournament && tournament.participants &&
        tournament.participants.find(p => p.user && p.user.id == user.id);
 
+export const isRunner = (user, tournament) => {
+       const p = isParticipant(user, tournament);
+       return p && p.roles && p.roles.includes('runner');
+};
+
+export const isTournamentAdmin = (user, tournament) => {
+       const p = isParticipant(user, tournament);
+       return p && p.roles && p.roles.includes('admin');
+};
+
+export const isTournamentCrew = (user, tournament) =>
+       isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
+
+export const isTournamentMonitor = (user, tournament) => {
+       const p = isParticipant(user, tournament);
+       return p && p.roles && p.roles.includes('monitor');
+};
+
+export const hasFinished = (user, round) =>
+       user && round && round.results &&
+       round.results.find(r => r.user_id == user.id && r.has_finished);
+
 export const mayAddRounds = (user, tournament) =>
-       isAdmin(user) || isParticipant(user, tournament);
+       !tournament.locked &&
+               (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
+
+export const mayLockRound = (user, tournament) =>
+       !tournament.locked && isTournamentAdmin(user, tournament);
+
+export const maySetSeed = (user, tournament, round) =>
+       !round.locked &&
+               (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
+
+export const mayViewProtocol = (user, tournament) =>
+       isTournamentCrew(user, tournament);
+
+export const maySeeResults = (user, tournament, round) =>
+       round.locked ||
+       hasFinished(user, round) ||
+       isTournamentMonitor(user, tournament) ||
+       (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) ||
+       Round.isComplete(tournament, round);
+
+// Users
 
-export const mayViewProtocol = user =>
-       isAdmin(user);
+export const mayEditNickname = (user, subject) =>
+       isSameUser(user, subject);
 
+export const mayEditStreamLink = (user, subject) =>
+       isSameUser(user, subject);