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

Allow sharing with a User despite the Group share it might already have

Aaron Contreras13b739ab941b
diff --git a/app/components/work_packages/share/modal_body_component.html.erb b/app/components/work_packages/share/modal_body_component.html.erb
index 4f5ee382f0d..bdd24d120d7 100644
--- a/app/components/work_packages/share/modal_body_component.html.erb
+++ b/app/components/work_packages/share/modal_body_component.html.erb
@@ -27,7 +27,9 @@
end
shared_principals.each do |principal|
- share = principal.work_package_shares.where(entity: @work_package).first
+ share = principal.work_package_shares
+ .where(entity: @work_package)
+ .first
render(WorkPackages::Share::ShareRowComponent.new(share:,
container: border_box))
diff --git a/app/components/work_packages/share/modal_body_component.rb b/app/components/work_packages/share/modal_body_component.rb
index f322d85c257..b493b24ed32 100644
--- a/app/components/work_packages/share/modal_body_component.rb
+++ b/app/components/work_packages/share/modal_body_component.rb
@@ -75,8 +75,9 @@ module WorkPackages
def shared_principals
@shared_principals ||= Principal
.having_entity_membership(@work_package)
- .includes(work_package_shares: :roles)
- .where(work_package_shares: { entity: @work_package })
+ .includes(work_package_shares: { roles: :member_roles })
+ .where(work_package_shares: { entity: @work_package },
+ member_roles: { inherited_from: nil })
.ordered_by_name
end
end
diff --git a/app/components/work_packages/share/permission_button_component.rb b/app/components/work_packages/share/permission_button_component.rb
index 4a687277e82..5706a768c7e 100644
--- a/app/components/work_packages/share/permission_button_component.rb
+++ b/app/components/work_packages/share/permission_button_component.rb
@@ -70,8 +70,14 @@ module WorkPackages
end
def active_role
- # TODO: handle having more than one role
- share.roles.first
+ if share.persisted?
+ share.roles
+ .joins(:member_roles)
+ .where(member_roles: { inherited_from: nil })
+ .first
+ else
+ share.roles.first
+ end
end
def permission_name(value)
diff --git a/app/contracts/work_package_members/base_contract.rb b/app/contracts/work_package_members/base_contract.rb
index 66bc4fd61d2..036714b6c8c 100644
--- a/app/contracts/work_package_members/base_contract.rb
+++ b/app/contracts/work_package_members/base_contract.rb
@@ -35,7 +35,7 @@ module WorkPackageMembers
validate :user_allowed_to_manage
validate :role_grantable
- validate :single_role
+ validate :single_non_inherited_role
validate :project_set
validate :entity_set
@@ -54,8 +54,8 @@ module WorkPackageMembers
model.project)
end
- def single_role
- errors.add(:roles, :more_than_one) if active_roles.count > 1
+ def single_non_inherited_role
+ errors.add(:roles, :more_than_one) if active_non_inherited_roles.count > 1
end
def role_grantable
@@ -67,7 +67,15 @@ module WorkPackageMembers
end
def active_roles
- model.member_roles.reject(&:marked_for_destruction?).map(&:role)
+ active_member_roles.map(&:role)
+ end
+
+ def active_non_inherited_roles
+ active_member_roles.reject(&:inherited_from).map(&:role)
+ end
+
+ def active_member_roles
+ model.member_roles.reject(&:marked_for_destruction?)
end
def entity_set
diff --git a/spec/contracts/work_package_members/shared_contract_examples.rb b/spec/contracts/work_package_members/shared_contract_examples.rb
index b2218288e02..08b9d689c4d 100644
--- a/spec/contracts/work_package_members/shared_contract_examples.rb
+++ b/spec/contracts/work_package_members/shared_contract_examples.rb
@@ -79,8 +79,7 @@ RSpec.shared_examples_for 'work package member contract' do
it_behaves_like 'contract is invalid', base: :error_unauthorized
end
- # Needs to be changed once groups are introduced
- context 'if more than one role is assigned' do
+ context 'if more than one non-inherited role is assigned' do
let(:member_roles) do
[build_stubbed(:view_work_package_role), build_stubbed(:comment_work_package_role)]
end
diff --git a/spec/features/work_packages/share_spec.rb b/spec/features/work_packages/share_spec.rb
index a767762c548..85774bebc92 100644
--- a/spec/features/work_packages/share_spec.rb
+++ b/spec/features/work_packages/share_spec.rb
@@ -154,32 +154,41 @@ RSpec.describe 'Work package sharing',
click_button 'Share'
share_modal.expect_open
- share_modal.invite_group(not_shared_yet_with_group, 'Comment')
- share_modal.expect_shared_with(not_shared_yet_with_group, 'Comment', position: 1)
-
- # Close and re-open modal
+ share_modal.invite_group(not_shared_yet_with_group, 'View')
+
+ # Inviting a group propagates the membership to the group's users. However, these propagated
+ # memberships are not expected to be visible. We only care about seeing the group's share.
+ share_modal.expect_shared_with(not_shared_yet_with_group, 'View', position: 1)
+ shared_principals = Principal.where(id: Member.of_work_package(work_package).select(:user_id))
+ expect(shared_principals)
+ .to include(not_shared_yet_with_group, gilfoyle, dinesh)
+ # Closing the modal and re-opening it to ensure group's users aren't included in the
+ # list of shares
share_modal.close
share_modal.expect_closed
click_button 'Share'
share_modal.expect_open
+ share_modal.expect_shared_with(not_shared_yet_with_group, 'View')
+ share_modal.expect_not_shared_with(dinesh)
+ share_modal.expect_not_shared_with(gilfoyle)
- # Shares are propagated to the group's users
- share_modal.expect_shared_with(not_shared_yet_with_group, 'Comment')
- share_modal.expect_shared_with(dinesh, 'Comment')
- share_modal.expect_shared_with(gilfoyle, 'Comment')
+ # Inviting a user to a Work Package independently of the the group displays
+ # said user in the shares list
+ share_modal.invite_user(gilfoyle, 'Comment')
+ share_modal.expect_shared_with(gilfoyle, 'Comment', position: 1)
- # When removing a group's share, its users also get their share removed
+ # When removing a group's share, its users also get their inherited member roles removed
+ # while keeping member roles that were granted independently of the group
share_modal.remove_user(not_shared_yet_with_group)
share_modal.expect_not_shared_with(not_shared_yet_with_group)
- share_modal.close
- share_modal.expect_closed
+ share_modal.expect_shared_with(gilfoyle, 'Comment')
- click_button 'Share'
+ shared_principals.reload
- share_modal.expect_open
- share_modal.expect_not_shared_with(not_shared_yet_with_group)
- share_modal.expect_not_shared_with(dinesh)
- share_modal.expect_not_shared_with(gilfoyle)
+ expect(shared_principals)
+ .to include(gilfoyle)
+ expect(shared_principals)
+ .not_to include(not_shared_yet_with_group, dinesh)
end
end