Browse Source

Fixes #4322 - Wrong value in session timeout

Mantas 2 years ago
parent
commit
3ea4c58641

+ 31 - 0
db/migrate/20221110150329_issue_4322_session_timeout_stringify.rb

@@ -0,0 +1,31 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+class Issue4322SessionTimeoutStringify < ActiveRecord::Migration[6.1]
+  def up
+    return if !Setting.exists?(name: 'system_init_done')
+
+    setting = Setting.find_by name: 'session_timeout'
+
+    setting.options['form'].each do |form_option|
+      form_option['options'].each do |option|
+        option['value'] = option['value'].to_s
+      end
+    end
+
+    %i[state_current state_initial].each do |attr|
+      migrate_attribute setting, attr
+    end
+
+    setting.save!
+  end
+
+  def migrate_attribute(object, attr)
+    hash = object.send(attr)
+
+    hash['value'].each_key do |key|
+      hash['value'][key] = hash['value'][key].to_s
+    end
+
+    object.send("#{attr}=", hash)
+  end
+end

+ 5 - 5
db/seeds/settings.rb

@@ -1101,7 +1101,7 @@ Setting.create_if_not_exists(
   frontend:    true
 )
 
-options = [ { value: '0', name: 'disabled' }, { value: 1.hour.seconds, name: __('1 hour') }, { value: 2.hours.seconds, name: __('2 hours') }, { value: 1.day.seconds, name: __('1 day') }, { value: 7.days.seconds, name: __('1 week') }, { value: 14.days.seconds, name: __('2 weeks') }, { value: 21.days.seconds, name: __('3 weeks') }, { value: 28.days.seconds, name: __('4 weeks') } ]
+options = [ { value: '0', name: 'disabled' }, { value: 1.hour.seconds.to_s, name: __('1 hour') }, { value: 2.hours.seconds.to_s, name: __('2 hours') }, { value: 1.day.seconds.to_s, name: __('1 day') }, { value: 7.days.seconds.to_s, name: __('1 week') }, { value: 14.days.seconds.to_s, name: __('2 weeks') }, { value: 21.days.seconds.to_s, name: __('3 weeks') }, { value: 28.days.seconds.to_s, name: __('4 weeks') } ]
 Setting.create_if_not_exists(
   title:       __('Session Timeout'),
   name:        'session_timeout',
@@ -1147,10 +1147,10 @@ Setting.create_if_not_exists(
     prio: 30,
   },
   state:       {
-    'default'         => 4.weeks.seconds,
-    'admin'           => 4.weeks.seconds,
-    'ticket.agent'    => 4.weeks.seconds,
-    'ticket.customer' => 4.weeks.seconds,
+    'default'         => 4.weeks.seconds.to_s,
+    'admin'           => 4.weeks.seconds.to_s,
+    'ticket.agent'    => 4.weeks.seconds.to_s,
+    'ticket.customer' => 4.weeks.seconds.to_s,
   },
   frontend:    true
 )

+ 54 - 0
spec/db/migrate/issue_4322_session_timeout_stringify_spec.rb

@@ -0,0 +1,54 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe Issue4322SessionTimeoutStringify, type: :db_migration do
+  let(:setting)          { Setting.find_by name: 'session_timeout' }
+  let(:old_time_options) { [ { value: '0', name: 'disabled' }, { value: 1.hour.seconds, name: __('1 hour') }, { value: 2.hours.seconds, name: __('2 hours') } ] }
+  let(:new_time_options) { [ { value: '0', name: 'disabled' }, { value: 1.hour.seconds.to_s, name: __('1 hour') }, { value: 2.hours.seconds.to_s, name: __('2 hours') } ] }
+  let(:old_values)       { { value: { 'default' => 4.weeks.seconds.to_s, 'admin'   => 4.weeks.seconds, } } }
+  let(:new_values)       { { value: { 'default' => 4.weeks.seconds.to_s, 'admin'   => 4.weeks.seconds.to_s, } } }
+
+  before do
+    setting.update!(
+      options:       build_setting_options(old_time_options),
+      state_current: old_values,
+      state_initial: old_values
+    )
+  end
+
+  it 'changes from integer to string values' do
+    migrate
+
+    setting.reload
+
+    expect(setting).to have_attributes(
+      options:       build_setting_options(new_time_options),
+      state_current: new_values,
+      state_initial: new_values,
+    )
+  end
+
+  def build_setting_options(time_options)
+    {
+      form: [
+        {
+          display:   __('Default'),
+          null:      false,
+          name:      'default',
+          tag:       'select',
+          options:   time_options,
+          translate: true,
+        },
+        {
+          display:   __('admin'),
+          null:      false,
+          name:      'admin',
+          tag:       'select',
+          options:   time_options,
+          translate: true,
+        },
+      ]
+    }
+  end
+end