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

Inject permission checks from controller

Aaron Contrerasb318231c202f
diff --git a/app/components/shares/counter_component.rb b/app/components/shares/counter_component.rb
index 5b22c7fa2b0..9bd01472efb 100644
--- a/app/components/shares/counter_component.rb
+++ b/app/components/shares/counter_component.rb
@@ -33,19 +33,23 @@ module Shares
include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- include Shares::Concerns::Authorization
- def initialize(entity:, count:)
+ def initialize(entity:,
+ count:,
+ sharing_manageable:)
super
@entity = entity
@count = count
+ @sharing_manageable = sharing_manageable
end
private
attr_reader :entity, :count
+ def sharing_manageable? = @sharing_manageable
+
def shared_with_anyone_else_other_than_myself?
Member.of_entity(@entity)
.where.not(principal: User.current)
diff --git a/app/components/shares/invite_user_form_component.html.erb b/app/components/shares/invite_user_form_component.html.erb
index 963065e9b2b..fa52ae08d80 100644
--- a/app/components/shares/invite_user_form_component.html.erb
+++ b/app/components/shares/invite_user_form_component.html.erb
@@ -1,11 +1,11 @@
<%=
component_wrapper do
- if sharing_manageable?
+ if @sharing_manageable
primer_form_with(
model: new_share,
url: url_for([@entity, Member]),
data: { controller: 'user-limit ' \
- 'work-packages--share--user-selected',
+ 'work-packages--share--user-selected',
'application-target': 'dynamic',
'user-limit-open-seats-value': OpenProject::Enterprise.open_seats_count,
action: 'submit->work-packages--share--user-selected#ensureUsersSelected' }
diff --git a/app/components/shares/invite_user_form_component.rb b/app/components/shares/invite_user_form_component.rb
index abaca7db068..48ae8e0715e 100644
--- a/app/components/shares/invite_user_form_component.rb
+++ b/app/components/shares/invite_user_form_component.rb
@@ -31,14 +31,17 @@ module Shares
include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- include Shares::Concerns::Authorization
- def initialize(entity:, available_roles:, errors: nil)
+ def initialize(entity:,
+ available_roles:,
+ sharing_manageable:,
+ errors: nil)
super
@entity = entity
- @errors = errors
@available_roles = available_roles
+ @sharing_manageable = sharing_manageable
+ @errors = errors
end
def new_share
diff --git a/app/components/shares/modal_body_component.html.erb b/app/components/shares/modal_body_component.html.erb
index 3e8d3ad558c..2ac2d17c80d 100644
--- a/app/components/shares/modal_body_component.html.erb
+++ b/app/components/shares/modal_body_component.html.erb
@@ -2,7 +2,10 @@
component_wrapper(tag: 'turbo-frame') do
flex_layout(data: { turbo: true }) do |modal_content|
modal_content.with_row do
- render(Shares::InviteUserFormComponent.new(entity: @entity, available_roles: @available_roles, errors: @errors))
+ render(Shares::InviteUserFormComponent.new(entity: @entity,
+ available_roles: @available_roles,
+ sharing_manageable: @sharing_manageable,
+ errors: @errors))
end
modal_content.with_row(mt: 3,
@@ -13,7 +16,9 @@
border_box.with_header(color: :muted, data: { 'test-selector': 'op-share-wp-header' }) do
grid_layout('op-share-wp-modal-body--header', tag: :div, align_items: :center) do |header_grid|
header_grid.with_area(:counter, tag: :div) do
- render(Shares::CounterComponent.new(entity: @entity, count: @shares.size))
+ render(Shares::CounterComponent.new(entity: @entity,
+ count: @shares.size,
+ sharing_manageable: @sharing_manageable))
end
header_grid.with_area(:actions,
@@ -70,7 +75,7 @@
tag: :div,
hidden: true, # Prevent flicker on initial render
data: { 'work-packages--share--bulk-selection-target': 'bulkActions' }) do
- if sharing_manageable?
+ if @sharing_manageable
concat(
render(Shares::BulkPermissionButtonComponent.new(entity: @entity, available_roles: @available_roles))
)
@@ -107,7 +112,10 @@
end
else
@shares.each do |share|
- render(Shares::ShareRowComponent.new(share: share, available_roles: @available_roles, container: border_box))
+ render(Shares::ShareRowComponent.new(share: share,
+ available_roles: @available_roles,
+ sharing_manageable: @sharing_manageable,
+ container: border_box))
end
end
end
diff --git a/app/components/shares/modal_body_component.rb b/app/components/shares/modal_body_component.rb
index 72cfc850351..064cdf4efad 100644
--- a/app/components/shares/modal_body_component.rb
+++ b/app/components/shares/modal_body_component.rb
@@ -32,17 +32,25 @@ module Shares
include MemberHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- include Shares::Concerns::Authorization
- attr_reader :entity, :shares, :errors, :available_roles
-
- def initialize(entity:, shares:, available_roles:, errors: nil)
+ attr_reader :entity,
+ :shares,
+ :available_roles,
+ :sharing_manageable,
+ :errors
+
+ def initialize(entity:,
+ shares:,
+ available_roles:,
+ sharing_manageable:,
+ errors: nil)
super
@entity = entity
@shares = shares
- @errors = errors
@available_roles = available_roles
+ @sharing_manageable = sharing_manageable
+ @errors = errors
end
def self.wrapper_key
diff --git a/app/components/shares/share_row_component.rb b/app/components/shares/share_row_component.rb
index bb981e146ef..3c6b094698e 100644
--- a/app/components/shares/share_row_component.rb
+++ b/app/components/shares/share_row_component.rb
@@ -33,10 +33,10 @@ module Shares
include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- include Shares::Concerns::Authorization
def initialize(share:,
available_roles:,
+ sharing_manageable:,
container: nil)
super
@@ -44,6 +44,7 @@ module Shares
@entity = share.entity
@principal = share.principal
@available_roles = available_roles
+ @sharing_manageable = sharing_manageable
@container = container
end
@@ -59,6 +60,8 @@ module Shares
@share_editable ||= User.current != share.principal && sharing_manageable?
end
+ def sharing_manageable? = @sharing_manageable
+
def grid_css_classes
if sharing_manageable?
"op-share-wp-modal-body--user-row_manageable"
diff --git a/app/components/shares/user_details_component.rb b/app/components/shares/user_details_component.rb
index 0003900998a..8f189f2b6db 100644
--- a/app/components/shares/user_details_component.rb
+++ b/app/components/shares/user_details_component.rb
@@ -34,7 +34,7 @@ module Shares
include OpPrimer::ComponentHelpers
def initialize(share:,
- manager_mode: User.current.allowed_in_project?(:share_work_packages, share.project),
+ manager_mode:,
invite_resent: false)
super
diff --git a/app/controllers/shares_controller.rb b/app/controllers/shares_controller.rb
index 78f6167c8bb..f3a89fab62a 100644
--- a/app/controllers/shares_controller.rb
+++ b/app/controllers/shares_controller.rb
@@ -42,7 +42,12 @@ class SharesController < ApplicationController
flash.now[:error] = query.errors.full_messages
end
- render Shares::ModalBodyComponent.new(entity: @entity, shares: @shares, errors: @errors, available_roles:), layout: nil
+ render Shares::ModalBodyComponent.new(entity: @entity,
+ shares: @shares,
+ errors: @errors,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @entity.project),
+ available_roles:), layout: nil
end
def create
@@ -132,8 +137,8 @@ class SharesController < ApplicationController
def destroy_share(share)
Shares::DeleteService
- .new(user: current_user, model: share, contract_class: sharing_contract_scope::DeleteContract)
- .call
+ .new(user: current_user, model: share, contract_class: sharing_contract_scope::DeleteContract)
+ .call
end
def create_or_update_share(user_id, role_ids)
@@ -151,6 +156,8 @@ 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),
errors: @errors)
)
@@ -159,18 +166,31 @@ class SharesController < ApplicationController
def respond_with_prepend_shares
replace_via_turbo_stream(
- component: Shares::InviteUserFormComponent.new(entity: @entity, available_roles:, errors: @errors)
+ component: Shares::InviteUserFormComponent.new(entity: @entity,
+ available_roles:,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @project),
+ errors: @errors)
)
update_via_turbo_stream(
- component: Shares::CounterComponent.new(entity: @entity, count: current_visible_member_count)
+ component: Shares::CounterComponent.new(entity: @entity,
+ count: current_visible_member_count,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @project))
)
@new_shares.each do |share|
prepend_via_turbo_stream(
- component: Shares::ShareRowComponent.new(share:, available_roles:),
+ component: Shares::ShareRowComponent.new(share:,
+ available_roles:,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @project)),
target_component: Shares::ModalBodyComponent.new(entity: @entity,
available_roles:,
+ sharing_manageable: User.current.allowed_in_project?(
+ :share_work_packages, @project
+ ),
shares: load_shares,
errors: @errors)
)
@@ -182,6 +202,9 @@ 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
+ ),
errors: @errors))
respond_with_turbo_streams
@@ -196,14 +219,26 @@ class SharesController < ApplicationController
end
def respond_with_remove_share
- remove_via_turbo_stream(component: Shares::ShareRowComponent.new(share: @share, available_roles:))
- update_via_turbo_stream(component: Shares::CounterComponent.new(entity: @entity, count: current_visible_member_count))
+ remove_via_turbo_stream(component: Shares::ShareRowComponent.new(share: @share,
+ available_roles:,
+ sharing_manageable: User.current.allowed_in_project?(
+ :share_work_packages, @project
+ )))
+ 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
+ )))
respond_with_turbo_streams
end
def respond_with_update_user_details
- update_via_turbo_stream(component: Shares::UserDetailsComponent.new(share: @share, invite_resent: true))
+ update_via_turbo_stream(component: Shares::UserDetailsComponent.new(share: @share,
+ manager_mode: User.current.allowed_in_project?(
+ :share_work_packages, @entity.project
+ ),
+ invite_resent: true))
respond_with_turbo_streams
end
@@ -223,12 +258,18 @@ class SharesController < ApplicationController
def respond_with_bulk_removed_shares
@selected_shares.each do |share|
remove_via_turbo_stream(
- component: Shares::ShareRowComponent.new(share:, available_roles:)
+ component: Shares::ShareRowComponent.new(share:,
+ available_roles:,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @project))
)
end
update_via_turbo_stream(
- component: Shares::CounterComponent.new(entity: @entity, count: current_visible_member_count)
+ component: Shares::CounterComponent.new(entity: @entity,
+ count: current_visible_member_count,
+ sharing_manageable: User.current.allowed_in_project?(:share_work_packages,
+ @project))
)
respond_with_turbo_streams
@@ -237,7 +278,7 @@ class SharesController < ApplicationController
def load_entity
@entity = if params["work_package_id"]
WorkPackage.visible.find(params["work_package_id"])
- # TODO: Add support for other entities
+ # TODO: Add support for other entities
else
raise ArgumentError, <<~ERROR
Nested the SharesController under an entity controller that is not yet configured to support sharing.