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