<commit_before>require 'cancan'
module Spree
  module Core
    module ControllerHelpers
      def self.included(receiver)
        receiver.send :layout, :get_layout
        receiver.send :before_filter, 'set_user_language'

        receiver.send :helper_method, 'title'
        receiver.send :helper_method, 'title='
        receiver.send :helper_method, 'accurate_title'
        receiver.send :helper_method, 'get_taxonomies'
        receiver.send :helper_method, 'current_order'
        receiver.send :helper_method, 'spree_current_user'
        receiver.send :include, SslRequirement
        receiver.send :include, Spree::Core::CurrentOrder

        receiver.rescue_from CanCan::AccessDenied do |exception|
          return unauthorized
        end
      end

      def access_forbidden
        render :text => 'Access Forbidden', :layout => true, :status => 401
      end

      # can be used in views as well as controllers.
      # e.g. <% title = 'This is a custom title for this view' %>
      attr_writer :title

      def title
        title_string = @title.present? ? @title : accurate_title
        if title_string.present?
          if Spree::Config[:always_put_site_name_in_title]
            [default_title, title_string].join(' - ')
          else
            title_string
          end
        else
          default_title
        end
      end

      # To be overriden by authentication extensions
      def spree_current_user
        nil
      end

      protected

      # Needs to be overriden so that we use Spree's Ability rather than anyone else's.
      def current_ability
        @current_ability ||= Spree::Ability.new(spree_current_user)
      end

      def store_location
        # disallow return to login, logout, signup pages
        disallowed_urls = [spree_signup_path, spree_login_path, spree_logout_path]
        disallowed_urls.map!{ |url| url[/\/\w+$/] }
        unless disallowed_urls.include?(request.fullpath)
          session['user_return_to'] = request.fullpath.gsub('//', '/')
        end
      end

      # Redirect as appropriate when an access request fails.  The default action is to redirect to the login screen.
      # Override this method in your controllers if you want to have special behavior in case the user is not authorized
      # to access the requested action.  For example, a popup window might simply close itself.
      def unauthorized
        respond_to do |format|
          format.html do
            if spree_current_user
              flash.now[:error] = t(:authorization_failure)
              render 'spree/shared/unauthorized', :layout => '/spree/layouts/spree_application', :status => 401
            else
              store_location
              url = respond_to?(:spree_login_path) ? spree_login_path : root_path
              redirect_to url
            end<commit_msg>Add authentication routes to disallowed_routes in store_location, only if they exist<commit_after>require 'cancan'
module Spree
  module Core
    module ControllerHelpers
      def self.included(receiver)
        receiver.send :layout, :get_layout
        receiver.send :before_filter, 'set_user_language'

        receiver.send :helper_method, 'title'
        receiver.send :helper_method, 'title='
        receiver.send :helper_method, 'accurate_title'
        receiver.send :helper_method, 'get_taxonomies'
        receiver.send :helper_method, 'current_order'
        receiver.send :helper_method, 'spree_current_user'
        receiver.send :include, SslRequirement
        receiver.send :include, Spree::Core::CurrentOrder

        receiver.rescue_from CanCan::AccessDenied do |exception|
          return unauthorized
        end
      end

      def access_forbidden
        render :text => 'Access Forbidden', :layout => true, :status => 401
      end

      # can be used in views as well as controllers.
      # e.g. <% title = 'This is a custom title for this view' %>
      attr_writer :title

      def title
        title_string = @title.present? ? @title : accurate_title
        if title_string.present?
          if Spree::Config[:always_put_site_name_in_title]
            [default_title, title_string].join(' - ')
          else
            title_string
          end
        else
          default_title
        end
      end

      # To be overriden by authentication extensions
      def spree_current_user
        nil
      end

      protected

      # Needs to be overriden so that we use Spree's Ability rather than anyone else's.
      def current_ability
        @current_ability ||= Spree::Ability.new(spree_current_user)
      end

      def store_location
        # disallow return to login, logout, signup pages
        authentication_routes = [:spree_signup_path, :spree_login_path, :spree_logout_path]
        disallowed_urls = []
        authenticated_routes.each do |route|
          if respond_to?(route)
            disallowed_urls << send(route)
          end
        end

        disallowed_urls.map!{ |url| url[/\/\w+$/] }
        unless disallowed_urls.include?(request.fullpath)
          session['user_return_to'] = request.fullpath.gsub('//', '/')
        end
      end

      # Redirect as appropriate when an access request fails.  The default action is to redirect to the login screen.
      # Override this method in your controllers if you want to have special behavior in case the user is not authorized
      # to access the requested action.  For example, a popup window might simply close itself.
      def unauthorized
        respond_to do |format|
          format.html do
            if spree_current_user
              flash.now[:error] = t(:authorization_failure)
              render 'spree/shared/unauthorized', :layout => '/spree/layouts/spree_application', :status => 401
            else
              store_location
              url = respond_to?(:spree_login_path) ? spree_login_path : root_path
              redirect_to url
            end
{'fraction_non_alphanumeric': 0.07463938275746394, 'fraction_numerical': 0.0020127474002012745, 'mean_word_length': 2.3350111856823266, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '660953', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1644, 'n_words': 477}