Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

325 lines
9.4 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:36:37 +01:00
# frozen_string_literal: true
2023-09-21 21:18:36 +02:00
# -- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2023-09-21 21:18:36 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
# ++
2024-06-19 18:47:30 +02:00
class SharesController < ApplicationController
include OpTurbo::ComponentStream
include MemberHelper
before_action :load_entity
2024-06-20 16:50:37 +02:00
before_action :load_selected_shares, only: %i[bulk_update bulk_destroy]
before_action :load_share, only: %i[destroy update resend_invite]
before_action :check_if_manageable, except: %i[index dialog]
before_action :check_if_viewable, only: %i[index dialog]
authorization_checked! :dialog, :index, :create, :update, :destroy, :resend_invite, :bulk_update, :bulk_destroy
2024-06-26 15:12:30 +02:00
def dialog; end
2023-09-21 21:18:36 +02:00
def index
2024-07-09 17:07:13 +02:00
unless sharing_strategy.query.valid?
flash.now[:error] = sharing_strategy.query.errors.full_messages
end
render sharing_strategy.modal_body_component(@errors), layout: nil
2023-09-21 21:18:36 +02:00
end
2024-06-25 15:51:56 +02:00
def create # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
overall_result = []
@errors = ActiveModel::Errors.new(self)
2024-07-10 08:42:34 +02:00
visible_shares_before_adding = sharing_strategy.shares.present?
2024-07-09 17:43:46 +02:00
find_or_create_users(send_notification: send_notification?) do |member_params|
2026-02-02 11:06:25 +01:00
user = User.visible.find_by(id: member_params[:user_id])
2025-08-26 18:39:37 +02:00
if user.present? && (user.locked? || user.deleted?)
2024-06-25 14:21:58 +02:00
@errors.add(:base, I18n.t("sharing.warning_locked_user", user: user.name))
else
service_call = create_or_update_share(member_params[:user_id], [params[:member][:role_id]])
overall_result.push(service_call)
end
end
2024-07-09 17:43:46 +02:00
new_shares = overall_result.map(&:result).reverse
if overall_result.present?
2024-07-09 17:43:46 +02:00
# In case we did not have shares before we have to replace the modal to get rid of the blankstate,
# otherwise we can prepend the new shares
2024-07-10 08:42:34 +02:00
if visible_shares_before_adding
2024-07-09 17:43:46 +02:00
respond_with_prepend_shares(new_shares)
else
respond_with_replace_modal
end
else
respond_with_new_invite_form
end
2023-09-27 18:22:06 +02:00
end
def update
create_or_update_share(@share.principal.id, params[:role_ids])
2024-07-09 17:07:13 +02:00
shares = sharing_strategy.shares(reload: true)
2024-07-09 17:07:13 +02:00
if shares.empty?
respond_with_replace_modal
2024-07-09 17:07:13 +02:00
elsif shares.include?(@share)
respond_with_update_permission_button
else
respond_with_remove_share
end
2023-09-25 18:12:23 +02:00
end
def destroy
destroy_share(@share)
2023-09-25 18:12:23 +02:00
2024-07-09 17:43:46 +02:00
# When we removed the last share we have to replace the modal to show the blankstate
if sharing_strategy.shares(reload: true).empty?
respond_with_replace_modal
else
respond_with_remove_share
end
2023-09-25 18:12:23 +02:00
end
2024-07-09 17:07:13 +02:00
# TODO: This is still work package specific
2023-11-15 15:13:49 -05:00
def resend_invite
OpenProject::Notifications.send(OpenProject::Events::WORK_PACKAGE_SHARED,
work_package_member: @share,
send_notifications: true)
respond_with_update_user_details
2023-11-15 15:13:49 -05:00
end
2024-06-20 16:50:37 +02:00
def bulk_update
@selected_shares.each { |share| create_or_update_share(share.principal.id, params[:role_ids]) }
2024-06-20 16:50:37 +02:00
2024-07-09 17:43:46 +02:00
respond_with_bulk_updated_permission_buttons(@selected_shares)
2024-06-20 16:50:37 +02:00
end
def bulk_destroy
@selected_shares.each { |share| destroy_share(share) }
2024-06-20 16:50:37 +02:00
2024-07-09 17:43:46 +02:00
if sharing_strategy.shares(reload: true).empty?
2024-06-20 16:50:37 +02:00
respond_with_replace_modal
else
2024-07-09 17:43:46 +02:00
respond_with_bulk_removed_shares(@selected_shares)
2024-06-20 16:50:37 +02:00
end
end
2023-09-25 18:12:23 +02:00
private
attr_reader :sharing_strategy
def check_if_viewable
return if sharing_strategy.viewable? || sharing_strategy.manageable?
render_403
end
def check_if_manageable
return if sharing_strategy.manageable?
render_403
end
def destroy_share(share)
Shares::DeleteService
.new(user: current_user, model: share, contract_class: sharing_strategy.delete_contract_class)
2024-06-24 11:38:34 -05:00
.call
end
def create_or_update_share(user_id, role_ids)
2024-06-25 15:51:56 +02:00
Shares::CreateOrUpdateService.new(
user: current_user,
create_contract_class: sharing_strategy.create_contract_class,
update_contract_class: sharing_strategy.update_contract_class
).call(entity: @entity, user_id:, role_ids:)
end
def respond_with_replace_modal
2024-07-09 17:07:13 +02:00
sharing_strategy.shares(reload: true)
replace_via_turbo_stream(component: sharing_strategy.modal_body_component(@errors))
respond_with_turbo_streams
end
2024-07-09 17:43:46 +02:00
def respond_with_prepend_shares(new_shares)
replace_via_turbo_stream(
2024-06-25 15:51:56 +02:00
component: Shares::InviteUserFormComponent.new(
strategy: sharing_strategy,
2024-06-25 15:51:56 +02:00
errors: @errors
)
)
update_via_turbo_stream(
2024-06-25 15:51:56 +02:00
component: Shares::CounterComponent.new(
strategy: sharing_strategy,
2024-07-09 17:43:46 +02:00
count: sharing_strategy.shares(reload: true).count
2024-06-25 15:51:56 +02:00
)
)
2024-07-09 17:43:46 +02:00
new_shares.each do |share|
prepend_via_turbo_stream(
component: Shares::ShareRowComponent.new(share:, strategy: sharing_strategy),
target_component: Shares::ManageSharesComponent.new(strategy: sharing_strategy, modal_content: nil, errors: @errors)
)
end
respond_with_turbo_streams
end
def respond_with_new_invite_form
2024-06-25 15:51:56 +02:00
replace_via_turbo_stream(
component: Shares::InviteUserFormComponent.new(
strategy: sharing_strategy,
2024-06-25 15:51:56 +02:00
errors: @errors
)
)
respond_with_turbo_streams
end
2023-10-24 12:56:48 -05:00
def respond_with_update_permission_button
2024-06-25 15:51:56 +02:00
replace_via_turbo_stream(
component: Shares::PermissionButtonComponent.new(
share: @share,
strategy: sharing_strategy,
2024-06-25 15:51:56 +02:00
data: { "test-selector": "op-share-dialog-update-role" }
)
)
2023-10-24 12:56:48 -05:00
respond_with_turbo_streams
end
def respond_with_remove_share
2024-06-25 15:51:56 +02:00
remove_via_turbo_stream(
component: Shares::ShareRowComponent.new(
share: @share,
strategy: sharing_strategy
2024-06-25 15:51:56 +02:00
)
)
update_via_turbo_stream(
component: Shares::CounterComponent.new(
strategy: sharing_strategy,
2024-07-09 17:43:46 +02:00
count: sharing_strategy.shares(reload: true).count
2024-06-25 15:51:56 +02:00
)
)
respond_with_turbo_streams
end
def respond_with_update_user_details
2024-06-25 15:51:56 +02:00
update_via_turbo_stream(
component: Shares::UserDetailsComponent.new(
share: @share,
2024-07-10 10:18:00 +02:00
strategy: sharing_strategy,
2024-06-25 15:51:56 +02:00
invite_resent: true
)
)
respond_with_turbo_streams
end
2024-07-09 17:43:46 +02:00
def respond_with_bulk_updated_permission_buttons(selected_shares)
selected_shares.each do |share|
2024-06-20 16:50:37 +02:00
replace_via_turbo_stream(
2024-06-25 15:51:56 +02:00
component: Shares::PermissionButtonComponent.new(
share:,
strategy: sharing_strategy,
2024-06-25 15:51:56 +02:00
data: { "test-selector": "op-share-dialog-update-role" }
)
2024-06-20 16:50:37 +02:00
)
end
respond_with_turbo_streams
end
2024-07-09 17:43:46 +02:00
def respond_with_bulk_removed_shares(selected_shares)
selected_shares.each do |share|
2024-06-20 16:50:37 +02:00
remove_via_turbo_stream(
2024-06-25 15:51:56 +02:00
component: Shares::ShareRowComponent.new(
share:,
strategy: sharing_strategy
2024-06-25 15:51:56 +02:00
)
2024-06-20 16:50:37 +02:00
)
end
update_via_turbo_stream(
2024-06-25 15:51:56 +02:00
component: Shares::CounterComponent.new(
2024-07-09 17:43:46 +02:00
count: sharing_strategy.shares(reload: true).count,
strategy: sharing_strategy
2024-06-25 15:51:56 +02:00
)
2024-06-20 16:50:37 +02:00
)
respond_with_turbo_streams
end
def send_notification?
return false if @entity.is_a?(WorkPackage) # For WorkPackages we have a custom notification
true
end
2024-06-27 10:45:05 +02:00
def load_entity # rubocop:disable Metrics/AbcSize
if params["work_package_id"]
@entity = WorkPackage.visible.find(params["work_package_id"])
@project = @entity.project
2024-07-09 17:07:13 +02:00
@sharing_strategy = SharingStrategies::WorkPackageStrategy.new(@entity, user: current_user, query_params:)
elsif params["project_query_id"]
@entity = ProjectQuery.visible.find(params["project_query_id"])
2024-07-09 17:07:13 +02:00
@sharing_strategy = SharingStrategies::ProjectQueryStrategy.new(@entity, user: current_user, query_params:)
else
raise ArgumentError, <<~ERROR
Nested the SharesController under an entity controller that is not yet configured to support sharing.
Edit the SharesController#load_entity method to load the entity from the correct parent and specify what sharing
strategy should be applied.
Params: #{params.to_unsafe_h}
Request Path: #{request.path}
ERROR
end
2023-09-26 16:52:38 +02:00
end
2023-09-27 18:22:06 +02:00
def load_share
@share = @entity.members.find(params[:id])
2023-09-27 18:22:06 +02:00
end
def load_selected_shares
@selected_shares = Member.includes(:principal)
.of_entity(@entity)
.where(id: params[:share_ids])
end
2024-07-09 17:07:13 +02:00
def query_params
params
.slice(:filters, :sortBy, :groupBy)
.permit! # ParamsToQueryService will parse the data, so we can permit everything here
end
2023-09-21 21:18:36 +02:00
end