diff --git a/sockets/index.js b/sockets/index.js index 8055765..6a74c08 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -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"); }; diff --git a/sockets/set/userSet.js b/sockets/set/userSet.js new file mode 100644 index 0000000..ee3ee59 --- /dev/null +++ b/sockets/set/userSet.js @@ -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(); + } +};