feat: block no admin users to access some routes

This commit is contained in:
canove
2020-09-07 09:38:52 -03:00
parent 8e8658425f
commit 10bc003d02
5 changed files with 48 additions and 20 deletions

View File

@@ -23,7 +23,10 @@ exports.store = async (req, res, next) => {
} }
); );
return res return res.status(200).json({
.status(200) token: token,
.json({ token: token, username: user.name, userId: user.id }); username: user.name,
profile: user.profile,
userId: user.id,
});
}; };

View File

@@ -2,12 +2,24 @@ const Setting = require("../models/Setting");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
exports.index = async (req, res) => { exports.index = async (req, res) => {
if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can access this route." });
}
const settings = await Setting.findAll(); const settings = await Setting.findAll();
return res.status(200).json(settings); return res.status(200).json(settings);
}; };
exports.update = async (req, res) => { exports.update = async (req, res) => {
if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can access this route." });
}
const io = getIO(); const io = getIO();
const { settingKey } = req.params; const { settingKey } = req.params;
const setting = await Setting.findByPk(settingKey); const setting = await Setting.findByPk(settingKey);

View File

@@ -8,6 +8,12 @@ const Setting = require("../models/Setting");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
exports.index = async (req, res) => { exports.index = async (req, res) => {
if (req.user.profile !== "admin") {
return res
.status(403)
.json({ error: "Only administrators can access this route." });
}
const { searchParam = "", pageNumber = 1 } = req.query; const { searchParam = "", pageNumber = 1 } = req.query;
const whereCondition = { const whereCondition = {

View File

@@ -38,6 +38,7 @@ function ListItemLink(props) {
} }
const MainListItems = () => { const MainListItems = () => {
const userProfile = localStorage.getItem("profile");
return ( return (
<div> <div>
<ListItemLink to="/" primary="Dashboard" icon={<DashboardIcon />} /> <ListItemLink to="/" primary="Dashboard" icon={<DashboardIcon />} />
@@ -57,18 +58,22 @@ const MainListItems = () => {
primary={i18n.t("mainDrawer.listItems.contacts")} primary={i18n.t("mainDrawer.listItems.contacts")}
icon={<ContactPhoneIcon />} icon={<ContactPhoneIcon />}
/> />
<Divider /> {userProfile === "admin" && (
<ListSubheader inset>Administration</ListSubheader> <>
<ListItemLink <Divider />
to="/users" <ListSubheader inset>Administration</ListSubheader>
primary={i18n.t("mainDrawer.listItems.users")} <ListItemLink
icon={<GroupIcon />} to="/users"
/> primary={i18n.t("mainDrawer.listItems.users")}
<ListItemLink icon={<GroupIcon />}
to="/settings" />
primary={i18n.t("mainDrawer.listItems.settings")} <ListItemLink
icon={<SettingsIcon />} to="/settings"
/> primary={i18n.t("mainDrawer.listItems.settings")}
icon={<SettingsIcon />}
/>
</>
)}
</div> </div>
); );
}; };

View File

@@ -47,11 +47,12 @@ const useAuth = () => {
const handleLogin = async (e, user) => { const handleLogin = async (e, user) => {
e.preventDefault(); e.preventDefault();
try { try {
const res = await api.post("/auth/login", user); const { data } = await api.post("/auth/login", user);
localStorage.setItem("token", JSON.stringify(res.data.token)); localStorage.setItem("token", JSON.stringify(data.token));
localStorage.setItem("username", res.data.username); localStorage.setItem("username", data.username);
localStorage.setItem("userId", res.data.userId); localStorage.setItem("profile", data.profile);
api.defaults.headers.Authorization = `Bearer ${res.data.token}`; localStorage.setItem("userId", data.userId);
api.defaults.headers.Authorization = `Bearer ${data.token}`;
setIsAuth(true); setIsAuth(true);
toast.success(i18n.t("auth.toasts.success")); toast.success(i18n.t("auth.toasts.success"));
history.push("/tickets"); history.push("/tickets");
@@ -68,6 +69,7 @@ const useAuth = () => {
setIsAuth(false); setIsAuth(false);
localStorage.removeItem("token"); localStorage.removeItem("token");
localStorage.removeItem("username"); localStorage.removeItem("username");
localStorage.removeItem("profile");
localStorage.removeItem("userId"); localStorage.removeItem("userId");
api.defaults.headers.Authorization = undefined; api.defaults.headers.Authorization = undefined;
history.push("/login"); history.push("/login");