diff --git a/server/routes/auth.js b/server/routes/auth.js index 04e5fe2..d7ce9cb 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -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) diff --git a/server/routes/profiles.js b/server/routes/profiles.js index 57adcbc..aac40fe 100644 --- a/server/routes/profiles.js +++ b/server/routes/profiles.js @@ -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 diff --git a/server/routes/users.js b/server/routes/users.js index 959c464..c357c50 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -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) diff --git a/src/components/AppBar.vue b/src/components/AppBar.vue index 65e6820..b27ccf5 100644 --- a/src/components/AppBar.vue +++ b/src/components/AppBar.vue @@ -7,7 +7,7 @@ dense > + + + mdi-database-cog + + + Admin + + @@ -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 } } } diff --git a/src/components/Noise.vue b/src/components/Noise.vue index 9961765..1d68c1b 100644 --- a/src/components/Noise.vue +++ b/src/components/Noise.vue @@ -28,13 +28,14 @@ return-object label="Profiles" class="mx-3" + @change="loadProfile" /> - Load Profile + Delete Profile diff --git a/src/components/noise.js b/src/components/noise.js index afc5c8b..3e3afe3 100644 --- a/src/components/noise.js +++ b/src/components/noise.js @@ -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) + }) } } } diff --git a/src/router/index.js b/src/router/index.js index 30a47e7..8fc1929 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -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() }