blib/lib/Lemonldap/NG/Portal/Plugins/LocationDetect.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 180 CC 13 SCAR 28.6
CC
13
Cov
0%
getIpInfo
42.0
getUaInfo
20.0
init
12.0
CRAP 17.5
1 package Lemonldap::NG::Portal::Plugins::LocationDetect;
2  
3 use Mouse;
4 use List::MoreUtils qw/uniq/;
5 use Scalar::Util qw/blessed/;
6 use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_ERROR);
7 extends qw(
8 Lemonldap::NG::Portal::Main::Plugin
9 Lemonldap::NG::Portal::Lib::SMTP
10 );
11  
12 our $VERSION = '2.23.0';
13  
14 has reader => ( is => 'rw' );
15 has locales => ( is => 'rw' );
16 has ipDetail => ( is => 'rw' );
17 has uaDetail => ( is => 'rw' );
18 use constant betweenAuthAndData => 'storeEnvironment';
19  
20 sub init {
21 my ($self) = @_;
22  
23 eval { use HTTP::BrowserDetect; };
24 if ($@) {
25 $self->logger->error("Can't load HTTP::BrowserDetect: $@");
26 return 0;
27 }
28  
29 eval { use MaxMind::DB::Reader; };
30 if ($@) {
31 $self->logger->error("Can't load MaxMind::DB::Reader: $@");
32 return 0;
33 }
34 $self->addSessionDataToRemember( {
35 # This field will be hidden from the user
36 _location_detect_env => '__hidden__',
37 }
38 );
39  
40 my @languages = split( /,\s*/,
41 $self->conf->{locationDetectGeoIpLanguages}
42 || $self->conf->{languages} );
43  
44 $self->reader(
45 MaxMind::DB::Reader->new(
46 file => $self->conf->{locationDetectGeoIpDatabase},
47 )
48 );
49 $self->locales( \@languages );
50 $self->ipDetail( $self->conf->{locationDetectIpDetail} || "city" );
51 $self->uaDetail( $self->conf->{locationDetectUaDetail} || "browser" );
52 return 1;
53 }
54  
55 sub _get_localized {
56 my ( $self, $req, $names ) = @_;
57 return unless $names;
58 my $lang = $self->p->getLanguage($req);
59 if ( $names->{$lang} ) {
60 return $names->{$lang};
61 }
62 else {
63 return $names->{en};
64 }
65 }
66  
67 sub getUaInfo {
68 my ( $self, $req ) = @_;
69  
70 my $ua = HTTP::BrowserDetect->new( $req->user_agent );
71 my $os = $ua->os;
72 my $os_display = $ua->os_string;
73 if ( !$os ) {
74 $os = "unknown";
75 $os_display = "Unknown";
76 }
77 my $browser = $ua->browser;
78 my $browser_display = $ua->browser_string;
79 if ( !$browser ) {
80 $browser = "unknown";
81 $browser_display = "Unknown";
82 }
83  
84 # Select the correct amount of detail to store/display
85 my ( $uaraw, $uadisplay );
86  
87 if ( $self->uaDetail eq "browser" ) {
88 $uaraw = "$browser/$os";
89 $uadisplay = "$browser_display ($os_display)";
90 }
91 else {
92 $uaraw = "$os";
93 $uadisplay = "$os_display";
94 }
95  
96 $self->logger->debug(
97 "[LocationDetect] getUaInfo returns uaraw $uaraw and uadisplay $uadisplay"
98 );
99 return ( $uaraw, $uadisplay );
100 }
101  
102 sub getIpInfo {
103 my ( $self, $req ) = @_;
104  
105 # Try to detect type of GeoIP database
106 my $type = $self->reader->metadata->{database_type};
107 my $hasCity = ( $type =~ /City/ );
108  
109 my $city_display = "Unknown";
110 my $city_code = "unknown";
111 my $country_display = "Unknown";
112 my $country_code = "unknown";
113  
114 my $record = eval { $self->reader->record_for_address( $req->address ) };
115 if ($@) {
116 my $msg = $@;
117 if ( blessed($@) && $@->isa('Throwable::Error') ) {
118 $msg = $@->message;
119 }
120 $self->logger->warn( "[LocationDetect] Could not resolve IP "
121 . $req->address . ": "
122 . $msg );
123 }
124 else {
125 if ( $hasCity && $record->{city} ) {
126 $city_code = $record->{city}{geoname_id} || "unknown";
127 $city_display =
128 $self->_get_localized( $req, $record->{city}{names} )
129 || "Unknown";
130 }
131 if ( $record->{country} ) {
132 $country_code = $record->{country}{iso_code} || "unknown";
133 $country_display =
134 $self->_get_localized( $req, $record->{country}{names} )
135 || "Unknown";
136 }
137 }
138  
139 my ( $ipraw, $ipdisplay );
140 if ( $self->ipDetail eq "city" ) {
141 $ipraw = "$city_code/$country_code";
142 $ipdisplay = "$city_display ($country_display)";
143 }
144 else {
145 $ipraw = "$country_code";
146 $ipdisplay = "$country_display";
147 }
148  
149 $self->logger->debug(
150 "[LocationDetect] getIpInfo returns ipraw $ipraw and ipdisplay $ipdisplay"
151 );
152 return ( $ipraw, $ipdisplay );
153 }
154  
155 sub storeEnvironment {
156 my ( $self, $req ) = @_;
157  
158 my ( $uaraw, $uadisplay ) = $self->getUaInfo($req);
159 my ( $ipraw, $ipdisplay ) = $self->getIpInfo($req);
160  
161 # This is the value we store in history for comparison
162 $req->sessionInfo->{_location_detect_env} = "$uaraw/$ipraw";
163  
164 # This value is just for display
165 $req->sessionInfo->{_location_detect_env_display} =
166 "$uadisplay - $ipdisplay";
167 $req->sessionInfo->{_location_detect_env_ua} = $uadisplay;
168 $req->sessionInfo->{_location_detect_env_ip} = $ipdisplay;
169  
170 return PE_OK;
171 }
172  
173 1;