Frontend: Create default user if no users are present in database

This commit is contained in:
mrkubax10 2023-09-11 10:08:12 +02:00
parent ab8ef2f643
commit 0dc311859a
4 changed files with 18 additions and 8 deletions

View File

@ -1,15 +1,13 @@
create table channels(id int primary key not null,
server_id int not null, -- foreign key in servers table
name text not null,
public int not null,
accessor int -- foreign key in accessors table
public int not null
);
create table users(id int primary key not null,
name text not null,
password text not null,
privileges int not null, -- 0 - normal user, 1 - moderator, 2 - administrator
accessor int -- foreign key in accessors table
privileges int not null -- 0 - normal user, 1 - moderator, 2 - administrator
);
create table servers(id int primary key not null,

View File

@ -17,6 +17,7 @@
package frontend;
use IO::Socket;
use Digest::SHA;
use File::Spec;
use Time::Piece;
use DBI;
@ -450,11 +451,22 @@ sub sendResponse {
sub httpServerWorker {
my $db = DBI->connect("DBI:SQLite:dbname=$configuration::database", "", "", {RaiseError=>1});
my $query = $db->prepare(qq(select id from users;));
$query->execute();
my @row = $query->fetchrow_array();
if(scalar(@row)==0) {
# Create default user
my $password = Digest::SHA::sha256_hex("admin");
$query = $db->prepare(qq(insert into users values(0, "admin", "$password", 2);));
$query->execute();
}
my $server = new IO::Socket::INET(LocalHost=>"localhost", LocalPort=>$configuration::httpServerPort, Proto=>"tcp", Listen=>1, Reuse=>1);
if(!$server) {
print("Failed to open HTTP server on port $configuration::httpServerPort\n");
return;
}
while(1) {
my $client = $server->accept();
my $buffer;

View File

@ -91,8 +91,7 @@ sub handlePath {
}
my $username = $parameters{'username'};
#my $hashedPassword = Digest::SHA::sha256_hex($parameters{"password"});
my $hashedPassword = $parameters{"password"};
my $hashedPassword = Digest::SHA::sha256_hex($parameters{"password"});
my $query = $aConnection->prepare(qq(select name, password from users where name=?;));
$query->execute($username);
my @row = $query->fetchrow_array();
@ -237,7 +236,8 @@ sub handlePath {
$query->execute($session->{"username"});
my @row = $query->fetchrow_array();
my $password = $row[0];
if($parameters{"currentPassword"} ne $password) {
my $hashedPassword = Digest::SHA::sha256_hex($password);
if($hashedPassword ne $password) {
frontend::sendBadRequest($aClient, "Wrong password");
return 1;
}
@ -247,7 +247,7 @@ sub handlePath {
}
$query = $aConnection->prepare(qq(update users set password=? where name=?;));
$query->execute($parameters{"newPassword"}, $session->{"username"});
$query->execute(Digest::SHA::sha256_hex($parameters{"newPassword"}), $session->{"username"});
frontend::redirect($aClient, "/password_changed.html");
return 1;

0
prepare_database.sh Normal file → Executable file
View File