X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/211f4e60ebc94a7fd73564ca750f7f52f416773f..f217d072d76183bc07723dcc29503b732bd2022d:/SL/Auth.pm diff --git a/SL/Auth.pm b/SL/Auth.pm index 74a7d7fe5..5d97aa01f 100644 --- a/SL/Auth.pm +++ b/SL/Auth.pm @@ -5,13 +5,14 @@ use DBI; use Digest::MD5 qw(md5_hex); use IO::File; use Time::HiRes qw(gettimeofday); -use List::MoreUtils qw(uniq); +use List::MoreUtils qw(any uniq); use YAML; use Regexp::IPv6 qw($IPv6_re); use SL::Auth::ColumnInformation; use SL::Auth::Constants qw(:all); use SL::Auth::DB; +use SL::Auth::HTTPHeaders; use SL::Auth::LDAP; use SL::Auth::Password; use SL::Auth::SessionValue; @@ -72,7 +73,7 @@ sub reset { delete $self->{column_information}; } - $self->{authenticator}->reset; + $_->reset for @{ $self->{authenticators} }; $self->client(undef); } @@ -118,7 +119,10 @@ sub mini_error { my ($self, @msg) = @_; if ($ENV{HTTP_USER_AGENT}) { - print Form->create_http_response(content_type => 'text/html'); + # $::form might not be initialized yet at this point â therefore + # we cannot use "create_http_response" yet. + my $cgi = CGI->new(''); + print $cgi->header('-type' => 'text/html', '-charset' => 'UTF-8'); print "
", join ('
', @msg), "";
} else {
print STDERR "Error: @msg\n";
@@ -142,16 +146,34 @@ sub _read_auth_config {
$self->{DB_config} = $::lx_office_conf{'authentication/database'};
}
- if ($self->{module} eq 'DB') {
- $self->{authenticator} = SL::Auth::DB->new($self);
+ $self->{authenticators} = [];
+ $self->{module} ||= 'DB';
+ $self->{module} =~ s{^ +| +$}{}g;
- } elsif ($self->{module} eq 'LDAP') {
- $self->{authenticator} = SL::Auth::LDAP->new($::lx_office_conf{'authentication/ldap'});
- }
+ foreach my $module (split m{ +}, $self->{module}) {
+ my $config_name;
+ ($module, $config_name) = split m{:}, $module, 2;
+ $config_name ||= $module eq 'DB' ? 'database' : $module eq 'HTTPHeaders' ? 'http_headers' : lc($module);
+ my $config = $::lx_office_conf{'authentication/' . $config_name};
- if (!$self->{authenticator}) {
- my $locale = Locale->new('en');
- $self->mini_error($locale->text('No or an unknown authenticantion module specified in "config/kivitendo.conf".'));
+ if (!$config) {
+ my $locale = Locale->new('en');
+ $self->mini_error($locale->text('Missing configuration section "authentication/#1" in "config/kivitendo.conf".', $config_name));
+ }
+
+ if ($module eq 'DB') {
+ push @{ $self->{authenticators} }, SL::Auth::DB->new($self);
+
+ } elsif ($module eq 'LDAP') {
+ push @{ $self->{authenticators} }, SL::Auth::LDAP->new($config);
+
+ } elsif ($module eq 'HTTPHeaders') {
+ push @{ $self->{authenticators} }, SL::Auth::HTTPHeaders->new($config);
+
+ } else {
+ my $locale = Locale->new('en');
+ $self->mini_error($locale->text('Unknown authenticantion module #1 specified in "config/kivitendo.conf".', $module));
+ }
}
my $cfg = $self->{DB_config};
@@ -166,7 +188,7 @@ sub _read_auth_config {
$self->mini_error($locale->text('config/kivitendo.conf: Missing parameters in "authentication/database". Required parameters are "host", "db" and "user".'));
}
- $self->{authenticator}->verify_config();
+ $_->verify_config for @{ $self->{authenticators} };
$self->{session_timeout} *= 1;
$self->{session_timeout} = 8 * 60 if (!$self->{session_timeout});
@@ -210,6 +232,12 @@ sub authenticate_root {
return $result;
}
+sub set_session_authenticated {
+ my ($self, $login, $result) = @_;
+
+ $self->set_session_value(SESSION_KEY_USER_AUTH() => $result, login => $login, client_id => $self->client->{id});
+}
+
sub authenticate {
my ($self, $login, $password) = @_;
@@ -226,8 +254,16 @@ sub authenticate {
return ERR_PASSWORD;
}
- my $result = $login ? $self->{authenticator}->authenticate($login, $password) : ERR_USER;
- $self->set_session_value(SESSION_KEY_USER_AUTH() => $result, login => $login, client_id => $self->client->{id});
+ my $result = ERR_USER;
+ if ($login) {
+ foreach my $authenticator (@{ $self->{authenticators} }) {
+ $result = $authenticator->authenticate($login, $password);
+ last if $result == OK;
+ }
+ }
+
+ $self->set_session_authenticated($login, $result);
+
return $result;
}
@@ -411,15 +447,22 @@ sub save_user {
sub can_change_password {
my $self = shift;
- return $self->{authenticator}->can_change_password();
+ return any { $_->can_change_password } @{ $self->{authenticators} };
}
sub change_password {
my ($self, $login, $new_password) = @_;
- my $result = $self->{authenticator}->change_password($login, $new_password);
+ my $overall_result = OK;
- return $result;
+ foreach my $authenticator (@{ $self->{authenticators} }) {
+ next unless $authenticator->can_change_password;
+
+ my $result = $authenticator->change_password($login, $new_password);
+ $overall_result = $result if $result != OK;
+ }
+
+ return $overall_result;
}
sub read_all_users {
@@ -481,6 +524,9 @@ sub read_user {
my %user_data;
+ # Set defaults for options not present in database
+ $user_data{follow_up_notify_by_email} = 1;
+
while (my $ref = $sth->fetchrow_hashref()) {
$user_data{$ref->{cfg_key}} = $ref->{cfg_value};
@user_data{qw(id login)} = @{$ref}{qw(id login)};
@@ -847,7 +893,7 @@ sub get_session_value {
($self->{SESSION}{$key} //= SL::Auth::SessionValue->new(auth => $self, key => $key))->get
}
-sub create_unique_sesion_value {
+sub create_unique_session_value {
my ($self, $value, %params) = @_;
$self->{SESSION} ||= { };
@@ -880,7 +926,7 @@ sub save_form_in_session {
$data->{$key} = $form->{$key} if !ref($form->{$key}) || $non_scalars;
}
- return $self->create_unique_sesion_value($data, %params);
+ return $self->create_unique_session_value($data, %params);
}
sub restore_form_from_session {
@@ -1094,6 +1140,8 @@ sub evaluate_rights_ary {
my $negate = 0;
foreach my $el (@{$ary}) {
+ next unless defined $el;
+
if (ref $el eq "ARRAY") {
my $val = evaluate_rights_ary($el);
$val = !$val if $negate;
@@ -1195,6 +1243,15 @@ sub check_right {
return $granted;
}
+sub deny_access {
+ my ($self) = @_;
+
+ $::dispatcher->reply_with_json_error(error => 'access') if $::request->type eq 'json';
+
+ delete $::form->{title};
+ $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
+}
+
sub assert {
my ($self, $right, $dont_abort) = @_;
@@ -1203,8 +1260,7 @@ sub assert {
}
if (!$dont_abort) {
- delete $::form->{title};
- $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
+ $self->deny_access;
}
return 0;
@@ -1290,7 +1346,7 @@ The values can be any Perl structure. They are stored as YAML dumps.
Retrieve a value from the session. Returns C