Frontend: Add channel updating support

This commit is contained in:
mrkubax10 2023-09-18 17:36:21 +02:00
parent 457bc4bee8
commit 060f27cb1d
3 changed files with 59 additions and 16 deletions

View File

@ -85,6 +85,22 @@ sub verifyChannelAccess {
return 1;
}
sub enumerateChannels {
my $aConnection = $_[0];
my $output = "<select name=\"channel\">";
my $query = $aConnection->prepare(qq(select channels.id, channels.name, servers.name from channels inner join servers on channels.server_id=servers.id;));
$query->execute();
while(my @row = $query->fetchrow_array()) {
my $channelID = $row[0];
my $channel = $row[1];
my $server = $row[2];
$output.="<option value=\"$channelID\">$channel at $server</option>";
}
$output.="</select>";
return $output;
}
sub handlePath {
my $aClient = $_[0];
my $aPath = $_[1];
@ -238,16 +254,7 @@ sub handlePath {
$manageChannelAccess.="<option value=\"$id\">$name</option>";
}
$manageChannelAccess.="</select>";
$manageChannelAccess.="<select name=\"channel\">";
$query = $aConnection->prepare(qq(select channels.id, channels.name, servers.name from channels inner join servers on channels.server_id=servers.id;));
$query->execute();
while(@row = $query->fetchrow_array()) {
my $channelID = $row[0];
my $channel = $row[1];
my $server = $row[2];
$manageChannelAccess.="<option value=\"$channelID\">$channel at $server</option>";
}
$manageChannelAccess.="</select><br />";
$manageChannelAccess.=enumerateChannels($aConnection)."<br />";
$manageChannelAccess.="<input name=\"grant\" type=\"submit\" value=\"Grant access\" /> ";
$manageChannelAccess.="<input name=\"revoke\" type=\"submit\" value=\"Revoke access\" />";
$manageChannelAccess.="</form>";
@ -274,6 +281,7 @@ sub handlePath {
}
my $addChannel = "";
my $updateChannel = "";
if($privileges==2) {
$addChannel.="<h3>Add channel</h3>";
$addChannel.="<form action=\"add_channel_action\" method=\"POST\">";
@ -290,9 +298,16 @@ sub handlePath {
$addChannel.="<input name=\"public\" type=\"checkbox\" />Public<br />";
$addChannel.="<input type=\"submit\" value=\"Add\" />";
$addChannel.="</form>";
$updateChannel.="<h3>Update channel</h3>";
$updateChannel.="<form action=\"update_channel_action\" method=\"POST\">";
$updateChannel.=enumerateChannels($aConnection)."<br />";
$updateChannel.="<input name=\"public\" type=\"checkbox\" />Public<br />";
$updateChannel.="<input type=\"submit\" value=\"Update\" />";
$updateChannel.="</form>";
}
frontend::sendTemplate("templates/panel.html", $aClient, {"username"=>$session->{"username"}, "manageChannelAccess"=>$manageChannelAccess, "addUser"=>$addUser, "addServer"=>$addServer, "addChannel"=>$addChannel});
frontend::sendTemplate("templates/panel.html", $aClient, {"username"=>$session->{"username"}, "manageChannelAccess"=>$manageChannelAccess, "addUser"=>$addUser, "addServer"=>$addServer, "addChannel"=>$addChannel, "updateChannel"=>$updateChannel});
return 1;
}
when("/change_password_action") {
@ -498,10 +513,6 @@ sub handlePath {
frontend::sendBadRequest($aClient, "Server ID required");
return 1;
}
if(!defined($parameters{"public"}) || length($parameters{"public"})==0) {
frontend::sendBadRequest($aClient, "Public required");
return 1;
}
my $query = $aConnection->prepare(qq(select name from servers where id=?;));
$query->execute($parameters{"server"});
@ -530,13 +541,34 @@ sub handlePath {
}
$query = $aConnection->prepare(qq(insert into channels values($lastID, ?, ?, ?);));
$query->execute($parameters{"server"}, $parameters{"channel"}, $parameters{"public"});
$query->execute($parameters{"server"}, $parameters{"channel"}, defined($parameters{"public"})?1:0);
my $actionQueue = logger::getActionQueueByServerName($serverName);
push(@$actionQueue, "JOIN", $parameters{"channel"});
frontend::redirect($aClient, "/channel_added.html");
return 1;
}
when("/update_channel_action") {
if(!verifyRequestPrivileges($aRequest, $aClient, 2, $aConnection)) {
return 1;
}
my %parameters = frontend::parsePathParameters($aRequest->{"content"});
if(!defined($parameters{"channel"}) || length($parameters{"channel"})==0) {
frontend::sendBadRequest($aClient, "Channel required");
return 1;
}
my $query = $aConnection->prepare(qq(select id from channels where id=?;));
$query->execute($parameters{"channel"});
my @row = $query->fetchrow_array();
if(scalar(@row)==0) {
frontend::sendBadRequest($aClient, "Channel with ID $parameters{'channel'} doesn't exist");
return 1;
}
$query = $aConnection->prepare(qq(update channels set public=? where id=?;));
$query->execute(defined($parameters{"public"})?1:0, $parameters{"channel"});
frontend::redirect($aClient, "/channel_updated.html");
return 1;
}
when("/view_logs") {
my $channelID = $aRequest->{"path"}{"parameters"}{"channel"};
if(!defined($channelID)) {

View File

@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Channel updated</title>
</head>
<body>
<p>Channel successfully updated</p>
<a href="/panel">Return to user panel</a>
</body>
</html>

View File

@ -16,5 +16,6 @@
{{addUser}}
{{addServer}}
{{addChannel}}
{{updateChannel}}
</body>
</html>