70 lines
3.2 KiB
JavaScript
70 lines
3.2 KiB
JavaScript
const db = require("../config/db");
|
|
|
|
const authenticate = (req, res, next) => {
|
|
const uuid = req.headers["authorization"];
|
|
if (!uuid) return res.status(401).json({ error: "Unauthorized" });
|
|
|
|
db.get(`
|
|
SELECT sheeps.*, administrators.* FROM administrators JOIN sheeps ON sheeps.id = administrators.sheep_id WHERE administrators.uuid = ?`,
|
|
[uuid],
|
|
(err, administrator) => {
|
|
if (administrator) {
|
|
req.sheepId = administrator.sheep_id;
|
|
req.sheepRole = 'administrator';
|
|
req.group_id = administrator.group_id;
|
|
req.sheepName = administrator.name;
|
|
req.can_view_schedule = administrator.can_view_schedule;
|
|
req.can_view_stand = administrator.can_view_stand;
|
|
req.can_view_territory = administrator.can_view_territory;
|
|
|
|
return next();
|
|
}
|
|
|
|
db.get(`
|
|
SELECT sheeps.*, moderators.* FROM moderators JOIN sheeps ON sheeps.id = moderators.sheep_id WHERE moderators.uuid = ?`,
|
|
[uuid],
|
|
(err, moderator) => {
|
|
if (moderator) {
|
|
req.sheepId = moderator.sheep_id;
|
|
req.sheepRole = 'moderator';
|
|
req.moderator = {
|
|
"id": moderator.moderators_id ? moderator.moderators_id : false,
|
|
"can_add_sheeps": moderator.can_add_sheeps == 1 ? true : false,
|
|
"can_add_territory": moderator.can_add_territory == 1 ? true : false,
|
|
"can_manager_territory": moderator.can_manager_territory == 1 ? true : false,
|
|
"can_add_stand": moderator.can_add_stand == 1 ? true : false,
|
|
"can_manager_stand": moderator.can_manager_stand == 1 ? true : false,
|
|
"can_add_schedule": moderator.can_add_schedule == 1 ? true : false
|
|
}
|
|
req.group_id = moderator.group_id;
|
|
req.sheepName = moderator.name;
|
|
req.can_view_schedule = moderator.can_view_schedule;
|
|
req.can_view_stand = moderator.can_view_stand;
|
|
req.can_view_territory = moderator.can_view_territory;
|
|
|
|
return next();
|
|
}
|
|
|
|
db.get(`SELECT sheeps.* FROM sheeps WHERE sheeps.uuid = ?`, [uuid], (err, sheep) => {
|
|
if (sheep) {
|
|
req.sheepId = sheep.id;
|
|
req.sheepRole = 'sheep';
|
|
req.group_id = sheep.group_id;
|
|
req.sheepName = sheep.name;
|
|
req.can_view_schedule = sheep.can_view_schedule;
|
|
req.can_view_stand = sheep.can_view_stand;
|
|
req.can_view_territory = sheep.can_view_territory;
|
|
|
|
return next();
|
|
}
|
|
|
|
return res.status(401).json({ error: "UUID not found" });
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
};
|
|
|
|
module.exports = authenticate; |