package Newslash::Plugin::UserAuth;
use Mojo::Base 'Mojolicious::Plugin';
use Data::Dumper;
+use Mojo::Utils qw(md5_sum);
sub register {
my ($self, $app, $conf) = @_;
$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}) {
$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;