Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/admin/users/edit.js

38 lines
1.2 KiB
JavaScript

let express = require("express");
let router = express.Router();
let sessionCheck = require("../../../middlewares/sessionCheck");
let models = require("../../../models");
let error = require("../../utils/error");
let userUpdate = require("../../utils/userUpdate");
router.get("/", sessionCheck(3), async (req, res) => {
if (!req.query.name)
return error(req, res, "Can't edit user !", 400, "Missing arg");
let user = await models.User.findByPk(req.query.name);
if (!user)
return error(req, res, "Can't edit user !", 400, "user not found");
res.render("admin/users/edit", {
title: "SOD - Users administration",
targetUser: user,
departments: await models.Department.findAll()
});
}).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.oldUsername)
return error(req, res, "Fail to edit user !", 400, "Missing arg");
let user = await models.User.findByPk(req.body.oldUsername);
user = await userUpdate(req, res, user);
if (user) {
if (req.body.permissions && 0 >= req.body.permissions <= 3) {
user.permissions = req.body.permissions;
await user.save();
}
res.redirect("/admin/users");
}
});
module.exports = router;