2012-04-04 69 views
3

我试图限制注册到Devise管理员。如果可能的话,我现在想避免使用CanCan。我已经创建了一个单独的设计管理模型,如选项#1中所述:https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role如何使用Devise限制注册到管理员

接下来,我为用户设置了一个CRUD界面,如下所述:https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface

我想在Users控制器中使用诸如before_filter :authenticate_admin!之类的东西限制新注册,但由于某些原因,它不限制新注册。

我的routes.rb看起来是这样的:

devise_for :admins 
devise_for :users, :path_prefix => 'd' 
resources :admins 
resources :users, :controller => "users" 

任何想法,为什么before_filter :authenticate_admin!不限制新注册?

回答

8

您不能在用户控制器中使用before_filter :authenticate_admin!,因为管理员和用户是您应用中的两种不同型号。

我不知道我完全理解你的意思,但你可以做到这一点,如果你不想接受用户新注册(或管理):

# in your User(Admin) model 
devise :registerable # remove :registerable 

希望这有助于!

+0

当你添加一个管理员角色时,你应该能够使用'before_filter:authenticate_admin!'。目前,我正在为用户索引和显示操作(用于以管理员身份管理用户)为我工作。我不确定为什么它不能在创建操作上工作。我不想删除:可注册,因为我希望管理员能够添加用户。 – Scott 2012-04-05 01:17:59

+0

嗨斯科特,你不需要定义一个新的'create'动作。如果你仔细看看devise的源代码,你可以在'app/controllers/devise/registrations_controller.rb'中找到devise为你做的一切,除了这些动作'authenticate_scope!''authenticate_admin! '在你的情况下,是前置的,只适用于**'编辑销毁更新**这三个动作。如果你真的想添加'authenticate_admin!'过滤器来创建动作,你可以继承'Devise :: RegistrationsController'并添加'prepend_before_filter:authenticate_scope !,:except => [:cancel]'。 – Tomato 2012-04-05 02:09:53

+1

我认为这只是使用设计变得太复杂了,所以我添加了CanCan,这非常棒。谢谢您的帮助。 – Scott 2012-04-07 01:21:54

2

我正在寻找类似的东西;完全禁用新的注册。我一个邮件列表上的某个地方挖了这件事,虽然它解决了我的问题,这可能是你的一个不错的起点:

class RegistrationsController < Devise::RegistrationsController 
    def new 
    flash[:failure] = t('registrations.registrations_disabled') 
    redirect_to root_path 
    end 
end 

也许类似的东西,但添加一个检查,看看是否CURRENT_USER是管理员然后重定向基于在那...

1

我沉吟了一会儿,终于想出了这个。

有由色器件创建

class UsersController < Devise::RegistrationsController 

    before_filter :authenticate_admin! 

    def new 
    if admin_signed_in? 
     super 
    else 
     redirect_to admin_session_path 
    end 
    end 

希望这有助于每个模型的辅助功能。它就像一个魅力:)

相关问题