LoginScreen: im Check auf "schon angemeldet?" Client setzen
[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::DB::Employee;
14 use SL::Locale::String qw(t8);
15 use SL::User;
16
17 use Rose::Object::MakeMethods::Generic (
18   'scalar --get_set_init' => [ qw(clients default_client_id) ],
19 );
20
21 __PACKAGE__->run_before('set_layout');
22
23 #
24 # actions
25 #
26
27 sub action_user_login {
28   my ($self) = @_;
29
30   # If the user is already logged in then redirect to the proper menu
31   # script.
32   return if $self->_redirect_to_main_script_if_already_logged_in;
33
34   # Otherwise show the login form.
35   $self->show_login_form(error_state($::form->{error}));
36 }
37
38 sub action_logout {
39   my ($self) = @_;
40
41   $::auth->destroy_session;
42   $::auth->create_or_refresh_session;
43   $self->show_login_form(info => $::locale->text('You are logged out!'));
44 }
45
46 sub action_login {
47   my ($self) = @_;
48
49   my $login     = $::form->{'{AUTH}login'}     || $::auth->get_session_value('login');
50   my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
51   my $error     = t8('Incorrect username or password or no access to selected client!');
52
53   if (!$::auth->set_client($client_id)) {
54     $::auth->punish_wrong_login;
55     return $self->show_login_form(error => $error);
56   }
57
58   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
59   $::form->{login} = $login;
60   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
61   SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
62
63   $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
64
65   # if we get an error back, bale out
66   my $result = User->new(login => $::myconfig{login})->login($::form);
67
68   # Auth DB needs update? If so log the user out forcefully.
69   if (User::LOGIN_AUTH_DBUPDATE_AVAILABLE() == $result) {
70     $::auth->destroy_session;
71     return $self->render('login_screen/auth_db_needs_update');
72   }
73
74   # Basic client tables available? If not tell the user to create them
75   # and log the user out forcefully.
76   if (User::LOGIN_BASIC_TABLES_MISSING() == $result) {
77     $::auth->destroy_session;
78     return $self->render('login_screen/basic_tables_missing');
79   }
80
81   # Database update available?
82   ::end_of_request() if User::LOGIN_DBUPDATE_AVAILABLE() == $result;
83
84   # Other login errors.
85   if (User::LOGIN_OK() != $result) {
86     $::auth->punish_wrong_login;
87     return $self->show_login_form(error => $error);
88   }
89
90   # Everything is fine.
91   $::auth->set_cookie_environment_variable();
92
93   $self->_redirect_to_main_script;
94 }
95
96 #
97 # settings
98 #
99 sub get_auth_level {
100   return 'none';
101 }
102
103 sub keep_auth_vars_in_form {
104   return 1;
105 }
106
107 #
108 # private methods
109 #
110
111 sub _redirect_to_main_script {
112   my ($self) = @_;
113
114   $self->_ensure_employees_for_authorized_users_exist;
115
116   return $self->redirect_to($::form->{callback}) if $::form->{callback};
117
118   $self->redirect_to(controller => "login.pl", action => 'company_logo');
119 }
120
121 sub _redirect_to_main_script_if_already_logged_in {
122   my ($self) = @_;
123
124   # Get 'login' from valid session.
125   my $login = $::auth->get_session_value('login');
126   return unless $login;
127
128   # See whether or not the user exists in the database.
129   my %user = $::auth->read_user(login => $login);
130   return if ($user{login} || '') ne $login;
131
132   # Check if there's a client set in the session -- and whether or not
133   # the user still has access to the client.
134   my $client_id = $::auth->get_session_value('client_id');
135   return if !$client_id;
136
137   if (!$::auth->set_client($client_id)) {
138     $::auth->punish_wrong_login;
139     $::auth->destroy_session;
140     $::auth->create_or_refresh_session;
141     $self->show_login_form(error => t8('Incorrect username or password or no access to selected client!'));
142     return 1;
143   }
144
145   # Check if the session is logged in correctly.
146   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
147
148   $::auth->create_or_refresh_session;
149   $::auth->delete_session_value('FLASH');
150
151   $self->_redirect_to_main_script(\%user);
152
153   return 1;
154 }
155
156 sub _ensure_employees_for_authorized_users_exist {
157   my ($self) = @_;
158
159   my %employees_by_login = map { ($_->login => $_) } @{ SL::DB::Manager::Employee->get_all };
160
161   foreach my $user (@{ SL::DB::AuthClient->new(id => $::auth->client->{id})->load->users || [] }) {
162     my $user_config = $user->config_values;
163     my $employee    = $employees_by_login{$user->login} || SL::DB::Employee->new(login => $user->login);
164
165     $employee->update_attributes(
166       name      => $user_config->{name},
167       workphone => $user_config->{tel},
168       deleted   => 0,
169     );
170   }
171 }
172
173 sub error_state {
174   my %states = (
175     session  => { warning => t8('The session has expired. Please log in again.')                   },
176     password => { error   => t8('Incorrect username or password or no access to selected client!') },
177   );
178
179   return %{ $states{$_[0]} || {} };
180 }
181
182 sub set_layout {
183   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
184 }
185
186 sub init_clients {
187   return SL::DB::Manager::AuthClient->get_all_sorted;
188 }
189
190 sub init_default_client_id {
191   my ($self)         = @_;
192   my $default_client = first { $_->is_default } @{ $self->clients };
193   return $default_client ? $default_client->id : undef;
194 }
195
196 sub show_login_form {
197   my ($self, %params) = @_;
198
199   $self->render('login_screen/user_login', %params, version => $::form->read_version);
200 }
201
202 1;