]> wagnertech.de Git - mfinanz.git/blob - SL/Dispatcher/AuthHandler/User.pm
restart apache2 in postinst
[mfinanz.git] / SL / Dispatcher / AuthHandler / User.pm
1 package SL::Dispatcher::AuthHandler::User;
2
3 use strict;
4 use parent qw(SL::Dispatcher::AuthHandler::Base);
5
6 use SL::Helper::UserPreferences::DisplayPreferences;
7 use SL::Layout::Dispatcher;
8
9 sub handle {
10   my ($self, %param) = @_;
11
12   my ($http_auth_login,     $http_auth_password) = $self->_parse_http_basic_auth;
13   my ($http_headers_client, $http_headers_login) = $self->_parse_http_headers_auth;
14
15   my $login = $::form->{'{AUTH}login'} // $http_auth_login // $http_headers_login // $::auth->get_session_value('login');
16
17   return $self->_error(%param) if !defined $login;
18
19   my $client_id = $::form->{'{AUTH}client_id'} // $http_headers_client // $::auth->get_session_value('client_id') // $::auth->get_default_client_id;
20
21   return $self->_error(%param) if !$client_id || !$::auth->set_client($client_id);
22
23   %::myconfig = User->get_default_myconfig($::auth->read_user(login => $login));
24
25   return $self->_error(%param) unless $::myconfig{login};
26
27   $::locale = Locale->new($::myconfig{countrycode});
28
29   # user can force a layout version
30   my $user_prefs = SL::Helper::UserPreferences::DisplayPreferences->new();
31   $::request->is_mobile(0) if ($user_prefs->get_layout_style || '') eq 'desktop';
32   $::request->is_mobile(1) if ($user_prefs->get_layout_style || '') eq 'mobile';
33   $::request->{layout} = $::request->is_mobile
34     ? SL::Layout::Dispatcher->new(style => 'mobile')
35     : SL::Layout::Dispatcher->new(style => $::myconfig{menustyle});
36
37   my $ok   =  $::auth->is_api_token_cookie_valid;
38   $ok    ||=                            $http_headers_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, \'dummy!'));
39   $ok    ||=  $::form->{'{AUTH}login'}                      && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $::form->{'{AUTH}password'}));
40   $ok    ||= !$::form->{'{AUTH}login'} &&  $http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $http_auth_password));
41   $ok    ||= !$::form->{'{AUTH}login'} && !$http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, undef));
42
43   return $self->_error(%param) if !$ok;
44
45   $::auth->create_or_refresh_session;
46   $::auth->delete_session_value('FLASH');
47   $::instance_conf->reload->data;
48
49   return 1;
50 }
51
52 sub _error {
53   my ($self, %param) = @_;
54
55   $::auth->punish_wrong_login;
56   $::dispatcher->handle_login_error(%param, error => 'password');
57
58   return 0;
59 }
60
61 1;