Devise/Rails - How to allow only admin to create account for others?

前端 未结 3 544
走了就别回头了
走了就别回头了 2021-02-06 16:33

I am using devise as my authentication solution and now i am thinking about authorization. In my project I (the admin) is the only person authorized to create account for others

3条回答
  •  遇见更好的自我
    2021-02-06 17:15

    Setting :skip => :registrations also kills the ability for a user to edit their user info. If that's not what you are after you can instead create a (minimal) custom registrations controller and only remove the new_user_registration_path while preserving the edit_user_registration_path.

    # app/controllers/registrations_controller.rb
    class RegistrationsController < Devise::RegistrationsController
    
     def new
        # If you're not using CanCan, raise some other exception, or redirect as you please
        raise CanCan::AccessDenied
      end
    
    end
    
    # routes.rb
    devise_for :users, :controllers => { :registrations => "registrations" }
    

    Once you do this you also need to move the directory views/devise/registrations to just views/registrations.

提交回复
热议问题