From: Michael Andreen Date: Sun, 24 Feb 2019 21:54:05 +0000 (+0100) Subject: Add discord_id X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=commitdiff_plain;h=27a84187a27390a65531888c0068a11f69f6d2cf Add discord_id --- diff --git a/database/discord.sql b/database/discord.sql new file mode 100644 index 0000000..f5c950f --- /dev/null +++ b/database/discord.sql @@ -0,0 +1,3 @@ +ALTER TABLE users ADD COLUMN IF NOT EXISTS discord_id text DEFAULT NULL UNIQUE; +ALTER TABLE users ALTER COLUMN pnick DROP NOT NULL; +ALTER TABLE users ALTER COLUMN hostmask DROP NOT NULL; diff --git a/lib/NDWeb/Controller/Settings.pm b/lib/NDWeb/Controller/Settings.pm index f95a979..1635844 100644 --- a/lib/NDWeb/Controller/Settings.pm +++ b/lib/NDWeb/Controller/Settings.pm @@ -43,11 +43,12 @@ sub index :Path :Args(0) { } $c->stash(stylesheets => \@stylesheets); - my ($birthday,$timezone,$email) = $dbh->selectrow_array(q{ -SELECT birthday,timezone,email FROM users WHERE uid = $1 + my ($birthday,$timezone,$email,$discord_id) = $dbh->selectrow_array(q{ +SELECT birthday,timezone,email,discord_id FROM users WHERE uid = $1 },undef,$c->user->id); $c->stash(birthday => $birthday); $c->stash(email => $c->flash->{email} // $email); + $c->stash(discord_id => $c->flash->{discord_id} // $discord_id); my @timezone = split m{/},$timezone,2; $c->stash(timezone => \@timezone); @@ -188,6 +189,39 @@ use the following url to confirm the change: $c->res->redirect($c->uri_for('')); } +sub changeDiscordId : Local { + my ( $self, $c ) = @_; + my $dbh = $c->model; + + my $discord_id = $c->req->param('discord_id'); + + if ($discord_id =~ /^\s*$/) { + my $update = $dbh->prepare(q{ +UPDATE users SET discord_id = NULL WHERE uid = $1; + }); + $update->execute($c->user->id); + $c->flash(error => 'discord id cleared'); + $c->res->redirect($c->uri_for('')); + return, + } + + eval{ + my $update = $dbh->prepare(q{ +UPDATE users SET discord_id = $2 WHERE uid = $1; + }); + $update->execute($c->user->id,$discord_id); + }; + if($@){ + if($@ =~ /duplicate key value violates unique constraint/){ + $c->flash(discord_id => $discord_id); + $c->flash(error => 'Someone else is using this discord id, duplicate account?'); + }else{ + die $@; + } + } + $c->res->redirect($c->uri_for('')); +} + sub confirmEmail : Local { my ( $self, $c, $id ) = @_; my $dbh = $c->model; diff --git a/lib/NDWeb/Controller/Users.pm b/lib/NDWeb/Controller/Users.pm index 4ae8d54..117f1bd 100644 --- a/lib/NDWeb/Controller/Users.pm +++ b/lib/NDWeb/Controller/Users.pm @@ -29,7 +29,7 @@ sub index :Path :Args(0) { my $dbh = $c->model; my $query = $dbh->prepare(q{ -SELECT uid,username,pnick,array_to_string(array_agg(g.groupname),', ') AS groups +SELECT uid,username,discord_id,pnick,array_to_string(array_agg(g.groupname),', ') AS groups FROM users u LEFT OUTER JOIN (groupmembers gm NATURAL JOIN groups g) USING (uid) WHERE uid > 0 GROUP BY u.uid,username,pnick diff --git a/root/src/settings/index.tt2 b/root/src/settings/index.tt2 index 2b50419..8cafbf4 100644 --- a/root/src/settings/index.tt2 +++ b/root/src/settings/index.tt2 @@ -9,6 +9,10 @@
+
Discord ID + +
+
Email
diff --git a/root/src/users/index.tt2 b/root/src/users/index.tt2 index 3c8c086..d7fea84 100644 --- a/root/src/users/index.tt2 +++ b/root/src/users/index.tt2 @@ -1,10 +1,11 @@ [% META title = 'User listing' %] - + [% FOR u IN users %] - + + [% END %]
UserP-nickGroups
UserDiscordP-nickGroups
[% u.username | html %][% u.pnick %][% u.discord_id | html %][% u.pnick | html %] [% u.groups %]