Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Do not allow time tracking for users that are not visible to you

Klaus Zanders7b69b14399ce
diff --git a/app/models/principal.rb b/app/models/principal.rb
index 20a0b51e10a..aa06360d396 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -31,12 +31,12 @@ class Principal < ApplicationRecord
# Account statuses
# Disables enum scopes to include not_builtin (cf. Principals::Scopes::Status)
- enum status: {
+ enum :status, {
active: 1,
registered: 2,
locked: 3,
invited: 4
- }.freeze, _scopes: false
+ }.freeze, scopes: false
self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
@@ -176,6 +176,11 @@ class Principal < ApplicationRecord
false
end
+ # Returns true if usr or current user is allowed to view the user
+ def visible?(usr = User.current)
+ User.visible(usr).exists?(id: id)
+ end
+
def <=>(other)
if instance_of?(other.class)
to_s.downcase <=> other.to_s.downcase
diff --git a/modules/costs/app/contracts/time_entries/base_contract.rb b/modules/costs/app/contracts/time_entries/base_contract.rb
index 9584151ae9b..275350dfad9 100644
--- a/modules/costs/app/contracts/time_entries/base_contract.rb
+++ b/modules/costs/app/contracts/time_entries/base_contract.rb
@@ -46,6 +46,7 @@ module TimeEntries
validate :validate_hours_are_in_range
validate :validate_project_is_set
validate :validate_work_package
+ validate :validate_user
validates :spent_on,
date: { before_or_equal_to: Proc.new { Date.new(9999, 12, 31) },
@@ -97,6 +98,15 @@ module TimeEntries
end
end
+ def validate_user
+ return unless model.user || model.user_id_changed?
+ return if model.user == model.logged_by
+
+ if user_invisible?
+ errors.add :user_id, :invalid
+ end
+ end
+
def validate_hours_are_in_range
errors.add :hours, :invalid if model.hours&.negative?
end
@@ -117,8 +127,8 @@ module TimeEntries
model.work_package && model.project != model.work_package.project
end
- def validate_logged_by_current_user
- errors.add :logged_by_id, :not_current_user if model.logged_by != logged_by
+ def user_invisible?
+ model.user.nil? || !model.user.visible?
end
def validate_self_timer
diff --git a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
index e5772207f15..e7ba87969db 100644
--- a/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/create_contract_spec.rb
@@ -109,6 +109,14 @@ RSpec.describe TimeEntries::CreateContract do
end
end
+ context "if the user is set to a user that the user has no access to" do
+ let(:user_visible) { false }
+
+ it "is invalid" do
+ expect_valid(false, user_id: %i(invalid))
+ end
+ end
+
context "if time_entry user was not set by system" do
let(:other_user) { build_stubbed(:user) }
let(:time_entry_user) { other_user }
diff --git a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
index d2772e659ad..c8027517da6 100644
--- a/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
+++ b/modules/costs/spec/contracts/time_entries/shared_contract_examples.rb
@@ -51,6 +51,7 @@ RSpec.shared_examples_for "time entry contract" do
let(:time_entry_hours) { 5 }
let(:time_entry_comments) { "A comment" }
let(:work_package_visible) { true }
+ let(:user_visible) { true }
let(:time_entry_day_sum) { 5 }
let(:activities_scope) do
scope = class_double(TimeEntryActivity)
@@ -97,6 +98,8 @@ RSpec.shared_examples_for "time entry contract" do
.to receive(:active_in_project)
.with(time_entry_project)
.and_return(activities_scope)
+
+ allow(time_entry_user).to receive(:visible?).and_return(user_visible)
end
def expect_valid(valid, symbols = {})
diff --git a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
index eaa8c9d1790..90a2595bc34 100644
--- a/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
+++ b/modules/costs/spec/contracts/time_entries/update_contract_spec.rb
@@ -43,7 +43,7 @@ RSpec.describe TimeEntries::UpdateContract do
end
subject(:contract) { described_class.new(time_entry, current_user) }
- let(:permissions) { %i(edit_time_entries) }
+ let(:permissions) { %i(edit_time_entries log_time) }
context "if user is not allowed to edit time entries" do
let(:permissions) { [] }
@@ -127,6 +127,19 @@ RSpec.describe TimeEntries::UpdateContract do
end
end
+ context "if the user is changed to a user that the user has no access to" do
+ let(:new_user) do
+ create(:user).tap do |user|
+ allow(user).to receive(:visible?).and_return(false)
+ end
+ end
+
+ it "is invalid" do
+ time_entry.user = new_user
+ expect_valid(false, user_id: %i(invalid))
+ end
+ end
+
context "if time_entry user is not contract user" do
let(:time_entry_user) { other_user }