From e6085c6e0bbc5f49ea469c57fd94d89144b2f0f9 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sat, 23 Sep 2023 14:42:31 +0200 Subject: [PATCH] Frontend: Add support for updating users --- frontend_routes.pm | 78 +++++++++++++++++++++++++++++++++++++------- templates/panel.html | 1 + 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/frontend_routes.pm b/frontend_routes.pm index 2d822c3..f863dd1 100644 --- a/frontend_routes.pm +++ b/frontend_routes.pm @@ -117,6 +117,25 @@ sub enumerateChannels { return $output; } +sub enumerateUsers { + my $aConnection = $_[0]; + my $aSession = $_[1]; + + my $output = ""; + return $output; +} + sub handlePath { my $aClient = $_[0]; my $aPath = $_[1]; @@ -258,21 +277,11 @@ sub handlePath { my $manageChannelAccess = ""; my $addUser = ""; + my $updateUser = ""; if($privileges>=1) { # moderator $manageChannelAccess.="

Manage channel access

"; $manageChannelAccess.="
"; - $manageChannelAccess.=""; + $manageChannelAccess.=enumerateUsers($aConnection, $session)." "; $manageChannelAccess.=enumerateChannels($aConnection)."
"; $manageChannelAccess.=" "; $manageChannelAccess.=""; @@ -286,6 +295,14 @@ sub handlePath { $addUser.="Operator
"; $addUser.=""; $addUser.="
"; + + $updateUser.="

Update user

"; + $updateUser.="
"; + $updateUser.=enumerateUsers($aConnection, $session)."
"; + $updateUser.="Operator
"; + $updateUser.=" "; + $updateUser.=""; + $updateUser.="
"; } my $addServer = ""; @@ -338,6 +355,7 @@ sub handlePath { "username"=>$session->{"username"}, "manageChannelAccess"=>$manageChannelAccess, "addUser"=>$addUser, + "updateUser"=>$updateUser, "addServer"=>$addServer, "updateServer"=>$updateServer, "addChannel"=>$addChannel, @@ -495,6 +513,42 @@ sub handlePath { frontend::redirect($aClient, "/user_added.html"); return 1; } + when("/update_user_action") { + if(!verifyRequestPrivileges($aRequest, $aClient, 1, $aConnection)) { + return 1; + } + my %parameters = frontend::parsePathParameters($aRequest->{"content"}); + if(!defined($parameters{"user"}) || length($parameters{"user"})==0) { + frontend::sendBadRequest($aClient, "User required"); + return 1; + } + my $query = $aConnection->prepare(qq(select privileges from users where id=?;)); + $query->execute($parameters{"user"}); + my @row = $query->fetchrow_array(); + if(scalar(@row)==0) { + frontend::sendBadRequest($aClient, "User with ID $parameters{'user'} doesn't exist"); + return 1; + } + if($row[0]>1 && !verifyRequestPrivileges($aRequest, $aClient, 2, $aConnection)) { + return 1; + } + if(defined($parameters{"update"})) { + $query = $aConnection->prepare(qq(update users set privileges=? where id=?;)); + $query->execute(defined($parameters{"operator"})?1:0, $parameters{"user"}); + } + elsif(defined($parameters{"delete"})) { + $query = $aConnection->prepare(qq(delete from users where id=?;)); + $query->execute($parameters{"user"}); + $query = $aConnection->prepare(qq(delete from accessors where user_id=?;)); + $query->execute($parameters{"user"}); + } + else { + frontend::sendBadRequest($aClient, "Action (update or delete) required"); + return 1; + } + frontend::redirect($aClient, "/user_updated.html"); + return 1; + } when("/add_server_action") { if(!verifyRequestPrivileges($aRequest, $aClient, 2, $aConnection)) { return 1; diff --git a/templates/panel.html b/templates/panel.html index ea18388..a620f04 100644 --- a/templates/panel.html +++ b/templates/panel.html @@ -14,6 +14,7 @@ {{manageChannelAccess}} {{addUser}} + {{updateUser}} {{addServer}} {{updateServer}} {{addChannel}}