<commit_before>class SshAccess

  PERMISSIONS = %w{
    read
    write
  }

  PERMISSION_MAP = {
    everyone: {
      'public_r' => %w{read},
      'public_rw' => %w{read write},
      'private_r' => %w{},
      'private_rw' => %w{},
    },
    permission: {
      all: {
        'public_r' => %w{},
        'public_rw' => %w{},
        'private_r' => %w{read},
        'private_rw' => %w{read write},
      },
      owner: [],
      editor: []
    }
  }

  class << self

    def determine_permission(requested_permission, permission, repository)
      if repository.public_rw?
        true
      elsif repository.public_r? && requested_permission == 'read'
        true
      elsif PERMISSION_MAP[:everyone][repository.access].include?(requested_permission)
        true
      elsif permission
        return true if PERMISSION_MAP[:permission][:all][repository.access].
          include?(requested_permission)

        role = permission.role.to_sym
        allowed_permissions = PERMISSION_MAP[:permission][role]
        allowed_permissions.include?(requested_permission)
      else
        false
      end
    end

    def extract_permission_params(params, repository)
      key_field = params[:key_id].sub("key-", "")
      requested_permission = params[:permission]
      user = User.joins(:keys).
        where(keys: {id: key_field}).first
      permission = nil
      permission = repository.highest_permission(user) if user
      [requested_permission, permission]
    end

  end<commit_msg>extract permissions-check (git/ssh) into methods<commit_after>class SshAccess

  PERMISSIONS = %w{
    read
    write
  }

  PERMISSION_MAP = {
    everyone: {
      'public_r' => %w{read},
      'public_rw' => %w{read write},
      'private_r' => %w{},
      'private_rw' => %w{},
    },
    permission: {
      all: {
        'public_r' => %w{},
        'public_rw' => %w{},
        'private_r' => %w{read},
        'private_rw' => %w{read write},
      },
      owner: [],
      editor: []
    }
  }

  class << self

    def determine_permission(requested_permission, permission, repository)
      if repository.public_rw?
        true
      elsif repository.public_r? && requested_permission == 'read'
        true
      elsif allowed_for_everyone?(requested_permission, repository)
        true
      elsif permission
        allowed_for?(requested_permission, repository, through: permission)
      else
        false
      end
    end

    def allowed_for?(requested_permission, repository, through: nil)
      if through
        return true if included_in?(:permission, :all, repository, requested_permission)

        included_in_role?(through.role.to_sym, requested_permission)
      else
        false
      end
    end

    def allowed_for_everyone?(requested_permission, repository)
      included_in?(:everyone, repository, requested_permission)
    end

    def included_in?(*groups, repository, requested_permission)
      in_map = groups.reduce(PERMISSION_MAP) { |map, group| map[group] }
      in_map[repository.access].include?(requested_permission)
    end

    def included_in_role?(role, requested_permission)
      PERMISSION_MAP[:permission][role].include?(requested_permission)
    end

    def extract_permission_params(params, repository)
      key_field = params[:key_id].sub("key-", "")
      requested_permission = params[:permission]
      user = User.joins(:keys).
        where(keys: {id: key_field}).first
      permission = nil
      permission = repository.highest_permission(user) if user
      [requested_permission, permission]
    end

  end
{'fraction_non_alphanumeric': 0.11579241071428571, 'fraction_numerical': 0.0, 'mean_word_length': 2.7304890738813734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '6566276', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1064, 'n_words': 211}