我正在使用Devise
gem进行身份验证。我已经创建了一个Admin模型,并向其中添加了Devise。通常任何人都可以注册。但是我只需要一个管理员即可向系统添加新的管理员。我怎样才能做到这一点?
实际上,您可以在devise Wiki中找到答案
首先,您应该阅读此方法:添加管理员角色,方法2适合您的情况。
使用布尔类型向您的管理模型添加“超级管理员”属性。
$ rails generate migration add_super_admin_to_users super_admin:boolean
如果您对dbms使用postgresql / mysql,请不要忘记在迁移之前将默认false添加到super_admin
attibute
如果您的数据库上具有“超级管理员”用户,则应该自定义管理员注册页面Devise :: RegistrationsController。该定制设计注册控制器
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
before_filter :authenticate_user!, :authenticate_role!
# GET /resource/sign_up
def new
super
end
# POST /resource
def create
super
end
protected
def after_sign_up_path_for(resource)
your_specific_path
end
end
并添加authenticate_role!
您的application_controller.rb
外观
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def authenticate_role!
if current_user.super_admin == true
super
else
redirect_to another_path, notice: "You can't access this page"
end
end
end
将此添加到您的 views/devise/registrations/new.html.erb
<div><%= f.label :super_admin %><br />
<%= f.check_box :super_admin %></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句