Sha256: 8ae79bbb1444a9d2eec4f3498893a953c5482c8f682f8afb14e2872e21aeda01

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Admin
      class Permissions < Decidim::DefaultPermissions
        def permissions
          return permission_action unless user
          return permission_action unless permission_action.scope == :admin

          unless user.admin?
            disallow!
            return permission_action
          end

          if read_admin_dashboard_action?
            allow!
            return permission_action
          end

          allowed_translation_set_action?
          allowed_translation_action?

          permission_action
        end

        private

        def translation_set
          @translation_set ||= context.fetch(:translation_set, nil)
        end

        def translation
          @translation ||= context.fetch(:translation, nil)
        end

        def allowed_translation_set_action?
          return unless permission_action.subject == :translation_set

          case permission_action.action
          when :create, :read
            allow!
          when :update, :destroy
            toggle_allow(translation_set.present?)
          end
        end

        def allowed_translation_action?
          return unless permission_action.subject == :translation

          case permission_action.action
          when :create, :read
            allow!
          when :update, :destroy
            toggle_allow(translation.present?)
          end
        end

        def read_admin_dashboard_action?
          permission_action.action == :read &&
            permission_action.subject == :admin_dashboard
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-term_customizer-0.16.6 app/permissions/decidim/term_customizer/admin/permissions.rb
decidim-term_customizer-0.16.5 app/permissions/decidim/term_customizer/admin/permissions.rb
decidim-term_customizer-0.16.3 app/permissions/decidim/term_customizer/admin/permissions.rb
decidim-term_customizer-0.16.2 app/permissions/decidim/term_customizer/admin/permissions.rb
decidim-term_customizer-0.16.1 app/permissions/decidim/term_customizer/admin/permissions.rb
decidim-term_customizer-0.16.0 app/permissions/decidim/term_customizer/admin/permissions.rb