<commit_before># frozen_string_literal: true
require File.expand_path '../application_policy.rb', __FILE__

# Policy for Package
class PackagePolicy < ApplicationPolicy
  def permitted_attributes
    return Permissions::Admin.new(record).attributes if user.admin?
    return Permissions::Reseller.new(record).attributes if user.reseller?
    Permissions::User.new(record).attributes
  end

  # Checks if current user is allowed to show the record
  #
  # @return [Boolean]
  # rubocop:disable Metrics/AbcSize
  def show?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return true if record.owner == user
    return true if user.packages.map(&:id).include?(record.id)
    false
  end

  # Checks if current user is allowed to update the record
  #
  # @return [Boolean]
  def update?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return true if Package.all(user_id: user.id).map(&:id).include?(record.id)
    false
  end

  # Checks if current user is allowed to delete the record
  #
  # @return [Boolean]
  def destroy?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return false unless record.users.empty?
    record.user_id == user.id
  end

  # Checks if current user is allowed to create a record with given params
  #
  # @return [Boolean]
  def create_with?(params)
    return true if user.admin?
    check_create_params(params)
  end

  # Checks if current user is allowed to update the record with given params
  #
  # @return [Boolean]
  def update_with?(params)
    return true if user.admin?
    check_update_params(params)
  end

  # Scope for Package
  class Scope < Scope
    def resolve
      return(scope.all) if user.admin?
      packages
    end

    private

    def packages
      result = scope.all(user_id: user.id)
      result
    end
  end

  class Permissions < ApplicationPermissions
    # include :user relation
    class Admin < self
    end

    class Reseller < Admin
    end

    class User < Reseller
    end
  end

  private

  # @return [Boolean]
  def quotacheck
    return false unless user.reseller?
    used = Package.all(user_id: user.id).size
    available = user.packages.map(&:quota_custom_packages).reduce(0, :+)
    return true if used < available
    false
  end

  def check_create_params(params)
    return false unless user.reseller?
    return false unless quotacheck
    return true if params.key?(:user_id) && params[:user_id] == user.id
    false
  end

  # check amount of all assigned customers to a package before permission
  def check_update_params(params)
    return false unless user.reseller?
    params.each_pair do |k, v|
      next unless k.to_s =~ %r{^quota_}
      remaining = check_quota_prop(k)
      mult = record.users.size
      return false unless remaining >= ((v - record.send(k)) * mult)
    end
    true
  end

  def check_quota_prop(key)
    # call helper methods from quota_helper.rb
    count = reseller_allocated_quota(user, key)
    user.packages.map(&key).reduce(0, :+) - count
  end
end<commit_msg>prevent exception by using #to_i<commit_after># frozen_string_literal: true
require File.expand_path '../application_policy.rb', __FILE__

# Policy for Package
class PackagePolicy < ApplicationPolicy
  def permitted_attributes
    return Permissions::Admin.new(record).attributes if user.admin?
    return Permissions::Reseller.new(record).attributes if user.reseller?
    Permissions::User.new(record).attributes
  end

  # Checks if current user is allowed to show the record
  #
  # @return [Boolean]
  # rubocop:disable Metrics/AbcSize
  def show?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return true if record.owner == user
    return true if user.packages.map(&:id).include?(record.id)
    false
  end

  # Checks if current user is allowed to update the record
  #
  # @return [Boolean]
  def update?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return true if Package.all(user_id: user.id).map(&:id).include?(record.id)
    false
  end

  # Checks if current user is allowed to delete the record
  #
  # @return [Boolean]
  def destroy?
    if record.is_a?(DataMapper::Resource)
      return false if record.destroyed?
    end
    return true if user.admin?
    return false unless record.users.empty?
    record.user_id == user.id
  end

  # Checks if current user is allowed to create a record with given params
  #
  # @return [Boolean]
  def create_with?(params)
    return true if user.admin?
    check_create_params(params)
  end

  # Checks if current user is allowed to update the record with given params
  #
  # @return [Boolean]
  def update_with?(params)
    return true if user.admin?
    check_update_params(params)
  end

  # Scope for Package
  class Scope < Scope
    def resolve
      return(scope.all) if user.admin?
      packages
    end

    private

    def packages
      result = scope.all(user_id: user.id)
      result
    end
  end

  class Permissions < ApplicationPermissions
    # include :user relation
    class Admin < self
    end

    class Reseller < Admin
    end

    class User < Reseller
    end
  end

  private

  # @return [Boolean]
  def quotacheck
    return false unless user.reseller?
    used = Package.all(user_id: user.id).size
    available = user.packages.map(&:quota_custom_packages).reduce(0, :+)
    return true if used < available
    false
  end

  def check_create_params(params)
    return false unless user.reseller?
    return false unless quotacheck
    return true if params.key?(:user_id) && params[:user_id] == user.id
    false
  end

  # check amount of all assigned customers to a package before permission
  def check_update_params(params)
    return false unless user.reseller?
    params.each_pair do |k, v|
      next unless k.to_s =~ %r{^quota_}
      remaining = check_quota_prop(k)
      mult = record.users.size
      return false unless remaining >= ((v - record.send(k).to_i) * mult)
    end
    true
  end

  def check_quota_prop(key)
    # call helper methods from quota_helper.rb
    count = reseller_allocated_quota(user, key)
    user.packages.map(&key).reduce(0, :+) - count
  end
end
{'fraction_non_alphanumeric': 0.0836172189532363, 'fraction_numerical': 0.0006193868070610096, 'mean_word_length': 3.0673803526448364, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6413184', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1969, 'n_words': 615}