2016-11-04 44 views
0

我有一个rails应用程序,我在其中添加了一个名为'authorized'的布尔字段给用户模型。基本上,我想锁定应用程序,只有授权用户才能访问应用程序。我试图做到这一点在我的应用控制器:在设计中检查授权标志

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 

    def authorized 
    redirect_to root_path, alert: "Not Authorized" if !current_user.authorized? 
    end 

end 

然而,当我这样做,因为我有设置到需要验证的路径的根本途径我得到一个重定向错误。

我现在可以在视图或其他控制器中执行此检查,但是我想在应用程序控制器中执行此操作,因为我希望整个应用程序都处于锁定状态。

+0

写入的代码片段将用户重定向到th如果当前用户被授权,则为root_path。那是对的吗? –

+0

Opps!你是对的...现在修好了。 – Lumbee

回答

0

这里是我的解决方案...

在我想要的 '授权' 认证我添加了控制器...

before_filter :authorized, :except => :not_authorized 

... 

def authorized 
    if !current_user.authorized? 
     redirect_to not_authorized_path 
    end 
end 

在我的路线,我说..

get 'not_authorized' => 'my_controller#not_authorized' 

...并添加了一个简单的应用程序/ views/my_controller/not_authorized.html.erb

0

您可以在用户模型中重写这些方法。

def active_for_authentication? 
    super && approved? 
    end 

    def inactive_message 
    'my custom inactive message' unless approved? 
    super 
    end 

我建议你通过批准更改授权名称,我不确定但授权声音像内部Devise方法名称。