+ $::auth->create_or_refresh_session;
+
+ die "cannot find user $login" unless %::myconfig = $::auth->read_user(login => $login);
+ die "cannot find locale for user $login" unless $::locale = Locale->new($::myconfig{countrycode} || $::lx_office_conf{system}->{language});
+
+ $::form->{__ERROR_HANDLER} = sub { die @_ };
+}
+
+sub cleanup_kivitendo {
+ eval { SL::DB::Auth->new->db->dbh->rollback; };
+ eval { SL::DB::BackgroundJob->new->db->dbh->rollback; };
+
+ $::auth->save_session;
+ $::auth->expire_sessions;
+ $::auth->reset;
+
+ $::form = undef;
+ $::myconfig = ();
+ $::request = undef;
+ Form::disconnect_standard_dbh;
+}
+
+sub drop_privileges {
+ my $user = $lx_office_conf{task_server}->{run_as};
+ return unless $user;
+
+ my ($uid, $gid);
+ while (my @details = getpwent()) {
+ next unless $details[0] eq $user;
+ ($uid, $gid) = @details[2, 3];
+ last;
+ }
+ endpwent();
+
+ if (!$uid) {
+ print "Error: Cannot drop privileges to ${user}: user does not exist\n";
+ exit 1;
+ }
+
+ if (!setgid($gid)) {
+ print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
+ exit 1;
+ }
+
+ if (!setuid($uid)) {
+ print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
+ exit 1;
+ }
+}
+
+sub notify_on_failure {
+ my (%params) = @_;
+
+ my $cfg = $lx_office_conf{'task_server/notify_on_failure'} || {};
+
+ return if any { !$cfg->{$_} } qw(send_email_to email_from email_subject email_template);