]> ruin.nu Git - ndwebbie.git/commitdiff
Add discord_id discord
authorMichael Andreen <harv@ruin.nu>
Sun, 24 Feb 2019 21:54:05 +0000 (22:54 +0100)
committerMichael Andreen <harv@ruin.nu>
Thu, 7 Mar 2019 19:06:55 +0000 (20:06 +0100)
database/discord.sql [new file with mode: 0644]
lib/NDWeb/Controller/Settings.pm
lib/NDWeb/Controller/Users.pm
root/src/settings/index.tt2
root/src/users/index.tt2

diff --git a/database/discord.sql b/database/discord.sql
new file mode 100644 (file)
index 0000000..f5c950f
--- /dev/null
@@ -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;
index f95a97922373f9853649ad4f3091fb1b117f97d4..16358444618efe089b44f12be74ee43e7ce8edbf 100644 (file)
@@ -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;
index 4ae8d54450f033fe1dbdd5a6d62d666d93e1339c..117f1bde98a1ca785549c5bf49005db0411db6e2 100644 (file)
@@ -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
index 2b5041918192a5c3accf003b2b347932a52b83bc..8cafbf4890200404918ffe880fd707d3a62eff55 100644 (file)
@@ -9,6 +9,10 @@
        <br>
        <input type="submit" value="Change">
 </fieldset></form>
+<form action="[% c.uri_for('changeDiscordId') %]" method="post"><fieldset> <legend>Discord ID</legend>
+       <input type="text" name="discord_id" value="[% discord_id | html %]" class="discord_id">
+       <br><input type="submit" value="Change">
+</fieldset></form>
 <form action="[% c.uri_for('changeEmail') %]" method="post"><fieldset> <legend>Email</legend>
        <input type="text" name="email" value="[% email | html %]" class="email">
        <br><input type="submit" value="Change">
index 3c8c0866d5ab6e98922d284ef75e2311e8e0d67d..d7fea8403012f363049c4e062b710bfa10ba7f94 100644 (file)
@@ -1,10 +1,11 @@
 [% META title = 'User listing' %]
 <table>
-       <tr><th>User</th><th>P-nick</th><th>Groups</th></tr>
+       <tr><th>User</th><th>Discord</th><th>P-nick</th><th>Groups</th></tr>
 [% FOR u IN users %]
        <tr class="[% loop.count % 2 == 0 ? 'even' : 'odd' %]">
                <td><a href="[% c.uri_for('edit',u.uid) %]">[% u.username | html %]</a></td>
-               <td>[% u.pnick %]</td>
+               <td>[% u.discord_id | html %]</td>
+               <td>[% u.pnick | html %]</td>
                <td>[% u.groups %]</td>
        </tr>
 [% END %]