Sha256: 9b893b5c79003ad25ee41d6ee0c14bfdf42f9efda70a0d27e6c007dfca0e3f86

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # Shared behaviour for controllers that need authorization to work.
  module NeedsAuthorization
    extend ActiveSupport::Concern

    included do
      check_authorization

      rescue_from CanCan::AccessDenied, with: :user_not_authorized

      private

      # Overwrites `cancancan`'s method to point to the correct ability class,
      # since the gem expects the ability class to be in the root namespace.
      def current_ability
        @current_ability ||= Decidim::Ability.new(current_user)
      end

      # Handles the case when a user visits a path that is not allowed to them.
      # Redirects the user to the root path and shows a flash message telling
      # them they are not authorized.
      def user_not_authorized
        flash[:alert] = t("actions.unauthorized", scope: "decidim.core")
        redirect_to(request.referrer || user_not_authorized_path)
      end

      def user_not_authorized_path
        raise NotImplementedError
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.0.2 app/controllers/concerns/decidim/needs_authorization.rb
decidim-core-0.0.1 app/controllers/concerns/decidim/needs_authorization.rb
decidim-core-0.0.1.alpha9 app/controllers/concerns/decidim/needs_authorization.rb
decidim-core-0.0.1.alpha8 app/controllers/concerns/decidim/needs_authorization.rb