Add profile deletion, hide admin page to non-admins

This commit is contained in:
Kevin Thomas
2021-08-07 23:46:20 -07:00
parent cb1904ec2f
commit cd6af207be
7 changed files with 137 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
const express = require('express')
const passport = require('passport')
const db = require('../db')
const router = express.Router()
router.post('/login/password', passport.authenticate('local'), function (req, res, next) {
@@ -14,6 +15,24 @@ router.get('/auth', function (req, res) {
}
})
router.get('/admin', function (req, res) {
if (!req.user) {
return res.sendStatus(401)
}
db.get('SELECT is_admin FROM users WHERE id = ?', [req.user.id], (err, row) => {
if (err) {
return res.sendStatus(500)
}
if (row.is_admin === 0) {
res.sendStatus(401)
} else {
res.sendStatus(200)
}
})
})
router.get('/logout', function (req, res) {
req.logout()
res.sendStatus(200)

View File

@@ -27,18 +27,18 @@ router.post('/profiles', function (req, res) {
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
req.body.name,
req.user.id,
req.body.isTimerEnabled,
req.body.isTimerEnabled ? 1 : 0,
req.body.duration,
req.body.volume,
req.body.noiseColor,
req.body.isFilterEnabled,
req.body.isFilterEnabled ? 1 : 0,
req.body.filterType,
req.body.filterCutoff,
req.body.isLFOFilterCutoffEnabled,
req.body.isLFOFilterCutoffEnabled ? 1 : 0,
req.body.lfoFilterCutoffFrequency,
req.body.lfoFilterCutoffLow,
req.body.lfoFilterCutoffHigh,
req.body.isTremoloEnabled,
req.body.isTremoloEnabled ? 1 : 0,
req.body.tremoloFrequency,
req.body.tremoloDepth
],
@@ -105,19 +105,19 @@ router.get('/profiles/:profileId', function (req, res) {
return res.sendStatus(500)
}
// TODO: Should return 'true' or 'false' rather than 1 or 0 for bool values
profile.name = row.name
profile.isTimerEnabled = row.isTimerEnabled
profile.isTimerEnabled = row.isTimerEnabled === 1
profile.duration = row.duration
profile.volume = row.volume
profile.noiseColor = row.noiseColor
profile.isFilterEnabled = row.isFilterEnabled
profile.isFilterEnabled = row.isFilterEnabled === 1
profile.filterType = row.filterType
profile.isLFOFilterCutoffEnabled = row.isLFOFilterCutoffEnabled
profile.filterCutoff = row.filterCutoff
profile.isLFOFilterCutoffEnabled = row.isLFOFilterCutoffEnabled === 1
profile.lfoFilterCutoffFrequency = row.lfoFilterCutoffFrequency
profile.lfoFilterCutoffLow = row.lfoFilterCutoffLow
profile.lfoFilterCutoffHigh = row.lfoFilterCutoffHigh
profile.isTremoloEnabled = row.isTremoloEnabled
profile.isTremoloEnabled = row.isTremoloEnabled === 1
profile.tremoloFrequency = row.tremoloFrequency
profile.tremoloDepth = row.tremoloDepth
@@ -125,4 +125,28 @@ router.get('/profiles/:profileId', function (req, res) {
})
})
router.delete('/profiles/:profileId', function (req, res) {
if (!req.user) {
return res.sendStatus(401)
}
db.get('SELECT user FROM profiles WHERE id = ?', [req.params.profileId], (err, row) => {
if (err) {
return res.sendStatus(500)
}
if (row.user.toString() !== req.user.id) {
return res.sendStatus(401)
}
db.run('DELETE FROM profiles WHERE id = ?', [req.params.profileId], (err) => {
if (err) {
return res.sendStatus(500)
}
})
})
res.sendStatus(200)
})
module.exports = router

View File

@@ -3,6 +3,27 @@ const crypto = require('crypto')
const db = require('../db')
const router = express.Router()
router.get('/users/current', function (req, res) {
if (!req.user) {
return res.sendStatus(401)
}
db.get('SELECT is_admin as isAdmin, * FROM users WHERE id = ?', [req.user.id], (err, row) => {
if (err) {
return res.sendStatus(500)
}
const user = {}
user.id = row.id
user.username = row.username
user.name = row.name
user.isAdmin = row.isAdmin === 1
res.json({ user: user })
})
})
router.get('/users', function (req, res) {
if (!req.user) {
return res.sendStatus(401)

View File

@@ -7,7 +7,7 @@
dense
>
<v-app-bar-nav-icon
@click="drawyer = true"
@click="openDrawyer"
/>
</v-app-bar>
<v-navigation-drawer
@@ -31,6 +31,17 @@
Home
</v-list-item-title>
</v-list-item>
<v-list-item
v-if="isAdmin"
@click="admin"
>
<v-list-item-icon>
<v-icon>mdi-database-cog</v-icon>
</v-list-item-icon>
<v-list-item-title>
Admin
</v-list-item-title>
</v-list-item>
<v-list-item
@click="logout"
>
@@ -59,7 +70,8 @@ export default {
data: () => ({
drawyer: false,
group: null
group: null,
isAdmin: false
}),
methods: {
home () {
@@ -78,6 +90,21 @@ export default {
.catch(function (error) {
console.error(error.response)
})
},
openDrawyer () {
this.$http.get('https://localhost:3000/users/current')
.then(response => {
if (response.data.user.isAdmin) {
this.isAdmin = true
} else {
this.isAdmin = false
}
})
.catch(function (error) {
console.error(error.response)
this.isAdmin = false
})
this.drawyer = true
}
}
}

View File

@@ -28,13 +28,14 @@
return-object
label="Profiles"
class="mx-3"
@change="loadProfile"
/>
<v-btn
class="mx-3 mb-5"
@click="loadProfile"
@click="deleteProfile"
>
Load Profile
Delete Profile
</v-btn>
</v-row>

View File

@@ -146,18 +146,18 @@ export default {
saveProfile () {
this.$http.post('https://localhost:3000/profiles', {
name: this.profileName,
isTimerEnabled: this.isTimerEnabled ? 1 : 0,
isTimerEnabled: this.isTimerEnabled,
duration: this.duration,
volume: this.volume,
noiseColor: this.noiseColor,
isFilterEnabled: this.isFilterEnabled ? 1 : 0,
isFilterEnabled: this.isFilterEnabled,
filterType: this.filterType,
filterCutoff: this.filterCutoff,
isLFOFilterCutoffEnabled: this.isLFOFilterCutoffEnabled ? 1 : 0,
isLFOFilterCutoffEnabled: this.isLFOFilterCutoffEnabled,
lfoFilterCutoffFrequency: this.lfoFilterCutoffFrequency,
lfoFilterCutoffLow: this.lfoFilterCutoffRange[0],
lfoFilterCutoffHigh: this.lfoFilterCutoffRange[1],
isTremoloEnabled: this.isTremoloEnabled ? 1 : 0,
isTremoloEnabled: this.isTremoloEnabled,
tremoloFrequency: this.tremoloFrequency,
tremoloDepth: this.tremoloDepth
})
@@ -174,18 +174,18 @@ export default {
if (response.status === 200) {
const profile = response.data.profile
this.isTimerEnabled = profile.isTimerEnabled === 1
this.isTimerEnabled = profile.isTimerEnabled
this.duration = profile.duration
this.volume = profile.volume
this.noiseColor = profile.noiseColor
this.isFilterEnabled = profile.isFilterEnabled === 1
this.isFilterEnabled = profile.isFilterEnabled
this.filterType = profile.filterType
this.filterCutoff = profile.filterCutoff
this.isLFOFilterCutoffEnabled = profile.isLFOFilterCutoffEnabled === 1
this.isLFOFilterCutoffEnabled = profile.isLFOFilterCutoffEnabled
this.lfoFilterCutoffFrequency = profile.lfoFilterCutoffFrequency
this.lfoFilterCutoffRange[0] = profile.lfoFilterCutoffLow
this.lfoFilterCutoffRange[1] = profile.lfoFilterCutoffHigh
this.isTremoloEnabled = profile.isTremoloEnabled === 1
this.isTremoloEnabled = profile.isTremoloEnabled
this.tremoloFrequency = profile.tremoloFrequency
this.tremoloDepth = profile.tremoloDepth
}
@@ -193,6 +193,17 @@ export default {
.catch(function (error) {
console.error(error.response)
})
},
deleteProfile () {
this.$http.delete('https://localhost:3000/profiles/'.concat(this.selectedProfile.id))
.then(response => {
if (response.status === 200) {
this.populateProfileItems()
}
})
.catch(function (error) {
console.error(error.response)
})
}
}
}

View File

@@ -59,6 +59,19 @@ router.beforeEach((to, from, next) => {
console.error(error.response)
next('/login')
})
} else if (to.name === 'Admin') {
instance.get('/admin')
.then(response => {
if (response.status === 200) {
next()
} else {
next('/')
}
})
.catch(function (error) {
console.error(error.response)
next('/')
})
} else {
next()
}