Added the Sheepstar API to the Archive
This commit is contained in:
25
SheepstarAPIV1/middlewares/checkPermission.js
Normal file
25
SheepstarAPIV1/middlewares/checkPermission.js
Normal file
@ -0,0 +1,25 @@
|
||||
const GrantedPermission = require("../models/GrantedPermission");
|
||||
const Session = require("../models/Session");
|
||||
|
||||
module.exports = function(permissionNode) {
|
||||
return async function(req, res, next) {
|
||||
|
||||
// Set the token
|
||||
let token = req.token;
|
||||
if (req.tokenType === "client") token = req.clientId;
|
||||
|
||||
// Get permissions from client
|
||||
const clientPermissions = await GrantedPermission.findAll({where: {token: token}});
|
||||
|
||||
// Check permission
|
||||
for (let permission in clientPermissions) {
|
||||
if (clientPermissions[permission].permissionNode === "*") return next();
|
||||
try {
|
||||
if (new RegExp(clientPermissions[permission].permissionNode).test(permissionNode)) return next();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Return if client has no permission
|
||||
res.status(403).json({message: `You need the permission '${permissionNode}' to perform this action.`});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user