diff --git a/server/src/errors/index.js b/server/src/errors/index.js new file mode 100644 index 000000000..eb000c4ee --- /dev/null +++ b/server/src/errors/index.js @@ -0,0 +1,4 @@ +export class AuthentificationError extends Error { + code = 401 + message = 'You have to be authentificated to ' + ( this.message || "perform this action") +} diff --git a/server/src/index.js b/server/src/index.js index 8b63b3f94..a37022464 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -6,7 +6,7 @@ import graphqlHTTP from 'express-graphql' import expressPlayground from 'graphql-playground-middleware-express' import schema from './schema' -const Sentry = require('@sentry/node') +import * as Sentry from '@sentry/node' // Initialise express const app = express() diff --git a/server/src/schema/mutations/AddPrize.js b/server/src/schema/mutations/AddPrize.js index 0e31a3ba1..620442ab6 100644 --- a/server/src/schema/mutations/AddPrize.js +++ b/server/src/schema/mutations/AddPrize.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLInt } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -13,8 +14,12 @@ const args = { } const resolve = (parent, { eventDesignator, rank, amount }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { - Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { if (err || !event) { reject(err) } else { diff --git a/server/src/schema/mutations/AddRule.js b/server/src/schema/mutations/AddRule.js index 606729114..b3183fb6c 100644 --- a/server/src/schema/mutations/AddRule.js +++ b/server/src/schema/mutations/AddRule.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -13,8 +14,12 @@ const args = { } const resolve = (parent, { eventDesignator, title, description }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { - Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { if (err || !event) { reject(err) } else { diff --git a/server/src/schema/mutations/CreateAndSetFormula.js b/server/src/schema/mutations/CreateAndSetFormula.js index 1da7c5e5b..f8e145a22 100644 --- a/server/src/schema/mutations/CreateAndSetFormula.js +++ b/server/src/schema/mutations/CreateAndSetFormula.js @@ -2,6 +2,7 @@ import { GraphQLNonNull, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event, Formula } from '../../models' @@ -12,6 +13,9 @@ const args = { const resolve = (parent, { name, eventDesignator }, context) => { const ownerId = context.userId + if (!ownerId) { + throw new AuthentificationError() + } let newFormula = new Formula({ name, ownerId }) return new Promise((resolve, reject) => { Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { diff --git a/server/src/schema/mutations/CreateAndSetPlotter.js b/server/src/schema/mutations/CreateAndSetPlotter.js index c8e850d05..75d518614 100644 --- a/server/src/schema/mutations/CreateAndSetPlotter.js +++ b/server/src/schema/mutations/CreateAndSetPlotter.js @@ -2,6 +2,7 @@ import { GraphQLNonNull, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event, Plotter } from '../../models' @@ -15,6 +16,9 @@ const args = { const resolve = (parent, { name, host, repo, moduleName, eventDesignator }, context) => { const ownerId = context.userId + if (!ownerId) { + throw new AuthentificationError() + } let newPlotter = new Plotter({ name, host, repo, moduleName, ownerId }) return new Promise((resolve, reject) => { Event.findOne({ designator: eventDesignator, hostId: ownerId }).exec((err, event) => { diff --git a/server/src/schema/mutations/CreateFormula.js b/server/src/schema/mutations/CreateFormula.js index 89fd9b611..a8153209a 100644 --- a/server/src/schema/mutations/CreateFormula.js +++ b/server/src/schema/mutations/CreateFormula.js @@ -2,6 +2,7 @@ import { GraphQLNonNull, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { FormulaType } from '../types' import { Formula } from '../../models' @@ -11,6 +12,9 @@ const args = { const resolve = (parent, { name }, context) => { const ownerId = context.userId + if (!ownerId) { + throw new AuthentificationError() + } let newFormula = new Formula({ name, ownerId }) return new Promise((resolve, reject) => { newFormula.save((err) => { diff --git a/server/src/schema/mutations/CreatePlotter.js b/server/src/schema/mutations/CreatePlotter.js index 30e290b99..669dc81ec 100644 --- a/server/src/schema/mutations/CreatePlotter.js +++ b/server/src/schema/mutations/CreatePlotter.js @@ -2,6 +2,7 @@ import { GraphQLNonNull, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { PlotterType } from '../types' import { Plotter } from '../../models' @@ -14,6 +15,9 @@ const args = { const resolve = (parent, { name, host, repo, moduleName }, context) => { const ownerId = context.userId + if (!ownerId) { + throw new AuthentificationError() + } let newPlotter = new Plotter({ name, host, repo, moduleName, ownerId }) return new Promise((resolve, reject) => { newPlotter.save((err) => { diff --git a/server/src/schema/mutations/EditEvent.js b/server/src/schema/mutations/EditEvent.js index 1e093b8e5..f36fa26f9 100644 --- a/server/src/schema/mutations/EditEvent.js +++ b/server/src/schema/mutations/EditEvent.js @@ -4,6 +4,7 @@ import { GraphQLInt, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -18,8 +19,12 @@ const args = { } const resolve = (parent, { designator, name, description, startDatetime, finishDatetime, formulaId, plotterId }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { - Event.findOne({ designator, hostId: context.userId }).exec((err, event) => { + Event.findOne({ designator, hostId }).exec((err, event) => { if (err || !event) { reject(err) } else { diff --git a/server/src/schema/mutations/EditParticipant.js b/server/src/schema/mutations/EditParticipant.js index 6adac2274..3b8462da5 100644 --- a/server/src/schema/mutations/EditParticipant.js +++ b/server/src/schema/mutations/EditParticipant.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -14,6 +15,10 @@ const args = { } const resolve = (parent, { eventDesignator, teamId, botId, releaseId }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { Event.findOne({ designator: eventDesignator }).exec((err, event) => { if (err) reject(err) diff --git a/server/src/schema/mutations/EditRule.js b/server/src/schema/mutations/EditRule.js index 4ec533f94..1821d8aeb 100644 --- a/server/src/schema/mutations/EditRule.js +++ b/server/src/schema/mutations/EditRule.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -14,8 +15,12 @@ const args = { } const resolve = (parent, { eventDesignator, title, newtitle, description }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { - Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { + Event.findOne({ designator: eventDesignator, hostId }).exec((err, event) => { if (err || !event) { reject(err) } else { diff --git a/server/src/schema/mutations/HostEvent.js b/server/src/schema/mutations/HostEvent.js index c2145736b..a562f6cb4 100644 --- a/server/src/schema/mutations/HostEvent.js +++ b/server/src/schema/mutations/HostEvent.js @@ -3,6 +3,7 @@ import { GraphQLInt, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' import { slugify } from '../../utils/functions' @@ -16,6 +17,9 @@ const args = { const resolve = (parent, { name, description, startDatetime, finishDatetime }, context) => { const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } let newEvent = new Event({ hostId, name, diff --git a/server/src/schema/mutations/RegisterToEvent.js b/server/src/schema/mutations/RegisterToEvent.js index 1c2e5f1a0..c34a29b57 100644 --- a/server/src/schema/mutations/RegisterToEvent.js +++ b/server/src/schema/mutations/RegisterToEvent.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLString } from 'graphql' +import { AuthentificationError } from '../../errors' import { EventType } from '../types' import { Event } from '../../models' @@ -14,6 +15,10 @@ const args = { } const resolve = (parent, { designator, teamId, botId, releaseId }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { Event.findOne({ designator }).exec((err, event) => { if (err) reject(err) diff --git a/server/src/schema/mutations/ReorderRules.js b/server/src/schema/mutations/ReorderRules.js index 7e14e114e..00846507e 100644 --- a/server/src/schema/mutations/ReorderRules.js +++ b/server/src/schema/mutations/ReorderRules.js @@ -3,6 +3,7 @@ import { GraphQLID, GraphQLInt } from 'graphql' +import { AuthentificationError } from '../../errors' import { isBetween } from '../../utils/functions' import { EventType } from '../types' import { Event } from '../../models' @@ -14,6 +15,10 @@ const args = { } const resolve = (parent, { eventDesignator, fromPosition, toPosition }, context) => { + const hostId = context.userId + if (!hostId) { + throw new AuthentificationError() + } return new Promise((resolve, reject) => { Event.findOne({ designator: eventDesignator, hostId: context.userId }).exec((err, event) => { if (err || !event) {