[英]In Rails when using the Devise Gem how to let only admin register new admins?
Im using the Devise
gem for authentications. 我使用
Devise
gem进行身份验证。 I have already created an Admin model and added Devise to it. 我已经创建了一个Admin模型并添加了Devise。 Normally anyone can register.
通常任何人都可以注册。 But I need only an Admin to add new Admins to the system.
但我只需要一个管理员来添加新的管理员到系统。 How can I do that?
我怎样才能做到这一点?
Actually, you can found your answer in devise wiki 实际上,你可以在设计维基中找到答案
First you should read this How To: Add an Admin Role , Option 2 is suitable for your case. 首先,您应该阅读如何:添加管理员角色 ,选项2适合您的情况。
Adding an "super admin" attribute to your admin model with boolean type. 使用布尔类型向管理模型添加“超级管理员”属性。
$ rails generate migration add_super_admin_to_users super_admin:boolean
If you're using postgresql/mysql for dbms, Don't forget before migration add default false to super_admin
attibute 如果你正在使用postgresql / mysql for dbms,请不要忘记在迁移之前将default default false添加到
super_admin
If you have "Super Admin" user on your database, and then you should customize admin registration pages, Devise::RegistrationsController . 如果您的数据库上有“超级管理员”用户,那么您应该自定义管理员注册页面, Devise :: RegistrationsController 。 This customize devise regristration controller
这个自定义设备注册控制器
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
And add authenticate_role!
并添加
authenticate_role!
on your application_controller.rb
looks like 在您的
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
Add this to your views/devise/registrations/new.html.erb
将其添加到您的
views/devise/registrations/new.html.erb
<div><%= f.label :super_admin %><br />
<%= f.check_box :super_admin %></div>
you go to db under this seed.rb file 你去这个seed.rb文件下的db
you type your details 你输入你的详细信息
Role.create(name: 'Admin') Role.create(名称:'Admin')
User.create(name: "admin", email: admin@ibc.com", password: "admin", password_confirm: "admin) User.create(名称:“admin”,电子邮件:admin@ibc.com“,密码:”admin“,password_confirm:”admin)
ad go to terminal type 广告转到终端类型
$rake db:seed $ rake db:seed
and check database fields 并检查数据库字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.