education/classroom

View on GitHub
app/controllers/concerns/organization_authorization.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module OrganizationAuthorization
  extend ActiveSupport::Concern

  included do
    before_action :set_organization, :authorize_organization_access
  end

  def authorize_organization_access
    return if @organization.users.include?(current_user)

    if github_organization.admin?(current_user.github_user.login(use_cache: false))
      @organization.users << current_user
    else
      not_found
    end
  end

  private

  def github_organization
    @github_organization ||= GitHubOrganization.new(current_user.github_client, @organization.github_id)
  end

  def set_organization
    return @organization if defined?(@organization)

    organization_id = params[:organization_id] || params[:id]
    @organization = Organization.find_by!(slug: organization_id)
  end
end