简体   繁体   中英

Rails Mercury authentication

I have set up mercury in my application. It works properly but since I'm still very new to rails I can't set up authentication. This is what I tried after run: rails generate mercury:install:authentication

  • It seems I can use the module in lib directory

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

And I tried to use this method in the view but it doesn't work. Lib directory should be autoloaded since that row is not commented in config file.

By the way, just adding a before_filter on the update method , I prevent normal users form confirming edited pages. But they can still see the editor itself if they modify the Url manually which is unwanted.

  • I tried to override Mercury Controller but it doesn't even work

Any suggestion?

Late answer I know but if it helps anyone else.

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
 ....

restart your server and then only the admin can see and update the page

I had the same problem. The solution that worked for me was to explicitly include whichever module handles into authentication.rb like so:

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

Insert your module and authentication method accordingly and restart the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM