<commit_before>module RestfulAclController

  def self.included(base)
    base.extend(ClassMethods)
    base.send :include, ClassMethods
  end

  module ClassMethods

    attr_accessor :restful_object, :restful_parent, :restful_klass, :restful_user

    def has_permission?
      return true if administrator?

      load_actors(params[:id])

      begin
        # Let's let the Model decide what is acceptable
        permission_denied unless case params[:action]
          when "index"          then @restful_klass.is_indexable_by(@restful_user, @restful_parent)
          when "new", "create"  then @restful_klass.is_creatable_by(@restful_user, @restful_parent)
          when "show"           then @restful_object.is_readable_by(@restful_user, @restful_parent)
          when "edit", "update" then @restful_object.is_updatable_by(@restful_user, @restful_parent)
          when "destroy"        then @restful_object.is_deletable_by(@restful_user, @restful_parent)
          else check_non_restful_route
        end

      rescue NoMethodError => e
        # Misconfiguration: A RESTful_ACL specific method is missing.
        raise_error(e)
      rescue
        # Failsafe: If any funny business is going on, log and redirect
        routing_error
      end
    end

    private

      def load_actors(id)
        @restful_user = current_user

        # Load the Model based on the controller name
        @restful_klass = self.controller_name.classify.demodulize.constantize

        if id.present?
          # Load the object and possible parent requested
          @restful_object = @restful_klass.find(params[:id])
          @restful_parent = @restful_object.get_mom if @restful_klass.has_parent?
        else
          # No object was requested, so we need to go to the URI to figure out the parent
          @restful_parent = get_morestful_frorestful_request_uri(@restful_klass) if @restful_klass.has_parent?

          if @restful_klass.is_singleton?
            @restful_object = @restful_parent.send(@restful_klass.to_s.tableize.singularize.to_sym)
          else
            # No object was requested (index, create actions)
            @restful_object = nil
          end
        end
      end

      def check_non_restful_route
        if @restful_object
          @restful_object.is_readable_by(@restful_user, @restful_parent)
        elsif @restful_klass
          @restful_klass.is_indexable_by(@restful_user, @restful_parent)
        else
          false # If all else fails, deny access
        end
      end

      def get_method_frorestful_error(error)
        error.message.gsub('`', "'").split("'").at(1)
      end

      def raise_error(error)
        method = get_method_frorestful_error(error)
        message = (is_class_method?(method)) ? "#{@restful_klass}#self.#{method}" : "#{@restful_klass}##{method}"
        raise NoMethodError, "[RESTful_ACL] #{message}(user, parent = nil) seems to be missing?"
      end

      def is_class_method?(method)
        method =~ /[index|creat]able/
      end

      def get_morestful_frorestful_request_uri(child_klass)
        parent_klass = child_klass.mom.to_s
        bits         = request.request_uri.split('/')
        parent_id    = bits.at(bits.index(parent_klass.pluralize) + 1)

        parent_klass.classify.constantize.find(parent_id)
      end

      def administrator?
        @restful_user.respond_to?("is_admin?") && @restful_user.is_admin?
      end

      def blame
        @restful_user.respond_to?(:login) ? @restful_user.login : @restful_user.username
      end

      def permission_denied
        logger.info("[RESTful_ACL] Permission denied to %s at %s for %s" % [(logged_in? ? blame : 'guest'), Time.now, request.request_uri])
        redirect_to denied_url
      end

      def routing_error
        logger.info("[RESTful_ACL] Routing error by %s at %s for %s" % [(logged_in? ? blame : 'guest'), Time.now, request.request_uri])
        redirect_to error_url
      end

  end

end
<commit_msg>Heavily refactored and moved most functionality to :base lib<commit_after>module RestfulAcl
  module Controller

    def self.included(base)
      base.extend(ClassMethods)
      base.send :include, ClassMethods
    end

    module ClassMethods

      def has_permission?
        options = {
          :controller_name => self.controller_name,
          :object_id       => params[:id],
          :uri             => request.request_uri,
          :user            => current_user,
          :action          => params[:action]
        }

        permission_denied unless RestfulAcl::Base.new(options).allowed?
      end


      private

        def permission_denied
          logger.info("[RESTful_ACL] Permission denied to %s at %s for %s" % [blame, Time.now, request.request_uri])
          redirect_to denied_url
        end

        def blame
          (@current_user.present?) ? "User ##{@current_user.id}" : "GUEST"
        end

    end

  end
end

{'fraction_non_alphanumeric': 0.08802104411169567, 'fraction_numerical': 0.0004046944556859571, 'mean_word_length': 2.535765379113019, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7365063', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1508, 'n_words': 338}