commit 796d0b76d08c53f715b80ceaa4292135fb910063
Author: Bernd Bleßmann <bernd@kivitendo-premium.de>
Date:   Tue Jan 3 16:07:24 2023 +0100

    Gültigkeitstoken Reklamationscontroller

diff --git a/SL/Controller/Reclamation.pm b/SL/Controller/Reclamation.pm
index 0d7ca9fa0..8df7cf88d 100644
--- a/SL/Controller/Reclamation.pm
+++ b/SL/Controller/Reclamation.pm
@@ -25,6 +25,7 @@ use SL::DB::Language;
 use SL::DB::RecordLink;
 use SL::DB::Shipto;
 use SL::DB::Translation;
+use SL::DB::ValidityToken;
 
 use SL::Helper::CreatePDF qw(:all);
 use SL::Helper::PrintOptions;
@@ -96,6 +97,10 @@ sub action_add {
 
   $self->pre_render();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
@@ -119,6 +124,10 @@ sub action_add_from_order {
 
   $self->reinit_after_new_reclamation();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
@@ -142,6 +151,10 @@ sub action_add_from_delivery_order {
 
   $self->reinit_after_new_reclamation();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
@@ -165,6 +178,10 @@ sub action_add_from_sales_invoice {
 
   $self->reinit_after_new_reclamation();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
@@ -189,6 +206,10 @@ sub action_add_from_purchase_invoice {
 
   $self->reinit_after_new_reclamation();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
@@ -314,6 +335,10 @@ sub action_save_as_new {
            )
          );
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   # save
   $self->action_save();
 }
@@ -1721,6 +1746,16 @@ sub delete {
 sub save {
   my ($self) = @_;
 
+  my $validity_token;
+  if (!$self->reclamation->id) {
+    $validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token(
+      scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE(),
+      token => $::form->{form_validity_token},
+    );
+
+    die t8('The form is not valid anymore.') if !$validity_token;
+  }
+
   my $errors = [];
   my $db     = $self->reclamation->db;
 
@@ -1739,6 +1774,9 @@ sub save {
 
     $self->save_history('SAVED');
 
+    $validity_token->delete if $validity_token;
+    delete $::form->{form_validity_token};
+
     1;
   }) || push(@{$errors}, $db->error);
 
@@ -1784,6 +1822,10 @@ sub action_add_from_reclamation {
 
   $self->reinit_after_new_reclamation();
 
+  if (!$::form->{form_validity_token}) {
+    $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
+  }
+
   $self->render(
     'reclamation/form',
     title => $self->get_title_for('add'),
diff --git a/SL/DB/ValidityToken.pm b/SL/DB/ValidityToken.pm
index 9ca11057b..05521bca7 100644
--- a/SL/DB/ValidityToken.pm
+++ b/SL/DB/ValidityToken.pm
@@ -14,6 +14,7 @@ __PACKAGE__->meta->initialize;
 use constant SCOPE_SALES_INVOICE_POST  => 'SalesInvoice::Post';
 use constant SCOPE_DELIVERY_ORDER_SAVE => 'DeliveryOrder::Save';
 use constant SCOPE_ORDER_SAVE          => 'Order::Save';
+use constant SCOPE_RECLAMATION_SAVE    => 'Reclamation::Save';
 
 sub create {
   my ($class, %params) = @_;
diff --git a/templates/webpages/reclamation/form.html b/templates/webpages/reclamation/form.html
index 5a2afed27..69c9c5967 100644
--- a/templates/webpages/reclamation/form.html
+++ b/templates/webpages/reclamation/form.html
@@ -24,6 +24,10 @@
   [% L.hidden_tag('converted_from_record_type_ref', SELF.reclamation.converted_from_record_type_ref) %]
   [% L.hidden_tag('converted_from_record_id',  SELF.reclamation.converted_from_record_id) %]
 
+  [% IF !SELF.reclamation.id %]
+  [%   L.hidden_tag('form_validity_token', FORM.form_validity_token) %]
+  [% END %]
+
   [%- INCLUDE 'common/flash.html' %]
 
   <div class="tabwidget" id="reclamation_tabs">
