Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Add hasPermission middleware + fixes #39

Merged
merged 4 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/api/controllers/state/add.state.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const isAdmin = require('../../middlewares/isAdmin')
const isRespo = require('../../middlewares/isRespo')
const errorHandler = require('../../utils/errorHandler')
const isAuth = require('../../middlewares/isAuth')
const { check } = require('express-validator/check')
const validateBody = require('../../middlewares/validateBody')
const log = require('../../utils/log')(module)

/**
* GET /users
Expand All @@ -13,36 +14,42 @@ const validateBody = require('../../middlewares/validateBody')
* ]
*/
module.exports = app => {
app.post('/states', [isAuth(), isAdmin()])
app.post('/states', [
app.post('/states/:id', [isAuth(), isRespo()])

app.post('/states/:id', [
check('title')
.exists()
.matches(/^[A-zÀ-ÿ0-9 '#@!&\-$%]{3,}$/i),
check('desc')
.exists()
.matches(/^[A-zÀ-ÿ0-9 '#@!&\-$%]{3,}$/i),
check('popover')
.exists()
.matches(/^[A-zÀ-ÿ0-9 '#@!&\-$%]{3,}$/i),
check('spotlightId')
.exists()
.matches(/\d/),
validateBody()
])
app.post('/states', async (req, res) => {

app.post('/states/:id', async (req, res) => {
const { State, Spotlight } = req.app.locals.models

try {
const { title, desc, popover, spotlightId } = req.body
const spotlightId = req.params.id
const { title, desc, popover } = req.body

let spotlight = await Spotlight.findById(spotlightId)
if(!spotlight) return res.status(404).json({ error: 'NOT_FOUND' }).end()
if(!spotlight) {
return res
.status(404)
.json({ error: 'NOT_FOUND' })
.end()
}

let state = await State.create({
title,
desc,
popover
popover: popover || ''
})

await spotlight.addState(state)
await state.save()
await spotlight.save()

return res
Expand Down
22 changes: 14 additions & 8 deletions src/api/controllers/state/set.spotlight.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,41 @@ const { check } = require('express-validator/check')
const validateBody = require('../../middlewares/validateBody')

/**
* GET /users
* PUT /states/:id
*
* Response:
* [
*
* ]
*/
module.exports = app => {
app.put('/spotlights/:id/state', [isAuth(), isRespo()])
app.put('/spotlights/:id/state', [
app.put('/states/:id', [isAuth(), isRespo()])
app.put('/states/:id', [
check('value')
.exists()
.matches(/\d/),
validateBody()
])
app.put('/spotlights/:id/state', async (req, res) => {
app.put('/states/:id', async (req, res) => {
const { Spotlight } = req.app.locals.models

try {
const { value } = req.body
const { id } = req.params
let spotlight = await Spotlight.findById(id)
if(!spotlight) return res.status(404).json({ error: 'NOT_FOUND' })
const spotlightId = req.params.id

let spotlight = await Spotlight.findById(spotlightId)
if(!spotlight) {
return res
.status(404)
.json({ error: 'NOT_FOUND' })
.end()
}

spotlight.state = value
await spotlight.save()

return res
.status(200)
.json(spotlight)
.end()
} catch (err) {
errorHandler(err, res)
Expand Down
3 changes: 2 additions & 1 deletion src/api/controllers/user/user.infos.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ module.exports = app => {
if(permission) {
permissionData = {
admin: permission.admin,
respo: permission.respo
respo: permission.respo,
permission: permission.permission
}
}
else {
Expand Down
28 changes: 28 additions & 0 deletions src/api/middlewares/hasPermission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const jwt = require('jsonwebtoken')
const { promisify } = require('util')
const log = require('../utils/log')(module)

jwt.verify = promisify(jwt.verify)

module.exports = route => async (req, res, next) => {
let authorized = false

if(req.user && req.user.permission) {
if(req.user.permission.admin) {
authorized = true
}
else if(req.user.permission.respo && req.user.permission.respo.includes(req.params.id)) {
authorized = true
}
}

if(authorized) {
next()
}
else {
return res
.status(401)
.json({ error: 'UNAUTHORIZED' })
.end()
}
}