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

Extract permission check for share management to controller concern

Aaron Contreras36f6685a803c
diff --git a/app/components/shares/concerns/authorization.rb b/app/controllers/concerns/shares/work_packages/authorization.rb
similarity index 78%
rename from app/components/shares/concerns/authorization.rb
rename to app/controllers/concerns/shares/work_packages/authorization.rb
index 6c5041866a9..32b4bf5f34e 100644
--- a/app/components/shares/concerns/authorization.rb
+++ b/app/controllers/concerns/shares/work_packages/authorization.rb
@@ -2,7 +2,7 @@
# -- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2023 the OpenProject GmbH
+# Copyright (C) 2010-2024 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
@@ -29,14 +29,22 @@
# ++
module Shares
- module Concerns
+ module WorkPackages
module Authorization
extend ActiveSupport::Concern
included do
def sharing_manageable?
# TODO: Fix this to check based on the entity
- User.current.allowed_in_project?(:share_work_packages, @entity.project)
+ case @entity
+ when WorkPackage
+ User.current.allowed_in_project?(:share_work_packages, @entity.project)
+ else
+ raise ArgumentError, <<~ERROR
+ Checking sharing capabilities for an unsupported entity:
+ - #{@entity.class}
+ ERROR
+ end
end
end
end
diff --git a/app/controllers/shares_controller.rb b/app/controllers/shares_controller.rb
index f3a89fab62a..efb79c77f52 100644
--- a/app/controllers/shares_controller.rb
+++ b/app/controllers/shares_controller.rb
@@ -28,6 +28,7 @@
class SharesController < ApplicationController
include OpTurbo::ComponentStream
+ include Shares::WorkPackages::Authorization
include MemberHelper
before_action :load_entity
@@ -45,8 +46,7 @@ class SharesController < ApplicationController
render Shares::ModalBodyComponent.new(entity: @entity,
shares: @shares,
errors: @errors,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @entity.project),
+ sharing_manageable: sharing_manageable?,
available_roles:), layout: nil
end
@@ -156,8 +156,7 @@ class SharesController < ApplicationController
component: Shares::ModalBodyComponent.new(entity: @entity,
available_roles:,
shares: @new_shares || load_shares,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project),
+ sharing_manageable: sharing_manageable?,
errors: @errors)
)
@@ -168,29 +167,24 @@ class SharesController < ApplicationController
replace_via_turbo_stream(
component: Shares::InviteUserFormComponent.new(entity: @entity,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project),
+ sharing_manageable: sharing_manageable?,
errors: @errors)
)
update_via_turbo_stream(
component: Shares::CounterComponent.new(entity: @entity,
count: current_visible_member_count,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project))
+ sharing_manageable: sharing_manageable?)
)
@new_shares.each do |share|
prepend_via_turbo_stream(
component: Shares::ShareRowComponent.new(share:,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project)),
+ sharing_manageable: sharing_manageable?),
target_component: Shares::ModalBodyComponent.new(entity: @entity,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(
- :share_work_packages, @project
- ),
+ sharing_manageable: sharing_manageable?,
shares: load_shares,
errors: @errors)
)
@@ -202,9 +196,7 @@ class SharesController < ApplicationController
def respond_with_new_invite_form
replace_via_turbo_stream(component: Shares::InviteUserFormComponent.new(entity: @entity,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(
- :share_work_packages, @project
- ),
+ sharing_manageable: sharing_manageable?,
errors: @errors))
respond_with_turbo_streams
@@ -221,23 +213,17 @@ class SharesController < ApplicationController
def respond_with_remove_share
remove_via_turbo_stream(component: Shares::ShareRowComponent.new(share: @share,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(
- :share_work_packages, @project
- )))
+ sharing_manageable: sharing_manageable?))
update_via_turbo_stream(component: Shares::CounterComponent.new(entity: @entity,
count: current_visible_member_count,
- sharing_manageable: User.current.allowed_in_project?(
- :share_work_packages, @project
- )))
+ sharing_manageable: sharing_manageable?))
respond_with_turbo_streams
end
def respond_with_update_user_details
update_via_turbo_stream(component: Shares::UserDetailsComponent.new(share: @share,
- manager_mode: User.current.allowed_in_project?(
- :share_work_packages, @entity.project
- ),
+ manager_mode: sharing_manageable?,
invite_resent: true))
respond_with_turbo_streams
@@ -260,16 +246,14 @@ class SharesController < ApplicationController
remove_via_turbo_stream(
component: Shares::ShareRowComponent.new(share:,
available_roles:,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project))
+ sharing_manageable: sharing_manageable?)
)
end
update_via_turbo_stream(
component: Shares::CounterComponent.new(entity: @entity,
count: current_visible_member_count,
- sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
- @project))
+ sharing_manageable: sharing_manageable?)
)
respond_with_turbo_streams