<commit_before>class Ability
  include CanCan::Ability

  attr_accessor :allow_reg_modifications

  def config
    @config ||= EventConfiguration.singleton
  end

  def reg_closed?
    EventConfiguration.closed? && !allow_reg_modifications
  end

  def artistic_reg_closed?
    reg_closed? || config.artistic_closed?
  end

  def event_sign_up_closed?
    reg_closed? || config.event_sign_up_closed?
  end

  def initialize(user, allow_reg_modifications = false)
    @allow_reg_modifications = allow_reg_modifications

    if user.present?
      define_ability_for_logged_in_user(user)
    end
    # Even Non-Logged-In Users can still:

    can :read, StandardSkillEntry
    can :read, AgeGroupType
    can [:index, :scores], :result
    can [:announcer, :start_list], Competition
    can [:acl, :set_acl, :code, :use_code], :permission
    can :show, PublishedAgeGroupEntry do |entry|
      entry.published_at.present?
    end

    if config.test_mode
      # allow the user to upgrade their account in TEST MODE
      can :test_mode_role, EventConfiguration
      can :fake_complete, Payment
    else
      # disable for all
      cannot :test_mode_role, EventConfiguration
      cannot :fake_complete, Payment
    end
  end

  def set_data_entry_volunteer_abilities(user)
    can :data_entry_menu, :welcome

    can [:read], Competitor
    can :read, Competition

    can :read, Judge, user_id: user.id

    # Freestyle
    can :create, Score
    can [:read, :update, :destroy], Score do |score|
      score.try(:user) == user && !score.competitor.competition.locked?
    end

    can [:create_scores], Competition do |competition|
      !competition.locked?
    end

    # printing forms:
    can [:announcer, :heat_recording, :single_attempt_recording, :two_attempt_recording], Competition

    # data entry
    can :manage, ImportResult do |import_result|
      !import_result.competition.locked?
    end
    cannot [:approve, :approve_heat], ImportResult

    can :manage, TwoAttemptEntry do |two_attempt_entry|
      !two_attempt_entry.competition.locked?
    end

    # High/Long Data Entry
    can :manage, DistanceAttempt
    can :manage, TieBreakAdjustment
  end

  def director_and_unlocked(user, competition)
    !competition.locked? && user.has_role?(:director, competition.try(:event))
  end

  def director_or_competition_admin?(user, competition)
    user.has_role?(:director, competition.try(:event)) || user.has_role?(:competition_admin)
  end

  def set_director_abilities(user)
    set_data_entry_volunteer_abilities(user)

    # Abilities to be able to read data about a competition
    can :read, Competition do |competition|
      user.has_role?(:director, competition.event) || user.has_role?(:competition_admin)
    end

    # SignUp/Competitor management abilities
    can [:sign_ups], Event do |ev|
      user.has_role? :director, ev || user.has_role?(:competition_admin)
    end

    can :sign_ups, EventCategory
    #     #this is the way recommended by rolify...but it must not be called with the class (ie: do not call "can? :results, Event")
    #     can [:read, :results, :sign_ups], Event, id: Event.with_role(:director, user).pluck(:id)

    # Abilities to be able to change data about a competition
    can [:manage, :update_row_order], Competitor do |comp|
      comp.unlocked? && (director_or_competition_admin?(user, comp.competition))
    end

    can :manage, Member do |member|
      comp.unlocked? && (director_or_competition_admin?(user, member.competitor.competition))
    end

    can [:set_sort, :toggle_final_sort, :sort_random, :lock], Competition do |comp|
      comp.unlocked? && (director_or_competition_admin?(user, comp))
    end

    # Volunteer Abilities

    can [:summary, :general_volunteers, :specific_volunteers], Event

    can :manage, DataEntryVolunteer

    # Judging/Scoring Abilities

    can [:read], Score do |score|<commit_msg>fix competition_admin management abilities (exception)<commit_after>class Ability
  include CanCan::Ability

  attr_accessor :allow_reg_modifications

  def config
    @config ||= EventConfiguration.singleton
  end

  def reg_closed?
    EventConfiguration.closed? && !allow_reg_modifications
  end

  def artistic_reg_closed?
    reg_closed? || config.artistic_closed?
  end

  def event_sign_up_closed?
    reg_closed? || config.event_sign_up_closed?
  end

  def initialize(user, allow_reg_modifications = false)
    @allow_reg_modifications = allow_reg_modifications

    if user.present?
      define_ability_for_logged_in_user(user)
    end
    # Even Non-Logged-In Users can still:

    can :read, StandardSkillEntry
    can :read, AgeGroupType
    can [:index, :scores], :result
    can [:announcer, :start_list], Competition
    can [:acl, :set_acl, :code, :use_code], :permission
    can :show, PublishedAgeGroupEntry do |entry|
      entry.published_at.present?
    end

    if config.test_mode
      # allow the user to upgrade their account in TEST MODE
      can :test_mode_role, EventConfiguration
      can :fake_complete, Payment
    else
      # disable for all
      cannot :test_mode_role, EventConfiguration
      cannot :fake_complete, Payment
    end
  end

  def set_data_entry_volunteer_abilities(user)
    can :data_entry_menu, :welcome

    can [:read], Competitor
    can :read, Competition

    can :read, Judge, user_id: user.id

    # Freestyle
    can :create, Score
    can [:read, :update, :destroy], Score do |score|
      score.try(:user) == user && !score.competitor.competition.locked?
    end

    can [:create_scores], Competition do |competition|
      !competition.locked?
    end

    # printing forms:
    can [:announcer, :heat_recording, :single_attempt_recording, :two_attempt_recording], Competition

    # data entry
    can :manage, ImportResult do |import_result|
      !import_result.competition.locked?
    end
    cannot [:approve, :approve_heat], ImportResult

    can :manage, TwoAttemptEntry do |two_attempt_entry|
      !two_attempt_entry.competition.locked?
    end

    # High/Long Data Entry
    can :manage, DistanceAttempt
    can :manage, TieBreakAdjustment
  end

  def director_and_unlocked(user, competition)
    !competition.locked? && user.has_role?(:director, competition.try(:event))
  end

  def director_or_competition_admin?(user, competition)
    user.has_role?(:director, competition.try(:event)) || user.has_role?(:competition_admin)
  end

  def set_director_abilities(user)
    set_data_entry_volunteer_abilities(user)

    # Abilities to be able to read data about a competition
    can :read, Competition do |competition|
      user.has_role?(:director, competition.event) || user.has_role?(:competition_admin)
    end

    # SignUp/Competitor management abilities
    can [:sign_ups], Event do |ev|
      user.has_role? :director, ev || user.has_role?(:competition_admin)
    end

    can :sign_ups, EventCategory
    #     #this is the way recommended by rolify...but it must not be called with the class (ie: do not call "can? :results, Event")
    #     can [:read, :results, :sign_ups], Event, id: Event.with_role(:director, user).pluck(:id)

    # Abilities to be able to change data about a competition
    can [:manage, :update_row_order], Competitor do |comp|
      comp.competition.unlocked? && (director_or_competition_admin?(user, comp.competition))
    end

    can :manage, Member do |member|
      member.competitor.competition.unlocked? && (director_or_competition_admin?(user, member.competitor.competition))
    end

    can [:set_sort, :toggle_final_sort, :sort_random, :lock], Competition do |comp|
      comp.unlocked? && (director_or_competition_admin?(user, comp))
    end

    # Volunteer Abilities

    can [:summary, :general_volunteers, :specific_volunteers], Event

    can :manage, DataEntryVolunteer

    # Judging/Scoring Abilities

    can [:read], Score do |score|
{'fraction_non_alphanumeric': 0.09558263518659559, 'fraction_numerical': 0.0, 'mean_word_length': 3.786755771567436, 'pattern_counts': {'":': 0, '<': 3, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7351760', 'n_tokens_mistral': 2560, 'n_tokens_neox': 2440, 'n_words': 637}