<commit_before># frozen_string_literal: true

#
# == Ability Class
#
class Ability
  include CanCan::Ability
  prepend Draper::CanCanCan

  def initialize(user)
    user ||= User.new # visitor user (not logged in)
    @user = user
    set_optional_modules

    alias_action :create, :read, :update, :destroy, to: :crud

    if @user.super_administrator?
      super_administrator_privilege
    elsif @user.administrator?
      administrator_privilege
    elsif @user.subscriber?
      subscriber_privilege
    else
      visitor_privilege
    end
  end

  private

  def set_optional_modules
    optional_modules = OptionalModule.all
    optional_modules.find_each do |optional_module|
      instance_variable_set("@#{optional_module.name.underscore.singularize}_module", optional_module)
    end
  end

  def super_administrator_privilege
    can :manage, :all
    cannot [:update, :destroy], User, role: { name: 'super_administrator' }
    cannot [:unlink], User
    can :manage, User, id: @user.id
    optional_modules_check
    can :manage, StringBox
    super_admin_batch_actions # Batch actions
  end

  def administrator_privilege
    can :read, :all
    can [:crud], [Post]
    can :update, [Setting]

    # Menu / Category
    cannot :manage, [Menu, Category], optional_module: { enabled: false }
    can [:update], [Menu, Category], optional_module: { enabled: true }
    can [:update], [Menu, Category], optional_module: nil

    can [:read, :destroy, :update], User, role_name: %w( subscriber )
    cannot [:create, :unlink], User
    cannot [:update, :destroy], User, role: { name: %w( administrator ) }
    cannot :manage, User, role: { name: %w( super_administrator ) }
    can :manage, User, id: @user.id
    can [:read, :update, :destroy], Picture
    cannot :manage, OptionalModule
    cannot [:read, :update, :destroy], [About, LegalNotice]
    can :manage, [About, LegalNotice], user_id: @user.id
    can [:read, :update], StringBox

    admin_batch_actions # Batch actions
    optional_modules_check # Optional modules
  end

  def subscriber_privilege
    cannot_manage_optional_modules
    rss_module
    comment_frontend
    newsletter_frontend
    mailing_frontend
    can :mapbox_popup, Contact
    can [:update, :read, :destroy, :unlink], User, id: @user.id
    cannot [:create], User
    can [:create, :read, :destroy], Comment, user_id: @user.id
    cannot :destroy, Comment, user_id: nil<commit_msg>AA Connection/position :: Fix bug while ordering posts as administrator<commit_after># frozen_string_literal: true

#
# == Ability Class
#
class Ability
  include CanCan::Ability
  prepend Draper::CanCanCan

  def initialize(user)
    user ||= User.new # visitor user (not logged in)
    @user = user
    set_optional_modules

    alias_action :create, :read, :update, :destroy, to: :crud

    if @user.super_administrator?
      super_administrator_privilege
    elsif @user.administrator?
      administrator_privilege
    elsif @user.subscriber?
      subscriber_privilege
    else
      visitor_privilege
    end
  end

  private

  def set_optional_modules
    optional_modules = OptionalModule.all
    optional_modules.find_each do |optional_module|
      instance_variable_set("@#{optional_module.name.underscore.singularize}_module", optional_module)
    end
  end

  def super_administrator_privilege
    can :manage, :all
    cannot [:update, :destroy], User, role: { name: 'super_administrator' }
    cannot [:unlink], User
    can :manage, User, id: @user.id
    optional_modules_check
    can :manage, StringBox
    super_admin_batch_actions # Batch actions
  end

  def administrator_privilege
    can :read, :all
    can [:crud], [Post]
    can :update, [Setting]

    # Menu / Category
    cannot :manage, [Menu, Category], optional_module: { enabled: false }
    can [:update], [Menu, Category], optional_module: { enabled: true }
    can [:update], [Menu, Category], optional_module: nil

    can [:read, :destroy, :update], User, role_name: %w( subscriber )
    cannot [:create, :unlink], User
    cannot [:update, :destroy], User, role: { name: %w( administrator ) }
    cannot :manage, User, role: { name: %w( super_administrator ) }
    can :manage, User, id: @user.id
    can [:read, :update, :destroy], Picture
    cannot :manage, OptionalModule
    cannot [:read, :update, :destroy], [About, LegalNotice]
    can :manage, [About, LegalNotice], user_id: @user.id
    can [:read, :update], StringBox
    can :manage, Connection

    admin_batch_actions # Batch actions
    optional_modules_check # Optional modules
  end

  def subscriber_privilege
    cannot_manage_optional_modules
    rss_module
    comment_frontend
    newsletter_frontend
    mailing_frontend
    can :mapbox_popup, Contact
    can [:update, :read, :destroy, :unlink], User, id: @user.id
    cannot [:create], User
    can [:create, :read, :destroy], Comment, user_id: @user.id
    cannot :destroy, Comment, user_id: nil
{'fraction_non_alphanumeric': 0.10578478964401294, 'fraction_numerical': 0.0, 'mean_word_length': 3.7639691714836223, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7557625', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1542, 'n_words': 454}