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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.erbindex 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 @@endnew_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' }))enddiff --git a/app/components/work_packages/share/invite_user_form_component.rb b/app/components/work_packages/share/invite_user_form_component.rbindex 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_packageend++ def new_share+ Member.new(roles: [Role.new(builtin: Role::BUILTIN_WORK_PACKAGE_COMMENTER)])+ endendendenddiff --git a/app/components/work_packages/share/modal_body_component.html.erb b/app/components/work_packages/share/modal_body_component.html.erbindex 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.firstinvited_user_list.with_row(data: { 'test-selector': "op-share-wp-active-user-#{user.id}" }) dogrid_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' }))endenddiff --git a/app/components/work_packages/share/modal_body_component.rb b/app/components/work_packages/share/modal_body_component.rbindex 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')enddef 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.erbindex 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)endoptions.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]+ })endend%>diff --git a/app/components/work_packages/share/permission_button_component.rb b/app/components/work_packages/share/permission_button_component.rbindex 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 WorkPackagesinclude ApplicationHelperinclude OpPrimer::ComponentHelpers- def initialize(permission_value:, **system_arguments)+ def initialize(share:, **system_arguments)super- @permission_value = permission_value+ @share = share@system_arguments = system_argumentsend+ # 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 }enddiff --git a/app/controllers/work_packages/shares_controller.rb b/app/controllers/work_packages/shares_controller.rbindex 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 < ApplicationControllerinclude OpTurbo::ComponentStreambefore_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_projectbefore_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_modalend@@ -60,8 +67,6 @@ class WorkPackages::SharesController < ApplicationControllerrespond_with_update_modalend- # Todo: Update-privatedef respond_with_update_modal@@ -85,4 +90,10 @@ class WorkPackages::SharesController < ApplicationControllerdef find_project@project = @work_package.projectend++ 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)+ endenddiff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex 636adcefb77..917364c656a 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -118,7 +118,7 @@ Rails.application.reloader.to_prepare domap.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.rbindex 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:331get '/bulk' => 'bulk#destroy'- resources :shares, only: %i[destroy]+ resources :shares, only: %i[destroy update]endresources :work_packages, only: [:index] dodiff --git a/spec/features/work_packages/share_spec.rb b/spec/features/work_packages/share_spec.rbindex 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 } dolet(:sharer_role) docreate(: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 roleit 'allows seeing and administrating sharing' dowork_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_jobsexpect(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)endenddiff --git a/spec/routing/work_package/shares_spec.rb b/spec/routing/work_package/shares_spec.rbindex 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' doaction: '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')+ endenddiff --git a/spec/support/components/work_packages/share_modal.rb b/spec/support/components/work_packages/share_modal.rbindex 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 Componentsendend+ 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 doexpect(page)