mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:17:08 +00:00
Add authorisation middleware.
Defines three levels of access: * read: anyone who is logged in * write: `user` and `admin` roles * admin: `admin` roles
This commit is contained in:
31
lib/www/server/api/middleware/auth/access.js
Normal file
31
lib/www/server/api/middleware/auth/access.js
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
async function read (req, res, next) {
|
||||
if (req.user) {
|
||||
next();
|
||||
} else {
|
||||
next({status: 403, message: "Access denied"});
|
||||
}
|
||||
}
|
||||
|
||||
async function write (req, res, next) {
|
||||
if (req.user && (req.user.role == "user" || req.user.role == "admin")) {
|
||||
next();
|
||||
} else {
|
||||
next({status: 403, message: "Access denied"});
|
||||
}
|
||||
}
|
||||
|
||||
async function admin (req, res, next) {
|
||||
if (req.user && req.user.role == "admin") {
|
||||
next();
|
||||
} else {
|
||||
next({status: 403, message: "Access denied"});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
read,
|
||||
write,
|
||||
admin
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
exports.jwt = require('./jwt');
|
||||
// exports.access = require('./access');
|
||||
exports.authentify = require('./authentify');
|
||||
exports.access = require('./access');
|
||||
|
||||
Reference in New Issue
Block a user