<commit_before># frozen_string_literal: true

# The application controller is the parent for all other controllers.
# It includes methods are relevant across the application, such as permissions
# and login.
require_dependency "#{Rails.root}/lib/errors/authentication_errors"
require_dependency "#{Rails.root}/lib/errors/rescue_development_errors"
require_dependency "#{Rails.root}/lib/errors/rescue_errors"

class ApplicationController < ActionController::Base
  include Errors::RescueDevelopmentErrors if Rails.env.development? || Rails.env.test?
  include Errors::RescueErrors
  include Errors::AuthenticationErrors
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  force_ssl if: :ssl_configured?

  before_action :check_for_expired_oauth_credentials
  before_action :check_for_unsupported_browser
  before_action :check_onboarded
  before_action :set_locale
  before_action :set_paper_trail_whodunnit

  def after_sign_out_path_for(_resource_or_scope)
    '/'
  end

  def after_sign_in_path_for(_resource_or_scope)
    request.env['omniauth.origin'] || '/'
  end

  def check_onboarded
    return unless current_user
    return if Features.disable_onboarding? || current_user.onboarded
    full_path = request.fullpath
    non_redirect_paths = [onboarding_path,
                          onboard_path,
                          new_user_session_path,
                          destroy_user_session_path,
                          true_destroy_user_session_path]
    return if non_redirect_paths.any? { |path| full_path.starts_with? path }
    redirect_to onboarding_path(return_to: full_path)
  end

  def require_signed_in
    raise NotSignedInError unless user_signed_in?
  end

  def require_permissions
    require_signed_in
    course = Course.find_by(slug: params[:id])
    raise NotPermittedError unless current_user.can_edit? course
  end

  def require_admin_permissions
    require_signed_in
    raise NotAdminError unless current_user.admin?
  end

  def require_super_admin_permissions
    require_signed_in
    exception = NotAdminError.new('Only super administrators may do that.')
    raise exception unless current_user.super_admin?
  end

  def require_participating_user
    require_signed_in
    course = Course.find_by(slug: params[:id])
    # Course roles for non-students are greater than STUDENT_ROLE.
    # Non-participating users have the VISITOR_ROLE, which is below STUDENT_ROLE.
    return if current_user.role(course) >= CoursesUsers::Roles::STUDENT_ROLE
    raise ParticipatingUserError
  end<commit_msg>Raise error when course is not found in `require_permissions`<commit_after># frozen_string_literal: true

# The application controller is the parent for all other controllers.
# It includes methods are relevant across the application, such as permissions
# and login.
require_dependency "#{Rails.root}/lib/errors/authentication_errors"
require_dependency "#{Rails.root}/lib/errors/rescue_development_errors"
require_dependency "#{Rails.root}/lib/errors/rescue_errors"

class ApplicationController < ActionController::Base
  include Errors::RescueDevelopmentErrors if Rails.env.development? || Rails.env.test?
  include Errors::RescueErrors
  include Errors::AuthenticationErrors
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  force_ssl if: :ssl_configured?

  before_action :check_for_expired_oauth_credentials
  before_action :check_for_unsupported_browser
  before_action :check_onboarded
  before_action :set_locale
  before_action :set_paper_trail_whodunnit

  def after_sign_out_path_for(_resource_or_scope)
    '/'
  end

  def after_sign_in_path_for(_resource_or_scope)
    request.env['omniauth.origin'] || '/'
  end

  def check_onboarded
    return unless current_user
    return if Features.disable_onboarding? || current_user.onboarded
    full_path = request.fullpath
    non_redirect_paths = [onboarding_path,
                          onboard_path,
                          new_user_session_path,
                          destroy_user_session_path,
                          true_destroy_user_session_path]
    return if non_redirect_paths.any? { |path| full_path.starts_with? path }
    redirect_to onboarding_path(return_to: full_path)
  end

  def require_signed_in
    raise NotSignedInError unless user_signed_in?
  end

  def require_permissions
    require_signed_in
    course = Course.find_by!(slug: params[:id])
    raise NotPermittedError unless current_user.can_edit? course
  end

  def require_admin_permissions
    require_signed_in
    raise NotAdminError unless current_user.admin?
  end

  def require_super_admin_permissions
    require_signed_in
    exception = NotAdminError.new('Only super administrators may do that.')
    raise exception unless current_user.super_admin?
  end

  def require_participating_user
    require_signed_in
    course = Course.find_by(slug: params[:id])
    # Course roles for non-students are greater than STUDENT_ROLE.
    # Non-participating users have the VISITOR_ROLE, which is below STUDENT_ROLE.
    return if current_user.role(course) >= CoursesUsers::Roles::STUDENT_ROLE
    raise ParticipatingUserError
  end
{'fraction_non_alphanumeric': 0.06065727699530517, 'fraction_numerical': 0.0, 'mean_word_length': 4.2164544564152795, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6707181', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1564, 'n_words': 371}