Refactor inventory limit checks [SCI-4434]

This commit is contained in:
Oleksii Kriuchykhin 2020-03-05 15:26:25 +01:00
parent f201826eb8
commit 02817a5a1e
2 changed files with 14 additions and 9 deletions

View file

@ -50,6 +50,18 @@ class Repository < ApplicationRecord
.where.not(id: accessible_by_teams(team.id).select(:id)).distinct
}
def self.within_global_limits?
return true unless Rails.configuration.x.global_repositories_limit.positive?
count < Rails.configuration.x.global_repositories_limit
end
def self.within_team_limits?(team)
return true unless Rails.configuration.x.team_repositories_limit.positive?
team.repositories.count < Rails.configuration.x.team_repositories_limit
end
def self.search(
user,
query = nil,

View file

@ -48,15 +48,8 @@ Canaid::Permissions.register_for(Team) do
# repository: create, copy
can :create_repositories do |user, team|
within_limits = true
if Rails.configuration.x.global_repositories_limit.positive?
within_limits = Repository.count < Rails.configuration.x.global_repositories_limit
end
if within_limits && Rails.configuration.x.team_repositories_limit.positive?
within_limits = team.repositories.count < Rails.configuration.x.team_repositories_limit
end
within_limits = Repository.within_global_limits?
within_limits = Repository.within_team_limits?(team) if within_limits
within_limits && user.is_admin_of_team?(team)
end