diff --git a/app/controllers/team_repositories_controller.rb b/app/controllers/team_repositories_controller.rb index 9606d9e80..64ada94c7 100644 --- a/app/controllers/team_repositories_controller.rb +++ b/app/controllers/team_repositories_controller.rb @@ -2,23 +2,7 @@ class TeamRepositoriesController < ApplicationController before_action :load_vars - before_action :check_sharing_permissions, only: %i(create destroy) - - # POST :team_id/repositories/:repository_id/team_repositories - def create - team_repository = TeamRepository.new(repository: @repository, - team_id: create_params[:target_team_id], - permission_level: create_params[:permission_level]) - - if team_repository.save - log_activity(:share_inventory, team_repository) - - render json: { team_repository: team_repository }, status: :ok - else - render json: { team_repository: { message: 'not saved!', errors: team_repository.errors } }, - status: :unprocessable_entity - end - end + before_action :check_sharing_permissions # DELETE :team_id/repositories/:repository_id/team_repositories/:id def destroy @@ -29,21 +13,23 @@ class TeamRepositoriesController < ApplicationController team_repository.destroy render json: {}, status: :no_content else - render json: { message: 'Can\'t find sharing relation for destroy' }, status: :unprocessable_entity + render json: { message: I18n.t('repositories.multiple_share_service.nothing_to_delete') }, + status: :unprocessable_entity end end - # POST :team_id/repositories/:repository_id/multiple_update - def multiple_update - service_call = Repositories::MultipleShareUpdateService.call(repository_id: @repository.id, - user_id: current_user.id, - team_id: current_team.id, + # POST :team_id/repositories/:repository_id/update + def update + service_call = Repositories::MultipleShareUpdateService.call(repository: @repository, + user: current_user, + team: current_team, team_ids_for_share: teams_to_share, team_ids_for_unshare: teams_to_unshare, team_ids_for_update: teams_to_update, **share_all_params) if service_call.succeed? - render json: { warnings: service_call.warnings.join(', '), status: @repository.i_shared?(current_team) }, status: :ok + render json: { warnings: service_call.warnings.join(', '), status: @repository.i_shared?(current_team) }, + status: :ok else render json: { errors: service_call.errors.map { |_, v| v }.join(', ') }, status: :unprocessable_entity end @@ -52,7 +38,7 @@ class TeamRepositoriesController < ApplicationController private def load_vars - @repository = Repository.find_by_id(params[:repository_id]) + @repository = current_team.repositories.find_by_id(params[:repository_id]) render_404 unless @repository end @@ -65,7 +51,7 @@ class TeamRepositoriesController < ApplicationController params.permit(:team_id, :id) end - def multiple_update_params + def update_params params.permit(:permission_changes, share_team_ids: [], write_permissions: []) end @@ -75,29 +61,29 @@ class TeamRepositoriesController < ApplicationController def teams_to_share existing_shares = @repository.teams_shared_with.pluck(:id) - teams_to_share = multiple_update_params[:share_team_ids]&.map(&:to_i).to_a - existing_shares - wp = multiple_update_params[:write_permissions]&.map(&:to_i) + teams_to_share = update_params[:share_team_ids]&.map(&:to_i).to_a - existing_shares + wp = update_params[:write_permissions]&.map(&:to_i) - teams_to_share.map { |e| { id: e, permission_level: wp&.include?(e) ? 'write' : 'read' } } + teams_to_share.map { |e| { id: e, permission_level: wp&.include?(e) ? 'shared_write' : 'shared_read' } } end def teams_to_unshare existing_shares = @repository.teams_shared_with.pluck(:id) - existing_shares - multiple_update_params[:share_team_ids]&.map(&:to_i).to_a + existing_shares - update_params[:share_team_ids]&.map(&:to_i).to_a end def teams_to_update - teams_to_update = JSON.parse(multiple_update_params[:permission_changes]).keys.map(&:to_i).to_a & - multiple_update_params[:share_team_ids]&.map(&:to_i).to_a - wp = multiple_update_params[:write_permissions]&.map(&:to_i) + 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) - teams_to_update.map { |e| { id: e, permission_level: wp&.include?(e) ? 'write' : 'read' } } + teams_to_update.map { |e| { id: e, permission_level: wp&.include?(e) ? 'shared_write' : 'shared_read' } } end def share_all_params { shared_with_all: params[:select_all_teams].present?, - shared_permissions_level: params[:select_all_write_permission].present? ? 'write' : 'read' + shared_permissions_level: params[:select_all_write_permission].present? ? 'shared_write' : 'shared_read' } end diff --git a/app/models/repository.rb b/app/models/repository.rb index bd6f17c4d..de039f809 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -34,7 +34,12 @@ class Repository < ApplicationRecord left_outer_joins(:team_repositories) .where('repositories.team_id IN (?) '\ 'OR team_repositories.team_id IN (?) '\ - 'OR repositories.shared = true', teams, teams) + 'OR repositories.permission_level = ? '\ + 'OR repositories.permission_level = ? ', + teams, + teams, + Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_read], + Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_write]) .distinct } @@ -87,25 +92,25 @@ class Repository < ApplicationRecord end def shared_with_anybody? - (shared? || team_repositories.any?) + (!not_shared? || team_repositories.any?) end def shared_with?(team) return false if self.team == team - shared? || private_shared_with?(team) + !not_shared? || private_shared_with?(team) end def shared_with_write?(team) return false if self.team == team - shared? && write? || private_shared_with_write?(team) + shared_write? || private_shared_with_write?(team) end def shared_with_read?(team) return false if self.team == team - shared? && read? || team_repositories.where(team: team, permission_level: :read).any? + shared_read? || team_repositories.where(team: team, permission_level: :shared_read).any? end def private_shared_with?(team) @@ -113,7 +118,7 @@ class Repository < ApplicationRecord end def private_shared_with_write?(team) - team_repositories.where(team: team, permission_level: :write).any? + team_repositories.where(team: team, permission_level: :shared_write).any? end def self.viewable_by_user(_user, teams) diff --git a/app/models/team_repository.rb b/app/models/team_repository.rb index e0caea7e2..cc551160b 100644 --- a/app/models/team_repository.rb +++ b/app/models/team_repository.rb @@ -8,5 +8,11 @@ class TeamRepository < ApplicationRecord validates :permission_level, presence: true validates :repository, uniqueness: { scope: :team_id } - validates :team_id, inclusion: { in: proc { |object| Team.pluck(:id) - [object.repository&.team&.id] } } + validate :team_cannot_be_the_same + + private + + def team_cannot_be_the_same + errors.add(:team_id, :same_team) if repository&.team_id == team_id + end end diff --git a/app/services/repositories/multiple_share_update_service.rb b/app/services/repositories/multiple_share_update_service.rb index c691bf9ed..9599e9b97 100644 --- a/app/services/repositories/multiple_share_update_service.rb +++ b/app/services/repositories/multiple_share_update_service.rb @@ -7,17 +7,17 @@ module Repositories attr_reader :repository, :user, :warnings, :errors - def initialize(repository_id:, - user_id:, - team_id:, + def initialize(repository:, + user:, + team:, team_ids_for_share: [], team_ids_for_unshare: [], team_ids_for_update: [], shared_with_all: nil, shared_permissions_level: nil) - @repository = Repository.find_by_id repository_id - @user = User.find_by_id user_id - @team = Team.find_by_id team_id + @repository = repository + @user = user + @team = team @team_ids_for_share = team_ids_for_share @team_ids_for_unshare = team_ids_for_unshare @team_ids_for_update = team_ids_for_update @@ -31,12 +31,10 @@ module Repositories return self unless valid? if !@shared_with_all.nil? && !@shared_permission_level.nil? - @repository.shared = @shared_with_all - @repository.permission_level = @shared_permission_level - + old_permission_level = @repository.permission_level + @repository.permission_level = @shared_with_all ? @shared_permission_level : :not_shared if @repository.changed? - change_type = @repository.changes.key?('shared') ? 'share' : 'update_permission_level' - log_activity_share_all(change_type, @repository) if @repository.save + log_activity_share_all(@repository.permission_level, old_permission_level, @repository) if @repository.save end end @@ -93,18 +91,11 @@ module Repositories 'user': @user, 'team': @team } .map do |key, value| - "Can't find #{key.capitalize}" if value.nil? + I18n.t('repositories.multiple_share_service.invalid_arguments', key: key.capitalize) if value.nil? end.compact return false end - - if can_share_repository?(@user, @repository) - true - else - @errors[:user_without_permissions] = - ['You are not allowed to share this repository'] - false - end + true end def log_activity(type_of, team_repository) @@ -119,11 +110,16 @@ module Repositories Extends::SHARED_INVENTORIES_PL_MAPPINGS[team_repository.permission_level.to_sym] }) end - def log_activity_share_all(change_type, repository) - type = if change_type == 'share' - @repository.shared ? :share_inventory_with_all : :unshare_inventory_with_all - else - :update_share_with_all_permission_level + def log_activity_share_all(permission_level, old_permission_level, repository) + type = case permission_level.to_sym + when :shared_read, :shared_write + if old_permission_level.to_sym == :not_shared + :share_inventory_with_all + else + :update_share_with_all_permission_level + end + when :not_shared + :unshare_inventory_with_all end Activities::CreateActivityService diff --git a/app/views/repositories/_share_repository_modal.html.erb b/app/views/repositories/_share_repository_modal.html.erb index 5f0bcdd93..5f7d3141a 100644 --- a/app/views/repositories/_share_repository_modal.html.erb +++ b/app/views/repositories/_share_repository_modal.html.erb @@ -1,6 +1,6 @@