Sha256: db866bceb0ae7200174dd257686a61c35104a4963b1445128d9d56ff7d7c6585

Contents?: true

Size: 1.1 KB

Versions: 79

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A Rails routes constraint to only allow access to an Organization admin to
    # the organization dashboard.
    class OrganizationDashboardConstraint
      # Initializes the contraint.
      #
      # request [Rack::Request]
      def initialize(request)
        @request = request
      end

      # Checks if the user can access the organization dashboard.
      #
      # Returns boolean.
      def matches?
        user && user.organization == organization && user_has_permission_to_access_dashboard?
      end

      private

      attr_reader :request

      def organization
        request.env["decidim.current_organization"]
      end

      def user
        return unless request.env["warden"].authenticate!(scope: :user)

        @user ||= request.env["warden"].user("user")
      end

      def user_has_permission_to_access_dashboard?
        Decidim::Admin::Permissions.new(
          user,
          Decidim::PermissionAction.new(scope: :admin, action: :read, subject: :admin_dashboard)
        ).permissions.allowed?
      end
    end
  end
end

Version data entries

79 entries across 79 versions & 1 rubygems

Version Path
decidim-admin-0.28.4 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.9 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.3 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.8 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.2 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.7 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.1 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.6 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.26.10 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.26.9 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.0 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.5 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.0.rc5 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.28.0.rc4 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.26.8 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.4 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.3 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.26.7 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.26.5 app/constraints/decidim/admin/organization_dashboard_constraint.rb
decidim-admin-0.27.2 app/constraints/decidim/admin/organization_dashboard_constraint.rb