Sha256: 2b532f273e5ad219e4b16f959ec2a044db3221f975be201b45ad88532dad3409

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Decidim
  module <%= component_module_name %>
    module Abilities
      # Defines the abilities related to <%= component_name %> for a logged in user.
      # Intended to be used with `cancancan`.
      class CurrentUserAbility
        include CanCan::Ability

        attr_reader :user, :context

        def initialize(user, context)
          return unless user

          @user = user
          @context = context

          # can :manage, SomeResource if authorized?(:some_action)
        end

        private

        def authorized?(action)
          return unless component

          ActionAuthorizer.new(user, component, action).authorize.ok?
        end

        def current_settings
          context.fetch(:current_settings, nil)
        end

        def component_settings
          context.fetch(:component_settings, nil)
        end

        def component
          component = context.fetch(:current_component, nil)
          return nil unless component && component.manifest.name == :<%= component_name %>

          component
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-generators-0.11.2 lib/decidim/generators/component_templates/app/models/decidim/component/abilities/current_user_ability.rb.erb
decidim-generators-0.11.1 lib/decidim/generators/component_templates/app/models/decidim/component/abilities/current_user_ability.rb.erb
decidim-generators-0.11.0.pre1 lib/decidim/generators/component_templates/app/models/decidim/component/abilities/current_user_ability.rb.erb