2024-10-30 17:59:44 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/multiwikiserver/routes/handlers/update-profile.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: mws-route
|
|
|
|
|
|
|
|
POST /update-user-profile
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.method = "POST";
|
|
|
|
|
|
|
|
exports.path = /^\/update-user-profile\/?$/;
|
|
|
|
|
|
|
|
exports.bodyFormat = "www-form-urlencoded";
|
|
|
|
|
|
|
|
exports.csrfDisable = true;
|
|
|
|
|
|
|
|
exports.handler = function (request,response,state) {
|
|
|
|
if(!state.authenticatedUser) {
|
2024-11-08 10:09:42 +00:00
|
|
|
$tw.mws.store.adminWiki.addTiddler(new $tw.Tiddler({
|
|
|
|
title: "$:/temp/mws/login/error",
|
|
|
|
text: "You must be logged in to update profiles"
|
|
|
|
}));
|
|
|
|
response.writeHead(302, { "Location": "/login" });
|
|
|
|
response.end();
|
2024-10-30 17:59:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-30 18:38:21 +00:00
|
|
|
var userId = state.data.userId;
|
2024-10-30 17:59:44 +00:00
|
|
|
var username = state.data.username;
|
|
|
|
var email = state.data.email;
|
|
|
|
var roleId = state.data.role;
|
2024-10-30 18:38:21 +00:00
|
|
|
var currentUserId = state.authenticatedUser.user_id;
|
2024-11-08 10:09:42 +00:00
|
|
|
|
|
|
|
var hasPermission = ($tw.utils.parseInt(userId) === currentUserId) || state.authenticatedUser.isAdmin;
|
2024-10-30 18:38:21 +00:00
|
|
|
|
|
|
|
if(!hasPermission) {
|
2024-11-08 10:09:42 +00:00
|
|
|
$tw.mws.store.adminWiki.addTiddler(new $tw.Tiddler({
|
|
|
|
title: "$:/temp/mws/update-profile/" + userId + "/error",
|
|
|
|
text: "You don't have permission to update this profile"
|
|
|
|
}));
|
|
|
|
response.writeHead(302, { "Location": "/admin/users/" + userId });
|
|
|
|
response.end();
|
2024-10-30 18:38:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!state.authenticatedUser.isAdmin) {
|
2024-11-08 10:09:42 +00:00
|
|
|
var userRole = state.server.sqlTiddlerDatabase.getUserRoles(userId);
|
2024-10-30 18:38:21 +00:00
|
|
|
roleId = userRole.role_id;
|
|
|
|
}
|
2024-10-30 17:59:44 +00:00
|
|
|
|
|
|
|
var result = state.server.sqlTiddlerDatabase.updateUser(userId, username, email, roleId);
|
|
|
|
|
|
|
|
if(result.success) {
|
2024-11-08 10:09:42 +00:00
|
|
|
$tw.mws.store.adminWiki.addTiddler(new $tw.Tiddler({
|
|
|
|
title: "$:/temp/mws/update-profile/" + userId + "/success",
|
|
|
|
text: result.message
|
|
|
|
}));
|
2024-10-30 17:59:44 +00:00
|
|
|
} else {
|
2024-11-08 10:09:42 +00:00
|
|
|
$tw.mws.store.adminWiki.addTiddler(new $tw.Tiddler({
|
|
|
|
title: "$:/temp/mws/update-profile/" + userId + "/error",
|
|
|
|
text: result.message
|
|
|
|
}));
|
2024-10-30 17:59:44 +00:00
|
|
|
}
|
2024-11-08 10:09:42 +00:00
|
|
|
|
|
|
|
response.writeHead(302, { "Location": "/admin/users/" + userId });
|
2024-10-30 17:59:44 +00:00
|
|
|
response.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|