Sha256: 82bb3bd6ec4a696ff8386c8e60f89527d91eee3cfea37c1e62f39708f5f06c10

Contents?: true

Size: 1.12 KB

Versions: 24

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Thredded
  class PrivatePostPolicy
    # The scope of readable private posts.
    # {PrivateTopicPolicy} must be applied separately.
    class Scope
      # @param user [Thredded.user_class]
      # @param scope [ActiveRecord::Relation<Thredded::Post>]
      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      # @return [ActiveRecord::Relation<Thredded::Post>]
      def resolve
        @scope
      end
    end

    # @param user [Thredded.user_class]
    # @param post [Thredded::PrivatePost]
    def initialize(user, post)
      @user = user
      @post = post
    end

    def create?
      @user.thredded_admin? || @post.postable.users.include?(@user) && !@user.thredded_user_detail.blocked?
    end

    def read?
      Thredded::PrivateTopicPolicy.new(@user, @post.postable).read?
    end

    def update?
      @user.thredded_admin? || own_post?
    end

    def destroy?
      !@post.first_post_in_topic? && update?
    end

    def anonymous?
      @user.thredded_anonymous?
    end

    private

    def own_post?
      @user.id == @post.user_id
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
thredded-1.1.0 app/policies/thredded/private_post_policy.rb
thredded-1.0.1 app/policies/thredded/private_post_policy.rb
thredded-1.0.0 app/policies/thredded/private_post_policy.rb
thredded-0.16.16 app/policies/thredded/private_post_policy.rb
thredded-0.16.15 app/policies/thredded/private_post_policy.rb
thredded-0.16.14 app/policies/thredded/private_post_policy.rb
thredded-0.16.13 app/policies/thredded/private_post_policy.rb
thredded-0.16.12 app/policies/thredded/private_post_policy.rb
thredded-0.16.11 app/policies/thredded/private_post_policy.rb
thredded-0.16.10 app/policies/thredded/private_post_policy.rb
thredded-0.16.9 app/policies/thredded/private_post_policy.rb
thredded-0.16.8 app/policies/thredded/private_post_policy.rb
thredded-0.16.7 app/policies/thredded/private_post_policy.rb
thredded-0.16.6 app/policies/thredded/private_post_policy.rb
thredded-0.16.5 app/policies/thredded/private_post_policy.rb
thredded-0.16.4 app/policies/thredded/private_post_policy.rb
thredded-0.16.3 app/policies/thredded/private_post_policy.rb
thredded-0.16.1 app/policies/thredded/private_post_policy.rb
thredded-0.16.0 app/policies/thredded/private_post_policy.rb
thredded-0.15.5 app/policies/thredded/private_post_policy.rb