OSDN Git Service

Plugin::UserAuth: add ipid and subnetid to user object
authorhylom <hylom@users.sourceforge.jp>
Thu, 6 Oct 2016 17:34:39 +0000 (02:34 +0900)
committerhylom <hylom@users.sourceforge.jp>
Thu, 6 Oct 2016 17:34:39 +0000 (02:34 +0900)
src/newslash_web/lib/Newslash/Plugin/UserAuth.pm

index 306baba..dfefbca 100644 (file)
@@ -1,6 +1,7 @@
 package Newslash::Plugin::UserAuth;
 use Mojo::Base 'Mojolicious::Plugin';
 use Data::Dumper;
+use Mojo::Utils qw(md5_sum);
 
 sub register {
     my ($self, $app, $conf) = @_;
@@ -8,6 +9,7 @@ sub register {
     $app->hook(before_dispatch => sub {
                    my $c = shift;
                    my $session = $c->session('session');
+                   my $host_ip = $c->tx->remote_address;
 
                    my $user = {};
                    if ($session->{token}) {
@@ -21,8 +23,23 @@ sub register {
                            $c->kvs->set('anonymous_user', $user);
                        }
                    }
+                   $user->{ipid} = ip_id($host_ip);
+                   $user->{subnetid} = subnet_id($host_ip);
                    $c->stash(user => $user);
                });
 }
 
+# from slash's get_ipids method (Slash::Utility::Environment)
+sub ip_id {
+    my $host_ip = shift;
+    return $host_ip ? md5_sum($host_ip) : '';
+}
+
+sub subnet_id {
+    my $subnet_id = shift;
+    $subnet_id =~ s/(\d+\.\d+\.\d+)\.\d+/$1\.0/;
+    return $subnet_id ? md5_sum($subnet_id): '';
+}
+
+
 1;