Userlogin mit Mandanten gefixt (erster Schritt)
[kivitendo-erp.git] / SL / Controller / LoginScreen.pm
1 package SL::Controller::LoginScreen;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first);
8
9 use SL::Dispatcher::AuthHandler::User;
10 use SL::DB::AuthClient;
11 use SL::DB::AuthGroup;
12 use SL::DB::AuthUser;
13 use SL::Locale::String qw(t8);
14 use SL::User;
15
16 use Rose::Object::MakeMethods::Generic (
17   'scalar --get_set_init' => [ qw(clients default_client_id) ],
18 );
19
20 __PACKAGE__->run_before('set_layout');
21
22 #
23 # actions
24 #
25
26 sub action_user_login {
27   my ($self) = @_;
28
29   # If the user is already logged in then redirect to the proper menu
30   # script.
31   return if $self->_redirect_to_main_script_if_already_logged_in;
32
33   # Otherwise show the login form.
34   $self->show_login_form(error => error_state($::form->{error}));
35 }
36
37 sub action_logout {
38   my ($self) = @_;
39
40   $::auth->destroy_session;
41   $::auth->create_or_refresh_session;
42   $self->show_login_form(error => $::locale->text('You are logged out!'));
43 }
44
45 sub action_login {
46   my ($self) = @_;
47
48   my $login     = $::form->{'{AUTH}login'}     || $::auth->get_session_value('login');
49   my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
50   my $error     = t8('Incorrect username or password or no access to selected client!');
51
52   if (!$::auth->set_client($client_id)) {
53     $::auth->punish_wrong_login;
54     return $self->show_login_form(error => $error);
55   }
56
57   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
58   $::form->{login} = $login;
59   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
60   SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
61
62   $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
63
64   # if we get an error back, bale out
65   my $result = User->new(login => $::myconfig{login})->login($::form);
66
67   # Database update available?
68   ::end_of_request() if -2 == $result;
69
70   # Auth DB needs update? If so log the user out forcefully.
71   if (-3 == $result) {
72     $::auth->destroy_session;
73     return $self->render('login_screen/auth_db_needs_update');
74   }
75
76   # Other login errors.
77   if (0 > $result) {
78     $::auth->punish_wrong_login;
79     return $self->show_login_form(error => $error);
80   }
81
82   # Everything is fine.
83   $::auth->set_cookie_environment_variable();
84
85   # TODO: Employees anlegen/checken
86   # $self->_ensure_employees_for_authorized_users_exist;
87
88   $self->_redirect_to_main_script;
89 }
90
91 #
92 # settings
93 #
94 sub get_auth_level {
95   return 'none';
96 }
97
98 sub keep_auth_vars_in_form {
99   return 1;
100 }
101
102 #
103 # private methods
104 #
105
106 sub _redirect_to_main_script {
107   my ($self) = @_;
108
109   return $self->redirect_to($::form->{callback}) if $::form->{callback};
110
111   $self->redirect_to(controller => "login.pl", action => 'company_logo');
112 }
113
114 sub _redirect_to_main_script_if_already_logged_in {
115   my ($self) = @_;
116
117   # Get 'login' from valid session.
118   my $login = $::auth->get_session_value('login');
119   return unless $login;
120
121   # See whether or not the user exists in the database.
122   my %user = $::auth->read_user(login => $login);
123   return if ($user{login} || '') ne $login;
124
125   # Check if the session is logged in correctly.
126   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
127
128   $::auth->create_or_refresh_session;
129   $::auth->delete_session_value('FLASH');
130
131   $self->_redirect_to_main_script(\%user);
132
133   return 1;
134 }
135
136 sub error_state {
137   return {
138     session  => $::locale->text('The session is invalid or has expired.'),
139     password => $::locale->text('Incorrect username or password or no access to selected client!'),
140   }->{$_[0]};
141 }
142
143 sub set_layout {
144   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
145 }
146
147 sub init_clients {
148   return SL::DB::Manager::AuthClient->get_all_sorted;
149 }
150
151 sub init_default_client_id {
152   my ($self)         = @_;
153   my $default_client = first { $_->is_default } @{ $self->clients };
154   return $default_client ? $default_client->id : undef;
155 }
156
157 sub show_login_form {
158   my ($self, %params) = @_;
159
160   $::request->layout->focus('#auth_login');
161   $self->render('login_screen/user_login', %params);
162 }
163
164 1;