$self->client(undef);
+ return undef unless $id_or_name;
+
my $column = $id_or_name =~ m/^\d+$/ ? 'id' : 'name';
my $dbh = $self->dbconnect;
return $self->client;
}
-sub get_user_dbh {
- my ($self, $login, %params) = @_;
- my $may_fail = delete $params{may_fail};
-
- my %user = $self->read_user(login => $login);
- my $dbh = SL::DBConnect->connect(
- $user{dbconnect},
- $user{dbuser},
- $user{dbpasswd},
- {
- pg_enable_utf8 => $::locale->is_utf8,
- AutoCommit => 0
- }
- );
-
- if (!$may_fail && !$dbh) {
- $::form->error($::locale->text('The connection to the authentication database failed:') . "\n" . $DBI::errstr);
- }
-
- if ($user{dboptions} && $dbh) {
- $dbh->do($user{dboptions}) or $::form->dberror($user{dboptions});
- }
-
- return $dbh;
-}
-
sub DESTROY {
my $self = shift;
$main::lxdebug->leave_sub();
}
+sub has_access_to_client {
+ my ($self, $login) = @_;
+
+ return 0 if !$self->client || !$self->client->{id};
+
+ my $sql = <<SQL;
+ SELECT cu.client_id
+ FROM auth.clients_users cu
+ LEFT JOIN auth."user" u ON (cu.user_id = u.id)
+ WHERE (u.login = ?)
+ AND (cu.client_id = ?)
+SQL
+
+ my ($has_access) = $self->dbconnect->selectrow_array($sql, undef, $login, $self->client->{id});
+ return $has_access;
+}
+
sub authenticate_root {
$main::lxdebug->enter_sub();
my ($self, $login, $password) = @_;
+ if (!$self->client || !$self->has_access_to_client($login)) {
+ $::lxdebug->leave_sub;
+ return ERR_PASSWORD;
+ }
+
my $session_auth = $self->get_session_value(SESSION_KEY_USER_AUTH());
if (defined $session_auth && $session_auth == OK) {
$::lxdebug->leave_sub;
}
my $result = $login ? $self->{authenticator}->authenticate($login, $password) : ERR_USER;
- $self->set_session_value(SESSION_KEY_USER_AUTH() => $result, login => $login);
+ $self->set_session_value(SESSION_KEY_USER_AUTH() => $result, login => $login, client_id => $self->client->{id});
$::lxdebug->leave_sub;
return $result;
my $dbh = $self->dbconnect;
my $id = $self->get_user_id($login);
- my $user_db_exists;
$dbh->rollback and return $::lxdebug->leave_sub if (!$id);
- my $u_dbh = $self->get_user_dbh($login, may_fail => 1);
- $user_db_exists = $self->check_tables($u_dbh) if $u_dbh;
-
- $u_dbh->begin_work if $u_dbh && $user_db_exists;
-
$dbh->begin_work;
do_query($::form, $dbh, qq|DELETE FROM auth.user_group WHERE user_id = ?|, $id);
do_query($::form, $dbh, qq|DELETE FROM auth.user_config WHERE user_id = ?|, $id);
do_query($::form, $dbh, qq|DELETE FROM auth.user WHERE id = ?|, $id);
- do_query($::form, $u_dbh, qq|UPDATE employee SET deleted = 't' WHERE login = ?|, $login) if $u_dbh && $user_db_exists;
+
+ # TODO: SL::Auth::delete_user
+ # do_query($::form, $u_dbh, qq|UPDATE employee SET deleted = 't' WHERE login = ?|, $login) if $u_dbh && $user_db_exists;
$dbh->commit;
- $u_dbh->commit if $u_dbh && $user_db_exists;
$::lxdebug->leave_sub;
}
use SL::DB::AuthClient;
use SL::DB::AuthGroup;
use SL::DB::AuthUser;
+use SL::Locale::String qw(t8);
use SL::User;
use Rose::Object::MakeMethods::Generic (
}
%::myconfig = $login ? $::auth->read_user(login => $login) : ();
- SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
- $::form->{login} = $::myconfig{login};
+ $::form->{login} = $login;
$::locale = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
- my $user = User->new(login => $::myconfig{login});
- $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
+ SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
+
+ $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
# if we get an error back, bale out
- my $result = $user->login($::form);
+ my $result = User->new(login => $::myconfig{login})->login($::form);
# Database update available?
::end_of_request() if -2 == $result;
# TODO: Employees anlegen/checken
# $self->_ensure_employees_for_authorized_users_exist;
- $self->_redirect_to_main_script($user);
+ $self->_redirect_to_main_script;
}
#
#
sub _redirect_to_main_script {
- my ($self, $user) = @_;
+ my ($self) = @_;
return $self->redirect_to($::form->{callback}) if $::form->{callback};
sub error_state {
return {
session => $::locale->text('The session is invalid or has expired.'),
- password => $::locale->text('Incorrect password!'),
+ password => $::locale->text('Incorrect username or password or no access to selected client!'),
}->{$_[0]};
}
}
sub login {
- $main::lxdebug->enter_sub();
-
my ($self, $form) = @_;
our $sid;
- local *FH;
-
- my $rc = -3;
+ return -3 if !$self->{login} || !$::auth->client;
- if ($self->{login}) {
- my %myconfig = $main::auth->read_user(login => $self->{login});
+ my %myconfig = $main::auth->read_user(login => $self->{login});
- # check if database is down
- my $dbh = SL::DBConnect->connect($myconfig{dbconnect}, $myconfig{dbuser}, $myconfig{dbpasswd}, SL::DBConnect->get_options)
- or $self->error($DBI::errstr);
-
- # we got a connection, check the version
- my $query = qq|SELECT version FROM defaults|;
- my $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
+ # check if database is down
+ my $dbh = $form->dbconnect_noauto;
- my ($dbversion) = $sth->fetchrow_array;
- $sth->finish;
+ # we got a connection, check the version
+ my $query = qq|SELECT version FROM defaults|;
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
- $self->create_employee_entry($form, $dbh, \%myconfig);
+ my ($dbversion) = $sth->fetchrow_array;
+ $sth->finish;
- $self->create_schema_info_table($form, $dbh);
+ $self->create_employee_entry($form, $dbh, \%myconfig);
- my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg', auth => 1)->parse_dbupdate_controls;
- if ($dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect)) {
- $::lxdebug->leave_sub;
- return -3;
- }
+ $self->create_schema_info_table($form, $dbh);
- $rc = 0;
+ # Auth DB upgrades available?
+ my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg', auth => 1)->parse_dbupdate_controls;
+ return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
- my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $myconfig{dbdriver})->parse_dbupdate_controls;
+ my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $myconfig{dbdriver})->parse_dbupdate_controls;
- map({ $form->{$_} = $myconfig{$_} } qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat));
- dbconnect_vars($form, $form->{dbname});
- my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
- $dbh->disconnect;
+ $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
+ $form->{$_} = $myconfig{$_} for qw(dateformat);
- if ($update_available) {
- $form->{"title"} = $main::locale->text("Dataset upgrade");
- $form->header(no_layout => $form->{no_layout});
- print $form->parse_html_template("dbupgrade/header");
+ dbconnect_vars($form, $form->{dbname});
- $form->{dbupdate} = "db$myconfig{dbname}";
- $form->{ $form->{dbupdate} } = 1;
+ my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
+ $dbh->disconnect;
- if ($form->{"show_dbupdate_warning"}) {
- print $form->parse_html_template("dbupgrade/warning");
- ::end_of_request();
- }
+ return 0 if !$update_available;
+ $form->{"title"} = $main::locale->text("Dataset upgrade");
+ $form->header(no_layout => $form->{no_layout});
+ print $form->parse_html_template("dbupgrade/header");
- # update the tables
- if (!$::lx_office_conf{debug}->{keep_installation_unlocked} && !open(FH, ">", $::lx_office_conf{paths}->{userspath} . "/nologin")) {
- $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
- 'Please verify that the directory "#1" is writeable by the webserver.',
- $::lx_office_conf{paths}->{userspath}),
- 'back_button' => 1);
- }
+ $form->{dbupdate} = "db" . $form->{dbname};
- # required for Oracle
- $form->{dbdefault} = $sid;
+ if ($form->{"show_dbupdate_warning"}) {
+ print $form->parse_html_template("dbupgrade/warning");
+ ::end_of_request();
+ }
- # ignore HUP, QUIT in case the webserver times out
- $SIG{HUP} = 'IGNORE';
- $SIG{QUIT} = 'IGNORE';
+ # update the tables
+ my $fh;
+ if (!$::lx_office_conf{debug}->{keep_installation_unlocked} && !open($fh, ">", $::lx_office_conf{paths}->{userspath} . "/nologin")) {
+ $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
+ 'Please verify that the directory "#1" is writeable by the webserver.',
+ $::lx_office_conf{paths}->{userspath}),
+ 'back_button' => 1);
+ }
- $self->dbupdate($form);
- $self->dbupdate2($form, $dbupdater);
- SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(0);
+ # ignore HUP, QUIT in case the webserver times out
+ $SIG{HUP} = 'IGNORE';
+ $SIG{QUIT} = 'IGNORE';
- close(FH);
+ $self->dbupdate($form);
+ $self->dbupdate2($form, $dbupdater);
+ SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(0);
- # remove lock file
- unlink($::lx_office_conf{paths}->{userspath} . "/nologin");
+ close($fh);
- print $form->parse_html_template("dbupgrade/footer");
+ # remove lock file
+ unlink($::lx_office_conf{paths}->{userspath} . "/nologin");
- $rc = -2;
- }
- }
+ print $form->parse_html_template("dbupgrade/footer");
- $main::lxdebug->leave_sub();
-
- return $rc;
+ return -2;
}
sub dbconnect_vars {
return @conf;
}
-sub error {
- $main::lxdebug->enter_sub();
-
- my ($self, $msg) = @_;
-
- $main::lxdebug->show_backtrace();
-
- if ($ENV{HTTP_USER_AGENT}) {
- print qq|Content-Type: text/html
-
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
-
-<body bgcolor=ffffff>
-
-<h2><font color=red>Error!</font></h2>
-<p><b>$msg</b>|;
-
- }
-
- die "Error: $msg\n";
-
- $main::lxdebug->leave_sub();
-}
-
sub data {
+{ %{ $_[0] } }
}