如何使我的 ruby on rails 应用程序的某些页面无法访问我的一种基于 STI 的 Devise User 模型类型?
How do I make certain pages of my ruby on rails application inaccessible to one of my STI based Devise User model types?
我希望我的 ruby 在 rails Web 应用程序中的一页无法被我的一种 STI 模型类型访问。我有两个模型 typeA 和 typeB 继承自 User。我已经使用 User table 中的列类型来实现 STI。我正在为用户会话使用 Devise gem。我想要一个网页“http://localhost:3000/rate' inaccessible to my typeA User. Whenever an User logs in who is of the type 'typeA', he does not have the option of seeing the link 'Rate'. But I also do not want him to be able to access that page by the link 'http://localhost:3000/rate”。如果他试图通过 link 访问它,我想将他注销并让他重新登录。
我通过在我的控制器中使用一段代码和 'rate'.
的特定方法来管理这个
def rate
if current_user.type == "typeA"
sign_out(current_user)
redirect_to new_user_session_path
else
#Code for User of typeB
end
end
这是可行的,但我想知道是否可以使用 before_filter :authenticate_user! 或其他方式以更好的方式完成此操作
现在我的 before_filter 部分看起来像这样
before_filter :authenticate_user!, except: [:index, :show]
有什么方法可以更改上面的代码来实现该功能。
P.S:如果我使用了角色或其他 gem,例如 CanCan/Pundit,也许这可以做得更好,但我没有太多时间提交我的项目,所以我不想现在就开始吧。
试试这个:
class ApplicationController
before_action :authenticate_user!
def authorize_user!
if current_user.type == "typeA"
sign_out(current_user)
redirect_to new_user_session_path
end
end
end
使用您的控制器:
class SomeController < ApplicationController
before_action :authorize_user!, except: [:index, :show]
def top_secret
...
end
end
我相信如果 before_action(before_filter 的新名称)呈现或重定向,将不会处理该操作。
您可以在您想要限制访问的控制器上添加另一个 before_filter
以确认您的 STI 用户类型,而无需覆盖设备的 authenticate_user!
过滤器。
application_controller.rb
class ApplicationController < ActionController::Base
def confirm_user_type(user_type)
redirect_to new_user_session_path unless current_user.is_a?(user_type)
end
end
pages_controller.rb
class PagesController < ApplicationController
# must be authenticated to access
before_filter :authenticate_user!
# must be user of TypeA to access
before_filter { |c| c.confirm_user_type(TypeA) }
def rate
...
end
end
然后,您可以对 STI 用户 type: 'TypeB'
使用相同的过滤器 before_filter { |c| c.confirm_user_type(TypeB) }
我希望我的 ruby 在 rails Web 应用程序中的一页无法被我的一种 STI 模型类型访问。我有两个模型 typeA 和 typeB 继承自 User。我已经使用 User table 中的列类型来实现 STI。我正在为用户会话使用 Devise gem。我想要一个网页“http://localhost:3000/rate' inaccessible to my typeA User. Whenever an User logs in who is of the type 'typeA', he does not have the option of seeing the link 'Rate'. But I also do not want him to be able to access that page by the link 'http://localhost:3000/rate”。如果他试图通过 link 访问它,我想将他注销并让他重新登录。 我通过在我的控制器中使用一段代码和 'rate'.
的特定方法来管理这个def rate
if current_user.type == "typeA"
sign_out(current_user)
redirect_to new_user_session_path
else
#Code for User of typeB
end
end
这是可行的,但我想知道是否可以使用 before_filter :authenticate_user! 或其他方式以更好的方式完成此操作 现在我的 before_filter 部分看起来像这样
before_filter :authenticate_user!, except: [:index, :show]
有什么方法可以更改上面的代码来实现该功能。
P.S:如果我使用了角色或其他 gem,例如 CanCan/Pundit,也许这可以做得更好,但我没有太多时间提交我的项目,所以我不想现在就开始吧。
试试这个:
class ApplicationController
before_action :authenticate_user!
def authorize_user!
if current_user.type == "typeA"
sign_out(current_user)
redirect_to new_user_session_path
end
end
end
使用您的控制器:
class SomeController < ApplicationController
before_action :authorize_user!, except: [:index, :show]
def top_secret
...
end
end
我相信如果 before_action(before_filter 的新名称)呈现或重定向,将不会处理该操作。
您可以在您想要限制访问的控制器上添加另一个 before_filter
以确认您的 STI 用户类型,而无需覆盖设备的 authenticate_user!
过滤器。
application_controller.rb
class ApplicationController < ActionController::Base
def confirm_user_type(user_type)
redirect_to new_user_session_path unless current_user.is_a?(user_type)
end
end
pages_controller.rb
class PagesController < ApplicationController
# must be authenticated to access
before_filter :authenticate_user!
# must be user of TypeA to access
before_filter { |c| c.confirm_user_type(TypeA) }
def rate
...
end
end
然后,您可以对 STI 用户 type: 'TypeB'
before_filter { |c| c.confirm_user_type(TypeB) }