1
0
Fork 0

Add userSet socket

This commit is contained in:
Ethanell 2020-12-14 23:04:32 +01:00
parent f08e249701
commit b930cd11ea
2 changed files with 45 additions and 0 deletions

View file

@ -13,6 +13,8 @@ module.exports = socket => {
socket.on("gradeGet", require("./get/gradeGet")(socket));
if (socket.request.session.user.permissions > 1)
socket.on("evaluationGet", require("./get/evaluationGet")(socket));
if (socket.request.session.user.permissions > 2)
socket.on("userSet", require("./set/userSet")(socket));
}
socket.emit("connected");
};

43
sockets/set/userSet.js Normal file
View file

@ -0,0 +1,43 @@
const models = require("../../models");
module.exports = socket => {
return async (data) => {
if (!data || !data.user) {
socket.emit("userSet", {error: {message: "missing_arguments"}});
return;
}
let u = await models.User.findByPk(data.user);
if (!u) {
socket.emit("userSet", {error: {message: "user_not_found"}});
return;
}
for (let i in data)
switch (i) {
case "firstName":
u.firstName = data.firstName;
break;
case "lastName":
u.lastName = data.lastName;
break;
case "password":
u.passwordHash = data.password;
break;
case "permissions":
u.permissions = data.permissions;
break;
case "groups":
let groups = [];
for (let g of data.groups) {
let gp = await models.Group.findByPk(g);
if (!gp) {
socket.emit("userGet", {error: {message: "group_not_found"}});
return
}
groups.push(gp);
}
u.setGroups(groups);
break;
}
await u.save();
}
};