【问题标题】:Rails - Limited Browsing of User Attachments using ckeditor Gem w/ CanCan & PaperclipRails - 使用带有 CanCan 和 Paperclip 的 ckeditor Gem 有限浏览用户附件
【发布时间】:2014-07-29 02:42:24
【问题描述】:

我的一切工作正常,但现在我想限制一些用户能力以执行一些附件操作。

具体来说,将所有已上传附件的查看限制为用户实际上传的附件。

这是我尝试过的ability.rb中适用的sn-p ...

if user.id
  can :access, :ckeditor
  can [:read, :create, :destroy], Ckeditor::Picture, assetable_id: user.id
  can [:read, :create, :destroy], Ckeditor::AttachmentFile, assetable_id: user.id
end

这种情况出现在我使用CKeditor UI的时候,点击Image按钮,然后点击Browse Server按钮就可以看到之前上传的图片——现在图片浏览器显示所有用户的上传。我希望查看的图像仅限于 current_user 的图像。

由于Ckeditor表中保存了附件的assetable_id(即user.id),而上面的逻辑本身并不起作用,我猜还需要一些自定义的Controller逻辑在这里。

谢谢。

【问题讨论】:

    标签: ruby-on-rails ckeditor paperclip cancan


    【解决方案1】:

    我能够使用自定义 Ckeditor 控制器解决此问题,并从此处获得一些指导: https://github.com/galetahub/ckeditor/issues/246

    首先我需要复制 Ckeditor 控制器 pictures_controller.rbattachment_files_controller.rb 并将它们放在这里: /app/controllers/ckeditor/

    然后对他们的更新index 的建议进行一些更新是必要的,特别是picture_model.find_all 需要在pictures_controller.rb 中为picture_adapter.find_all(类似地attachment_file_adapter.find_allattachment_files_controller .rb)

    这一切的关键是设置适当的范围:ckeditor_pictures_scope(assetable_id: ckeditor_current_user) & ckeditor_attachment_files_scope(assetable_id: ckeditor_current_user)

    一旦这些修订到位,图片和附件的文件浏览器将只显示该用户的适当文件。

    这是修改后的文件...更改都在第 4 行。

    /app/controllers/ckeditor/pictures_controller.rb

    class Ckeditor::PicturesController < Ckeditor::ApplicationController
    
      def index
        @pictures = Ckeditor.picture_adapter.find_all(ckeditor_pictures_scope(assetable_id: ckeditor_current_user))
        @pictures = Ckeditor::Paginatable.new(@pictures).page(params[:page])
        respond_with(@pictures, :layout => @pictures.first_page?)
      end
    
      def create
        @picture = Ckeditor.picture_model.new
        respond_with_asset(@picture)
      end
    
      def destroy
        @picture.destroy
        respond_with(@picture, :location => pictures_path)
      end
    
      protected
    
      def find_asset
        @picture = Ckeditor.picture_adapter.get!(params[:id])
      end
    
      def authorize_resource
        model = (@picture || Ckeditor.picture_model)
        @authorization_adapter.try(:authorize, params[:action], model)
      end
    
    end
    

    /app/controllers/ckeditor/attachment_files_controller.rb

    class Ckeditor::AttachmentFilesController < Ckeditor::ApplicationController
    
      def index
        @attachments = Ckeditor.attachment_file_adapter.find_all(ckeditor_attachment_files_scope(assetable_id: ckeditor_current_user))
        @attachments = Ckeditor::Paginatable.new(@attachments).page(params[:page])
        respond_with(@attachments, :layout => @attachments.first_page?)
      end
    
      def create
        @attachment = Ckeditor.attachment_file_model.new
        respond_with_asset(@attachment)
      end
    
      def destroy
        @attachment.destroy
        respond_with(@attachment, :location => attachment_files_path)
      end
    
      protected
    
      def find_asset
        @attachment = Ckeditor.attachment_file_adapter.get!(params[:id])
      end
    
      def authorize_resource
        model = (@attachment || Ckeditor.attachment_file_model)
        @authorization_adapter.try(:authorize, params[:action], model)
      end
    
    end
    

    【讨论】:

    • 我认为您应该将gem 'responders', '~&gt; 2.0' 添加到您的Gemfile,并将此代码添加到您的课程respond_to :html 之前def index。最后将此代码Rails.application.config.assets.precompile += %w( ckeditor/filebrowser/images/gal_del.png ) 添加到您的config/initializers/assets.rb
    • 如果您同意。我可以编辑你的答案。这些都是我遇到的问题。
    • 非常感谢!在我在 config.js 文件上设置一些配置之后,CKeditor 给了我很多问题,后来,我在 ckeditor 控制器上遇到了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-06-18
    • 2012-06-06
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    相关资源
    最近更新 更多