简体   繁体   中英

Rails 4 Devise Strong Parameters Admin Model

I have made a user and admin model using devise. I have used strong parameters in the file 文件中使用了强大的参数

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) }
  end

end

How do I whitelist the admin model?

devise_parameter_sanitizer.for(:sign_in) { |a| a.permit(:login, :password, :remember_me) }

Also how do I whitelist the admin sign_up so that no variables may be passed into it? My guess is

devise_parameter_sanitizer.for(:sign_up) { |a| a.permit()}

UPDATE

I would like to edit my question.

My question is how do I get the admin model to automatically blacklist my admin sign up page? If I simply leave nothing then I can still sign up through the . Sure I can delete the :regisitrations within the , but I would like to deny command line sign ups 的:regisitrations,但是我想拒绝命令行注册

--Would it be wise to use scoped views and specifically define each view for the admin and user models?--

If you have separate controllers for users and admins then try something like this:

def configure_permitted_parameters
  if params[:controller] == "user"
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) }
  else
    devise_parameter_sanitizer.for(:sign_in) { |a| a.permit(:login, :password, :remember_me) }
    devise_parameter_sanitizer.for(:sign_up) { |a| a.permit()}

and there's another approach to it as well, create only one controller for both users and admin, named registrations_controller.rb, have a field in your users table named is_admin and set it for a user only if he/she is a admin. Create a seed to make your admins like this in your seeds.rb file:

admin_user = User.new(email: "abc@xyz.com", password: "123", password_confirmation: "123", is_admin: "true" )
admin_user.skip_confirmation!
admin_user.save

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