Sha256: 36cd6edae84772499877da718e06a2c1209461f33827856b7d3b183f9952cb49

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    class Permissions < Decidim::DefaultPermissions
      def permissions
        return permission_action if permission_action.subject != :comment

        case permission_action.action
        when :read
          can_read_comments?
        when :create
          can_create_comment?
        when :update, :destroy
          can_update_comment?
        when :vote
          can_vote_comment?
        end

        permission_action
      end

      private

      def can_read_comments?
        return disallow! unless commentable.commentable?

        allow!
      end

      def can_create_comment?
        return disallow! unless user
        return disallow! unless commentable.commentable?
        return disallow! unless commentable&.user_allowed_to_comment?(user)

        allow!
      end

      def can_update_comment?
        return disallow! unless user
        return disallow! unless comment.authored_by?(user)

        allow!
      end

      def can_vote_comment?
        return disallow! unless user
        return disallow! unless commentable&.user_allowed_to_vote_comment?(user)

        allow!
      end

      def commentable
        @commentable ||= if comment
                           comment.root_commentable
                         else
                           context.fetch(:commentable, nil)
                         end
      end

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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-comments-0.29.1 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.4 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.9 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.29.0 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.3 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.8 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.29.0.rc4 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.29.0.rc3 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.29.0.rc2 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.29.0.rc1 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.2 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.7 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.1 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.6 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.0 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.5 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.0.rc5 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.28.0.rc4 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.4 app/permissions/decidim/comments/permissions.rb
decidim-comments-0.27.3 app/permissions/decidim/comments/permissions.rb