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

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