简体   繁体   English

在Rails中使用Devise Gem时如何让admin只注册新的管理员?

[英]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.

相关问题 使用 Ruby on Rails Devise Gem 使用用户名注册 - Register with a username using Ruby on Rails Devise Gem 如何使用Devise向管理员发送管理员专用的密码重置电子邮件? - How to send an admin specific password reset email to admins using Devise? 仅允许特定域名在Rails App(Devise gem)中注册 - Allow only specific domain names to register in Rails App (Devise gem) 使用devise gem,我的管理员如何同时充当Rails4中的管理员和用户 - using devise gem, how my admin works as both an admin and a user in rails4 如何在不使用Ruby on Rails的设计的情况下添加新的管理员用户 - How to add new admin user without using of devise in Ruby on Rails 在 Rails 中使用 Devise gem 时,如何记录失败的登录和锁定? - In Rails when using the Devise gem, how to log failed logins and lockups? Devise Gem Ruby On Rails-删除管理员的“取消我的帐户” - Devise Gem Ruby On Rails - Removing “Cancel my account” for admins social_stream如何注册新用户(使用devise gem)? - How social_stream register new user works (using devise gem)? 在Rails中使用Rails_admin gem在管理员中创建新用户时出现问题 - Issue when create new user in admin using Rails_admin gem in Rails 如何使用devise gem维护管理员和用户?(第4条) - How to maintain both admin and user with devise gem?(Rails 4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM