Sha256: cfb70f0cf66ab4b5ec513c12f55af5c55dc69a0962d05e8fbcca6e9beffc9a04

Contents?: true

Size: 1.59 KB

Versions: 68

Compression:

Stored size: 1.59 KB

Contents

module Pageflow
  class ThemingPolicy < ApplicationPolicy
    class Scope < Scope
      attr_reader :user, :scope

      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      def themings_allowed_for(accounts)
        if user.admin?
          scope.all
        else
          accounts_ids = accounts.try(:id) || accounts.try(:length) && accounts.map(&:id)
          scope.joins(publisher_memberships_for_accounts(user, accounts_ids))
            .where(membership_is_present)
        end
      end

      private

      def publisher_memberships_for_accounts(user, accounts_ids)
        sanitize_sql_array(['LEFT OUTER JOIN pageflow_memberships ON ' \
                            'pageflow_memberships.user_id = :user_id AND ' \
                            'pageflow_themings.account_id IN (:accounts_ids) AND ' \
                            'pageflow_memberships.entity_id IN (:accounts_ids) AND ' \
                            'pageflow_memberships.entity_type = "Pageflow::Account" AND ' \
                            'pageflow_memberships.role IN ("publisher", "manager")',
                            user_id: user.id, accounts_ids: accounts_ids])
      end

      def membership_is_present
        'pageflow_memberships.entity_id IS NOT NULL'
      end
    end

    def initialize(user, theming)
      @user = user
      @theming = theming
    end

    def edit?
      allows?(%w(publisher manager))
    end

    def index_widgets_for?
      @user.admin?
    end

    private

    def allows?(roles)
      @user.memberships.where(role: roles, entity: @theming.account).any?
    end
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/policies/pageflow/theming_policy.rb
pageflow-15.1.2 app/policies/pageflow/theming_policy.rb
pageflow-15.1.1 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.rc0 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta6 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta5 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta4 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta3 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta2 app/policies/pageflow/theming_policy.rb
pageflow-15.1.0.beta1 app/policies/pageflow/theming_policy.rb
pageflow-15.0.2 app/policies/pageflow/theming_policy.rb
pageflow-15.0.1 app/policies/pageflow/theming_policy.rb
pageflow-15.0.0 app/policies/pageflow/theming_policy.rb
pageflow-14.5.1 app/policies/pageflow/theming_policy.rb
pageflow-14.5.0 app/policies/pageflow/theming_policy.rb
pageflow-15.0.0.rc2 app/policies/pageflow/theming_policy.rb
pageflow-15.0.0.rc1 app/policies/pageflow/theming_policy.rb
pageflow-15.0.0.beta4 app/policies/pageflow/theming_policy.rb
pageflow-15.0.0.beta3 app/policies/pageflow/theming_policy.rb