<commit_before>class SectionsController < ApplicationController
  before_action :authorize_and_assign_section, only: :show
  before_action :authenticate_districtwide_access!, only: :index # Extra authentication layer

  def index
    #just setting this to all courses for now
    #since we are only testing with district wide admins
    @educator_courses = Course.all.order(:course_number)
  end

  def show
    students = authorized { @current_section.students } # extra layer while transitioning K8 to use sections
    students_json = serialize_students(students.map(&:id))
    section = serialize_section(@current_section)

    @serialized_data = {
      students: students_json,
      educators: @current_section.educators,
      section: section,
      sections: current_educator.allowed_sections,
      current_educator: current_educator,
    }
    render 'shared/serialized_data'
  end

  private
  def authorize_and_assign_section
    # Extra layer while transitioning K8 to use sections
    if !current_educator.districtwide_access && !current_educator.school.is_high_school?
      return redirect_to homepage_path_for_role(current_educator)
    end

    requested_section = Section.find(params[:id])
    if current_educator.is_authorized_for_section(requested_section)
      @current_section = requested_section
    else
      redirect_to homepage_path_for_role(current_educator)
    end
  rescue ActiveRecord::RecordNotFound     # Params don't match an actual section
    redirect_to homepage_path_for_role(current_educator)
  end

  def authenticate_districtwide_access!
    unless current_educator.districtwide_access
      redirect_to not_authorized_path
    end
  end

  def serialize_students(student_ids)
    Student.all
      .joins(:student_section_assignments)
      .where(id: student_ids)
      .select('students.*, student_section_assignments.grade_numeric')
      .as_json(methods: [:event_notes, :most_recent_school_year_discipline_incidents_count, :most_recent_school_year_absences_count, :most_recent_school_year_tardies_count])
  end

  def serialize_section(section)
    section.as_json(methods: [:course_number, :course_description])
  end
end
<commit_msg>Patch section authorization bug for HS teachers<commit_after>class SectionsController < ApplicationController
  before_action :authorize_and_assign_section, only: :show
  before_action :authenticate_districtwide_access!, only: :index # Extra authentication layer

  def index
    #just setting this to all courses for now
    #since we are only testing with district wide admins
    @educator_courses = Course.all.order(:course_number)
  end

  def show
    students = authorized { @current_section.students } # extra layer while transitioning K8 to use sections
    students_json = serialize_students(students.map(&:id))
    section = serialize_section(@current_section)

    @serialized_data = {
      students: students_json,
      educators: @current_section.educators,
      section: section,
      sections: current_educator.allowed_sections,
      current_educator: current_educator,
    }
    render 'shared/serialized_data'
  end

  private
  def authorize_and_assign_section
    if !can_view_section_page? # Extra layer while transitioning K8 to use sections
      return redirect_to homepage_path_for_role(current_educator)
    end

    requested_section = Section.find(params[:id])
    if current_educator.is_authorized_for_section(requested_section)
      @current_section = requested_section
    else
      redirect_to homepage_path_for_role(current_educator)
    end
  rescue ActiveRecord::RecordNotFound     # Params don't match an actual section
    redirect_to homepage_path_for_role(current_educator)
  end

  def can_view_section_page?
    current_educator.districtwide_access || current_educator.school.is_high_school?
  end

  def authenticate_districtwide_access!
    unless current_educator.districtwide_access
      redirect_to not_authorized_path
    end
  end

  def serialize_students(student_ids)
    Student.all
      .joins(:student_section_assignments)
      .where(id: student_ids)
      .select('students.*, student_section_assignments.grade_numeric')
      .as_json(methods: [:event_notes, :most_recent_school_year_discipline_incidents_count, :most_recent_school_year_absences_count, :most_recent_school_year_tardies_count])
  end

  def serialize_section(section)
    section.as_json(methods: [:course_number, :course_description])
  end
end

{'fraction_non_alphanumeric': 0.06205197132616488, 'fraction_numerical': 0.0008960573476702509, 'mean_word_length': 4.768733850129199, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1442530', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1297, 'n_words': 280}