<commit_before>class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper

  before_action :require_login, :except => :contact
  before_action :admin_only, :except => :contact

  private

  def require_login
    unless current_user
      redirect_to login_path
      flash[:danger] = 'Please log in.'
    end
  end

  def correct_user_only
    unless @current_user.privilege == 0
      if @current_user != User.find(params[:id])
          flash[:danger] = 'You are not allowed to access that page.'
          redirect_to current_user
      end
    end
  end

  def correct_initiative_only
    unless @current_user.privilege == 0
      if @current_user.privilege == 2
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
      unless @current_user.initiatives.include?(Initiative.find(params[:id]))
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end

  def correct_initiative_only_on_creation
    unless @current_user.privilege == 0
      if @current_user.privilege == 2
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
      unless @current_user.initiatives.include?(Initiative.find(params[:initiative_id]))
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end

  def correct_meeting_only
    unless @current_user.privilege == 0
      if @current_user.privilege == 2
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
      @current_user.initiatives.each do |init|
        if init.meetings.include?(Meeting.find(params[:id]))
          return
        end
      end
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def service_user_only
    unless @current_user.privilege == 2
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def volunteer_only
    unless @current_user.privilege == 1
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def admin_only
    unless @current_user.privilege == 0
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def service_user?
    @current_user.privilege == 2
  end

  def volunteer?
    @current_user.privilege == 1
  end

  def admin?
    @current_user.privilege == 0
  end

  def is_archived
    if self.is_archived? == true
      if @current_user.privilege > 0
        flash[:danger] = 'Sorry, this page no longer exists.'
        redirect_to :back
        return
      end
      flash[:danger] = 'This page has been archived and is only visible to admins.'
    end
  end

end<commit_msg>Some refactoring for application_controller and comments added.<commit_after>class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper

  # As this is in ApplicationController, all pages on the website require a login EXCEPT the contact page
  before_action :require_login, :except => :contact
  # Admin_Only here specifics that all pages, except contact, require a login to access. Where a service_user or
  #   a volunteer requires access, you must specifically skip_before_action this to allow access.
  before_action :admin_only, :except => :contact

  private

  def require_login
    unless current_user
      redirect_to login_path
      flash[:danger] = 'Please log in.'
    end
  end

  def correct_user_only
    unless admin?
      if @current_user != User.find(params[:id])
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end

  # Ensure that only a user enrolled for the particular initiative can access it
  def correct_initiative_only
    unless admin?
      if service_user?
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      elsif !@current_user.initiatives.include?(Initiative.find(params[:id]))
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end


  def correct_initiative_only_on_creation
    unless admin?
      if service_user?
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      elsif !@current_user.initiatives.include?(Initiative.find(params[:initiative_id]))
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end

  # Ensure that a user enrolled for the initiative belonging to the meeting can access the meeting(s) page
  def correct_meeting_only
    unless admin?
      if service_user?
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      else
        @current_user.initiatives.each do |init|
          if init.meetings.include?(Meeting.find(params[:id]))
            return
          end
        end
        flash[:danger] = 'You are not allowed to access that page.'
        redirect_to current_user
      end
    end
  end

  def service_user_only
    unless service_user?
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def volunteer_only
    unless volunteer?
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def admin_only
    unless admin?
      flash[:danger] = 'You are not allowed to access that page.'
      redirect_to current_user
    end
  end

  def service_user?
    @current_user.privilege == 2
  end

  def volunteer?
    @current_user.privilege == 1
  end

  def admin?
    @current_user.privilege == 0
  end

  def is_archived
    if self.is_archived?
      if @current_user.privilege > 0
        flash[:danger] = 'Sorry, this page no longer exists.'
        redirect_to :back
        return
      end
      flash[:danger] = 'This page has been archived and is only visible to admins.'
    end
  end

end
{'fraction_non_alphanumeric': 0.06866952789699571, 'fraction_numerical': 0.002861230329041488, 'mean_word_length': 2.725281231497928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5995689', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1788, 'n_words': 603}