From ca15adcacc42babc141463677eae5cb7aa43f2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Wed, 11 Dec 2024 23:41:29 +0200 Subject: [PATCH] Rework patch from #13244. diff --git a/app/models/setting.rb b/app/models/setting.rb index e955a7335..a9f54aa7a 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -250,6 +250,10 @@ class Setting < ApplicationRecord twofa == '3' end + def self.timelog_days_within_past + [Setting.timelog_accept_within_past_days.to_i, 0].max + end + # Helper that returns an array based on per_page_options setting def self.per_page_options_array per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 4a3a79987..e9da423f8 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -181,6 +181,8 @@ class TimeEntry < ApplicationRecord errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) if spent_on && spent_on_changed? && user errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today) + max_past_date = user.today - Setting.timelog_days_within_past + errors.add :base, I18n.t(:error_spent_on_past_date, :max_days => Setting.timelog_accept_within_past_days.to_i, :max_past_date => format_date(max_past_date)) if spent_on < max_past_date end if !Setting.timelog_accept_closed_issues? && issue&.closed? && issue.was_closed? errors.add :base, I18n.t(:error_spent_on_closed_issue) diff --git a/app/views/settings/_timelog.html.erb b/app/views/settings/_timelog.html.erb index 0e860ffd1..96cc5859c 100644 --- a/app/views/settings/_timelog.html.erb +++ b/app/views/settings/_timelog.html.erb @@ -11,6 +11,8 @@

<%= setting_check_box :timelog_accept_future_dates %>

<%= setting_check_box :timelog_accept_closed_issues %>

+ +

<%= setting_text_field :timelog_accept_within_past_days, :size => 6 %> <%= l(:label_day_plural) %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 0fee6786e..1a5e0bb23 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -236,6 +236,7 @@ en: error_can_not_delete_auth_source: "This authentication mode is in use and cannot be deleted." error_spent_on_future_date: "Cannot log time on a future date" error_spent_on_closed_issue: "Cannot log time on a closed issue" + error_spent_on_past_date: "Cannot log time on a date older than %{max_past_date} (%{max_days} days ago)" error_not_allowed_to_log_time_for_other_users: "You are not allowed to log time for other users" error_can_not_execute_macro_html: "Error executing the %{name} macro (%{error})" error_macro_does_not_accept_block: "This macro does not accept a block of text" @@ -521,6 +522,7 @@ en: setting_timelog_max_hours_per_day: Maximum hours that can be logged per day and user setting_timelog_accept_future_dates: Accept time logs on future dates setting_timelog_accept_closed_issues: Accept time logs on closed issues + setting_timelog_accept_within_past_days: Accept time logs within the past setting_show_status_changes_in_mail_subject: Show status changes in issue mail notifications subject setting_project_list_defaults: Projects list defaults setting_twofa: Two-factor authentication diff --git a/config/settings.yml b/config/settings.yml index 0aa8f0a44..5c4f8356b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -349,6 +349,9 @@ timelog_accept_future_dates: default: 1 timelog_accept_closed_issues: default: 1 +timelog_accept_within_past_days: + format: int + default: 36525 show_status_changes_in_mail_subject: default: 1 wiki_tablesort_enabled: diff --git a/test/unit/time_entry_test.rb b/test/unit/time_entry_test.rb index 578c7037c..1b48b210c 100644 --- a/test/unit/time_entry_test.rb +++ b/test/unit/time_entry_test.rb @@ -197,6 +197,19 @@ class TimeEntryTest < ActiveSupport::TestCase end end + def test_should_restrict_past_dates + with_settings :timelog_accept_within_past_days => '2' do + entry_two_days_ago = TimeEntry.generate + entry_two_days_ago.spent_on = User.current.today - 2 + assert entry_two_days_ago.save + + entry_three_days_ago = TimeEntry.generate + entry_three_days_ago.spent_on = User.current.today - 3 + assert !entry_three_days_ago.save + assert entry_three_days_ago.errors[:base].present? + end + end + def test_should_require_spent_on with_settings :timelog_accept_future_dates => '0' do entry = TimeEntry.find(1) -- 2.39.5 (Apple Git-154)