Erste Version Frameless
[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 SL::Dispatcher::AuthHandler::User;
8 use SL::User;
9
10 #
11 # actions
12 #
13
14 sub action_user_login {
15   my ($self) = @_;
16
17   # If the user is already logged in then redirect to the proper menu
18   # script.
19   return if $self->_redirect_to_main_script_if_already_logged_in;
20
21   # Otherwise show the login form.
22   $self->render('login_screen/user_login', { no_menu => 1 });
23 }
24
25 sub action_logout {
26   my ($self) = @_;
27
28   $::auth->destroy_session;
29   $::auth->create_or_refresh_session;
30   $self->render('login_screen/user_login', { no_menu => 1 }, error => $::locale->text('You are logged out!'));
31 }
32
33 sub action_login {
34   my ($self) = @_;
35
36   %::myconfig      = $::form->{'{AUTH}login'} ? $::auth->read_user(login => $::form->{'{AUTH}login'}) : ();
37   %::myconfig      = SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
38   $::form->{login} = $::myconfig{login};
39   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
40   my $user         = User->new(login => $::myconfig{login});
41
42   # if we get an error back, bale out
43   my $result = $user->login($::form);
44
45   # Database update available?
46   ::end_of_request() if -2 == $result;
47
48   # Auth DB needs update? If so log the user out forcefully.
49   if (-3 == $result) {
50     $::auth->destroy_session;
51     return $self->render('login_screen/auth_db_needs_update');
52   }
53
54   # Other login errors.
55   if (0 > $result) {
56     $::auth->punish_wrong_login;
57     return $self->render('login_screen/user_login', { no_menu => 1 }, error => $::locale->text('Incorrect username or password!'));
58   }
59
60   # Everything is fine.
61   $::auth->set_cookie_environment_variable();
62
63   $self->_redirect_to_main_script($user);
64 }
65
66 #
67 # settings
68 #
69 sub get_auth_level {
70   return 'none';
71 }
72
73 sub keep_auth_vars_in_form {
74   return 1;
75 }
76
77 #
78 # private methods
79 #
80
81 sub _redirect_to_main_script {
82   my ($self, $user) = @_;
83
84   return $self->redirect_to($::form->{callback}) if $::form->{callback};
85
86   $self->redirect_to(controller => "login.pl", action => 'company_logo');
87 }
88
89 sub _redirect_to_main_script_if_already_logged_in {
90   my ($self) = @_;
91
92   # Get 'login' from valid session.
93   my $login = $::auth->get_session_value('login');
94   return unless $login;
95
96   # See whether or not the user exists in the database.
97   my %user = $::auth->read_user(login => $login);
98   return if ($user{login} || '') ne $login;
99
100   # Check if the session is logged in correctly.
101   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
102
103   $::auth->create_or_refresh_session;
104   $::auth->delete_session_value('FLASH');
105
106   $self->_redirect_to_main_script(\%user);
107
108   return 1;
109 }
110
111 1;