2019-07-15 23:00:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TeamRepositoriesController < ApplicationController
|
|
|
|
before_action :load_vars
|
2019-08-29 23:17:38 +08:00
|
|
|
before_action :check_sharing_permissions
|
2019-07-15 23:00:55 +08:00
|
|
|
|
|
|
|
# DELETE :team_id/repositories/:repository_id/team_repositories/:id
|
|
|
|
def destroy
|
2023-10-12 22:16:43 +08:00
|
|
|
team_shared_object = @repository.team_shared_objects.find(destroy_params[:id])
|
2022-08-31 23:19:29 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2022-07-01 17:55:27 +08:00
|
|
|
log_activity(:unshare_inventory, team_shared_object)
|
|
|
|
team_shared_object.destroy!
|
2019-07-16 18:21:20 +08:00
|
|
|
end
|
2022-08-31 23:19:29 +08:00
|
|
|
render json: {}, status: :no_content
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render json: { message: I18n.t('repositories.multiple_share_service.nothing_to_delete') },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
|
|
|
render json: { message: I18n.t('general.error') }, status: :unprocessable_entity
|
2019-07-15 23:00:55 +08:00
|
|
|
end
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
# POST :team_id/repositories/:repository_id/update
|
|
|
|
def update
|
|
|
|
service_call = Repositories::MultipleShareUpdateService.call(repository: @repository,
|
|
|
|
user: current_user,
|
|
|
|
team: current_team,
|
2019-08-06 21:17:13 +08:00
|
|
|
team_ids_for_share: teams_to_share,
|
|
|
|
team_ids_for_unshare: teams_to_unshare,
|
2019-08-13 20:05:55 +08:00
|
|
|
team_ids_for_update: teams_to_update,
|
|
|
|
**share_all_params)
|
2019-08-06 21:17:13 +08:00
|
|
|
if service_call.succeed?
|
2019-08-29 23:17:38 +08:00
|
|
|
render json: { warnings: service_call.warnings.join(', '), status: @repository.i_shared?(current_team) },
|
|
|
|
status: :ok
|
2019-08-06 21:17:13 +08:00
|
|
|
else
|
|
|
|
render json: { errors: service_call.errors.map { |_, v| v }.join(', ') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-15 23:00:55 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
2022-03-09 21:12:16 +08:00
|
|
|
@repository = current_team.repositories.find_by(id: params[:repository_id])
|
|
|
|
|
|
|
|
render_404 unless @repository
|
2019-07-15 23:00:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
params.permit(:team_id, :repository_id, :target_team_id, :permission_level)
|
|
|
|
end
|
|
|
|
|
2023-10-12 22:16:43 +08:00
|
|
|
def destroy_params
|
2019-07-15 23:00:55 +08:00
|
|
|
params.permit(:team_id, :id)
|
|
|
|
end
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
def update_params
|
2019-08-06 21:17:13 +08:00
|
|
|
params.permit(:permission_changes, share_team_ids: [], write_permissions: [])
|
|
|
|
end
|
|
|
|
|
2019-07-15 23:00:55 +08:00
|
|
|
def check_sharing_permissions
|
2019-08-20 22:07:22 +08:00
|
|
|
render_403 unless can_share_repository?(@repository)
|
2019-07-15 23:00:55 +08:00
|
|
|
end
|
2019-07-16 18:21:20 +08:00
|
|
|
|
2019-08-06 21:17:13 +08:00
|
|
|
def teams_to_share
|
|
|
|
existing_shares = @repository.teams_shared_with.pluck(:id)
|
2019-08-29 23:17:38 +08:00
|
|
|
teams_to_share = update_params[:share_team_ids]&.map(&:to_i).to_a - existing_shares
|
|
|
|
wp = update_params[:write_permissions]&.map(&:to_i)
|
2019-08-06 21:17:13 +08:00
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
teams_to_share.map { |e| { id: e, permission_level: wp&.include?(e) ? 'shared_write' : 'shared_read' } }
|
2019-08-06 21:17:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def teams_to_unshare
|
|
|
|
existing_shares = @repository.teams_shared_with.pluck(:id)
|
2019-08-29 23:17:38 +08:00
|
|
|
existing_shares - update_params[:share_team_ids]&.map(&:to_i).to_a
|
2019-08-06 21:17:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def teams_to_update
|
2021-02-18 20:55:03 +08:00
|
|
|
return [] if update_params[:permission_changes].blank?
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
teams_to_update = JSON.parse(update_params[:permission_changes]).keys.map(&:to_i).to_a &
|
|
|
|
update_params[:share_team_ids]&.map(&:to_i).to_a
|
|
|
|
wp = update_params[:write_permissions]&.map(&:to_i)
|
2019-08-06 21:17:13 +08:00
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
teams_to_update.map { |e| { id: e, permission_level: wp&.include?(e) ? 'shared_write' : 'shared_read' } }
|
2019-08-06 21:17:13 +08:00
|
|
|
end
|
|
|
|
|
2019-08-13 20:05:55 +08:00
|
|
|
def share_all_params
|
|
|
|
{
|
|
|
|
shared_with_all: params[:select_all_teams].present?,
|
2019-08-29 23:17:38 +08:00
|
|
|
shared_permissions_level: params[:select_all_write_permission].present? ? 'shared_write' : 'shared_read'
|
2019-08-13 20:05:55 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-07-01 17:55:27 +08:00
|
|
|
def log_activity(type_of, team_shared_object)
|
2019-07-16 18:21:20 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
2022-07-01 17:55:27 +08:00
|
|
|
subject: team_shared_object.shared_repository,
|
2022-11-24 22:19:17 +08:00
|
|
|
team: @repository.team,
|
2022-07-01 17:55:27 +08:00
|
|
|
message_items: { repository: team_shared_object.shared_repository.id,
|
|
|
|
team: team_shared_object.team.id,
|
2019-08-09 18:44:41 +08:00
|
|
|
permission_level:
|
|
|
|
Extends::SHARED_INVENTORIES_PL_MAPPINGS[team_repository.permission_level.to_sym] })
|
2019-07-16 18:21:20 +08:00
|
|
|
end
|
2019-07-15 23:00:55 +08:00
|
|
|
end
|