Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Unify gate guardian check
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed Oct 17, 2020
1 parent 649e2c6 commit 91eebd7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 4 additions & 2 deletions api/routes/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {
isGateGuardian,
} from "../../helpers/auth";
import {
eventListFromStatus,
EventStatus,
Expand Down Expand Up @@ -73,9 +76,8 @@ const requireCv =
const requireGateGuardian =
(req, res, next) => {
const { authUser } = req;
const { companies } = authUser;

if (!companies || !companies.find(({ id }) => 428 === Number(id))) {
if (!isGateGuardian(authUser)) {
const status = HttpStatus.Error.Client.NotAcceptable;

res.status(status);
Expand Down
16 changes: 16 additions & 0 deletions helpers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
dotGet,
} from "./data";

export const isGateGuardian =
(user) =>
dotGet(
user,
"companies",
[],
)
.find(
({ id }) =>
428 === Number(id),
)
;
7 changes: 4 additions & 3 deletions middleware/is-gate-guardian.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
HttpStatus,
} from "~/api/helpers/http";
import {
isGateGuardian,
} from "~/helpers/auth";

export default function({ store, error }) {
const { user } = store.state.user;
Expand All @@ -11,9 +14,7 @@ export default function({ store, error }) {
});
}

const { companies } = user;

if (!companies.find(({ id }) => 428 === Number(id))) {
if (!isGateGuardian(user)) {
return error({
statusCode: HttpStatus.Error.Client.NotFound,
});
Expand Down
7 changes: 7 additions & 0 deletions store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
isModerator,
isStudent,
} from "~/api/helpers/permissions";
import {
isGateGuardian,
} from "~/helpers/auth";

export const state = () => (
{
Expand Down Expand Up @@ -49,6 +52,10 @@ export const getters = {
return isStudent(user.role);
},

isGateGuardian({ user }) {
return isGateGuardian(user);
},

isLoggedIn({ user }) {
return Boolean(user);
},
Expand Down

0 comments on commit 91eebd7

Please sign in to comment.