我使用Devise
gem进行身份验证。我已经创建了一个Admin模型并添加了Devise。通常任何人都可以注册。但我只需要一个管理员来添加新的管理员到系统。我怎么能这样做?
答案 0 :(得分:2)
实际上,您可以在devise wiki
中找到答案首先你应该阅读这个How To: Add an Admin Role,选项2适合你的情况。
添加"超级管理员"使用布尔类型归属于您的管理模型。
$ rails generate migration add_super_admin_to_users super_admin:boolean
如果您正在使用postgresql / mysql进行dbms,请不要忘记在迁移之前将默认值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>
答案 1 :(得分:0)
你在这个seed.rb文件下转到db
您输入详细信息
Role.create(姓名:&#39;管理员&#39;)
User.create(姓名:&#34;管理员&#34;,电子邮件:admin@ibc.com",密码:&#34;管理员&#34;,密码_确认:&#34;管理员)
广告转到终端类型
$ rake db:seed
并检查数据库字段