class Ecs::BaseController < ApplicationController

  before_action :require_login, :check_auth
  before_action :check_user_permission!

  helper_method :current_user, :current_school

  private

  # --- 每个子类controller可能有不同的实现，查看时需要注意 ---
  def current_year
    @_current_year ||= EcYear.find(params[:ec_year_id])
  end

  def current_major_school
    @_current_major_school ||= current_year.ec_major_school
  end

  def current_school
    @_current_school ||= current_major_school.school
  end

  def major_or_course_manager?
    !current_user.admin? && !current_school.manager?(current_user)
  end

  def check_user_permission!
    return if current_user.admin? || current_school.manage_permission?(current_user)

    render_forbidden
  end

  def check_manager_permission!
    return if current_user.admin? || current_school.manager?(current_user)

    render_forbidden
  end

  def check_major_manager_permission!
    return if current_user.admin? || current_school.manager?(current_user)
    return if current_major_school.manager?(current_user)

    render_forbidden
  end

  def paginate(objs)
    page     = params[:page].to_i <= 0 ? 1 : params[:page].to_i
    per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : 20

    Kaminari.paginate_array(objs).page(page).per(per_page)
  end
end