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

implement updating of share

ulferts7c95a33a546c
diff --git a/app/components/work_packages/share/invite_user_form_component.html.erb b/app/components/work_packages/share/invite_user_form_component.html.erb
index 3579ff7a3d9..1a91208d775 100644
--- a/app/components/work_packages/share/invite_user_form_component.html.erb
+++ b/app/components/work_packages/share/invite_user_form_component.html.erb
@@ -10,7 +10,7 @@
end
new_user_row.with_column(mr: 2) do
- render(WorkPackages::Share::PermissionButtonComponent.new(permission_value: Role::BUILTIN_WORK_PACKAGE_COMMENTER,
+ render(WorkPackages::Share::PermissionButtonComponent.new(share: new_share,
form_arguments: { builder: form, name: "role_id" },
data: { 'test-selector': 'op-share-wp-invite-role' }))
end
diff --git a/app/components/work_packages/share/invite_user_form_component.rb b/app/components/work_packages/share/invite_user_form_component.rb
index dbe2f7699b4..43e704f216d 100644
--- a/app/components/work_packages/share/invite_user_form_component.rb
+++ b/app/components/work_packages/share/invite_user_form_component.rb
@@ -39,6 +39,10 @@ module WorkPackages
@member = member
@work_package = work_package
end
+
+ def new_share
+ Member.new(roles: [Role.new(builtin: Role::BUILTIN_WORK_PACKAGE_COMMENTER)])
+ end
end
end
end
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 30f74c9bc86..6056a2a4001 100644
--- a/app/components/work_packages/share/modal_body_component.html.erb
+++ b/app/components/work_packages/share/modal_body_component.html.erb
@@ -37,8 +37,6 @@
shared_users.each do |user|
share = user.work_package_shares.first
- # TODO: handle having multiple roles e.g. from a group
- role = share.roles.first
invited_user_list.with_row(data: { 'test-selector': "op-share-wp-active-user-#{user.id}" }) do
grid_layout('op-share-wp-modal-body--user-row', tag: :div, align_items: :center, classes: 'ellipsis') do |user_row_grid|
@@ -48,7 +46,8 @@
if sharing_enabled?
user_row_grid.with_area(:button, tag: :div, color: :subtle) do
- render(WorkPackages::Share::PermissionButtonComponent.new(permission_value: role.builtin))
+ render(WorkPackages::Share::PermissionButtonComponent.new(share:,
+ data: { 'test-selector': 'op-share-wp-update-role' }))
end
end
diff --git a/app/components/work_packages/share/modal_body_component.rb b/app/components/work_packages/share/modal_body_component.rb
index 10c22714935..c5e51aac5a4 100644
--- a/app/components/work_packages/share/modal_body_component.rb
+++ b/app/components/work_packages/share/modal_body_component.rb
@@ -51,7 +51,7 @@ module WorkPackages
.having_entity_membership(@work_package)
.includes(work_package_shares: :roles)
.where(work_package_shares: { entity: @work_package })
- .order('work_package_shares.updated_at DESC')
+ .order('work_package_shares.created_at DESC')
end
def sharing_enabled?
diff --git a/app/components/work_packages/share/permission_button_component.html.erb b/app/components/work_packages/share/permission_button_component.html.erb
index 83993405495..0fae3af3716 100644
--- a/app/components/work_packages/share/permission_button_component.html.erb
+++ b/app/components/work_packages/share/permission_button_component.html.erb
@@ -1,11 +1,23 @@
<%=
- render(Primer::Alpha::ActionMenu.new(**@system_arguments.merge(select_variant: :single, dynamic_label: true, color: :subtle))) do |menu|
+ render(Primer::Alpha::ActionMenu.new(**{ select_variant: :single,
+ dynamic_label: true,
+ color: :subtle }
+ .deep_merge(@system_arguments))) do |menu|
menu.with_show_button do |button|
button.with_trailing_action_icon(icon: :"triangle-down")
- permission_name(@permission_value)
+ permission_name(active_role.builtin)
end
options.each do |option|
- menu.with_item(label: option[:label], active: option[:value] == @permission_value, data: { value: option[:value] })
+ menu.with_item(label: option[:label],
+ href: update_path,
+ method: :patch,
+ active: option_active?(option),
+ data: { value: option[:value] },
+ form_arguments: {
+ method: :patch,
+ name: 'role_ids[]',
+ value: option[:value]
+ })
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 8de9e14df04..90e956bdca7 100644
--- a/app/components/work_packages/share/permission_button_component.rb
+++ b/app/components/work_packages/share/permission_button_component.rb
@@ -32,15 +32,29 @@ module WorkPackages
include ApplicationHelper
include OpPrimer::ComponentHelpers
- def initialize(permission_value:, **system_arguments)
+ def initialize(share:, **system_arguments)
super
- @permission_value = permission_value
+ @share = share
@system_arguments = system_arguments
end
+ # Switches the component to either update the share directly (by sending a PATCH to the share path)
+ # or be passive and work like a select inside a form.
+ def update_path
+ if share.persisted?
+ work_packages_share_path(share)
+ end
+ end
+
+ def option_active?(option)
+ option[:value] == active_role.builtin
+ end
+
private
+ attr_reader :share
+
def options
[
{ label: I18n.t('work_package.sharing.permissions.edit'), value: Role::BUILTIN_WORK_PACKAGE_EDITOR },
@@ -49,6 +63,11 @@ module WorkPackages
]
end
+ def active_role
+ # TODO: handle having more than one role
+ share.roles.first
+ end
+
def permission_name(value)
options.select { |option| option[:value] == value }
end
diff --git a/app/controllers/work_packages/shares_controller.rb b/app/controllers/work_packages/shares_controller.rb
index 9c68379a755..daa2d0d0a55 100644
--- a/app/controllers/work_packages/shares_controller.rb
+++ b/app/controllers/work_packages/shares_controller.rb
@@ -30,7 +30,7 @@ class WorkPackages::SharesController < ApplicationController
include OpTurbo::ComponentStream
before_action :find_work_package, only: %i[index create]
- before_action :find_share, only: %i[destroy]
+ before_action :find_share, only: %i[destroy update]
before_action :find_project
before_action :authorize
@@ -44,9 +44,16 @@ class WorkPackages::SharesController < ApplicationController
.new(user: current_user)
.call(entity: @work_package,
user_id: params[:member][:user_id],
- # Role has a left join on permissions included leading to multiple ids being returned which
- # is why we unscope.
- role_ids: WorkPackageRole.unscoped.where(builtin: params[:member][:role_id]).pluck(:id))
+ role_ids: find_role_ids(params[:member][:role_id]))
+
+ respond_with_update_modal
+ end
+
+ def update
+ # TODO: error handling
+ WorkPackageMembers::UpdateService
+ .new(user: current_user, model: @share)
+ .call(role_ids: find_role_ids(params[:role_ids]))
respond_with_update_modal
end
@@ -60,8 +67,6 @@ class WorkPackages::SharesController < ApplicationController
respond_with_update_modal
end
- # Todo: Update
-
private
def respond_with_update_modal
@@ -85,4 +90,10 @@ class WorkPackages::SharesController < ApplicationController
def find_project
@project = @work_package.project
end
+
+ def find_role_ids(builtin_value)
+ # Role has a left join on permissions included leading to multiple ids being returned which
+ # is why we unscope.
+ WorkPackageRole.unscoped.where(builtin: builtin_value).pluck(:id)
+ end
end
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 636adcefb77..917364c656a 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -118,7 +118,7 @@ Rails.application.reloader.to_prepare do
map.permission :share_work_packages,
{
- 'work_packages/shares': %i[index create destroy]
+ 'work_packages/shares': %i[index create destroy update]
},
permissible_on: :project,
dependencies: %i[edit_work_packages view_shared_work_packages],
diff --git a/config/routes.rb b/config/routes.rb
index d181bf2c883..072a4491568 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -454,7 +454,7 @@ OpenProject::Application.routes.draw do
# cover the functionality. Route is being used in work-package-service.js:331
get '/bulk' => 'bulk#destroy'
- resources :shares, only: %i[destroy]
+ resources :shares, only: %i[destroy update]
end
resources :work_packages, only: [:index] do
diff --git a/spec/features/work_packages/share_spec.rb b/spec/features/work_packages/share_spec.rb
index c585dcf39e2..3ef2a724b13 100644
--- a/spec/features/work_packages/share_spec.rb
+++ b/spec/features/work_packages/share_spec.rb
@@ -32,6 +32,7 @@ require 'spec_helper'
RSpec.describe 'Work package sharing',
:js,
+ :with_cuprite,
with_flag: { work_package_sharing: true } do
let(:sharer_role) do
create(:role,
@@ -72,9 +73,6 @@ RSpec.describe 'Work package sharing',
current_user { create(:user) }
context 'when having share permission' do
- # TODO:
- # - Check email being sent
- # - Check updating a user's role
it 'allows seeing and administrating sharing' do
work_package_page.visit!
@@ -113,9 +111,38 @@ RSpec.describe 'Work package sharing',
share_modal.expect_shared_with(not_shared_yet_with_user, 'Edit')
- # Sent out email only on first share and not again when updating
+ # Sent out email only on first share and not again when updating.
perform_enqueued_jobs
expect(ActionMailer::Base.deliveries.size).to eq(1)
+
+ # Updating the share
+ share_modal.change_role(not_shared_yet_with_user, 'Comment')
+
+ share_modal.expect_shared_with(not_shared_yet_with_user, 'Comment')
+
+ # Sent out email only on first share and not again when updating so the
+ # count should still be 1.
+ perform_enqueued_jobs
+ expect(ActionMailer::Base.deliveries.size).to eq(1)
+
+ # Closing and reopening the modal will show the same state as before.
+ share_modal.close
+
+ click_button 'Share'
+
+ # These users were not changed
+ share_modal.expect_shared_with(view_user, 'View')
+ share_modal.expect_shared_with(comment_user, 'Comment')
+ share_modal.expect_shared_with(shared_project_user, 'Edit')
+ # This user's role was updated
+ share_modal.expect_shared_with(not_shared_yet_with_user, 'Comment')
+ # This user's share was revoked
+ share_modal.expect_not_shared_with(edit_user)
+
+ # This user has never been added
+ share_modal.expect_not_shared_with(non_shared_project_user)
+
+ share_modal.expect_shared_count_of(4)
end
end
diff --git a/spec/routing/work_package/shares_spec.rb b/spec/routing/work_package/shares_spec.rb
index 2a3ab1e6f18..610ea91b85a 100644
--- a/spec/routing/work_package/shares_spec.rb
+++ b/spec/routing/work_package/shares_spec.rb
@@ -34,4 +34,10 @@ RSpec.describe 'work package share routes' do
action: 'destroy',
id: '5')
end
+
+ it 'connects PATCH /work_packages/shares/:id to work_packages/shares#update' do
+ expect(patch('/work_packages/shares/5')).to route_to(controller: 'work_packages/shares',
+ action: 'update',
+ id: '5')
+ end
end
diff --git a/spec/support/components/work_packages/share_modal.rb b/spec/support/components/work_packages/share_modal.rb
index f9db9dfc94b..a2a096b4c92 100644
--- a/spec/support/components/work_packages/share_modal.rb
+++ b/spec/support/components/work_packages/share_modal.rb
@@ -70,6 +70,18 @@ module Components
end
end
+ def change_role(user, role_name)
+ within user_row(user) do
+ find('[data-test-selector="op-share-wp-update-role"]').click
+
+ find('.ActionListContent', text: role_name).click
+ end
+ end
+
+ def close
+ modal_element.send_keys(:escape)
+ end
+
def expect_shared_with(user, role_name = nil)
within active_list do
expect(page)