【问题标题】:Rails Mercury authenticationRails Mercury 身份验证
【发布时间】:2012-06-30 15:52:29
【问题描述】:

我已在我的应用程序中设置了水星。它工作正常,但由于我对 Rails 还是很陌生,所以无法设置身份验证。这是我在运行后尝试的: rails 生成水银:安装:身份验证

  • 看来我可以使用lib目录下的模块

    module Mercury
      module Authentication
    
         def can_edit?
           true if :authenticate_admin!  //(from device)
         end
      end
    end
    

我尝试在视图中使用此方法,但它不起作用。 Lib 目录应该被自动加载,因为该行没有在配置文件中注释。

顺便说一句,只是在更新方法上添加了一个 before_filter,我阻止了普通用户确认已编辑的页面。但如果他们手动修改 URL(这是不需要的),他们仍然可以看到编辑器本身。

  • 我试图覆盖 Mercury 控制器,但它甚至不起作用

有什么建议吗?

【问题讨论】:

  • 您需要定义“它不起作用”。您是否收到错误、堆栈跟踪或一些逻辑错误?
  • 我无法阻止用户查看编辑器。所以我没有正确使用 Mercury 的身份验证。

标签: ruby-on-rails authorization administration helpers mercury-editor


【解决方案1】:

我知道迟到的答案,但如果它对其他人有帮助。

lib/mercury/authentication.rb

module Mercury
  module Authentication

    def can_edit?
      if user_signed_in? && current_user.admin?
        true
      else 
        redirect_to root_path
      end
   end

 end
end

applicationcontroller.rb

class ApplicationController < ActionController::Base
   protect_from_forgery
   include Mercury::Authentication
 ....

重启你的服务器,然后只有管理员才能看到和更新页面

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。对我有用的解决方案是将处理的任何模块显式包含到 authentication.rb 中,如下所示:

    module Mercury
        module Authentication
            include YourAppName::YourAuthenticationModule
            def can_edit?
                admin_signed_in?
            end
        end
    end
    

    相应地插入您的模块和身份验证方法并重新启动服务器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-15
      • 2011-02-21
      • 2016-04-12
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      相关资源
      最近更新 更多