是否可以在单个 Rails 应用程序中使用域或子域的 cookie 用户会话

发布于 2024-10-11 07:23:27 字数 585 浏览 2 评论 0原文

我想在多子域应用程序中登录管理员和简单用户。管理员应该有权访问所有子域(即他们的会话应该存储在所有子域都可用的 cookie 中)。用户应该只能访问他们登录的子域。

例如:

admin“管理员”在域 URL (mydomain.com) 或任何子域(例如 abc.mydomain)登录.com)并在所有子域中保持登录状态。因此,他(“管理员”)可以访问 efg.mydomain.com、abc.mydomain.com、mydomain.com 等。

用户“simpleuser”在 abc.mydomain.com 登录。他(“simpleuser”)只能访问此子域(即 cookie 仅与 abc.mydomain.com 子域相关)

我知道可以选择将 cookie 关联到子域或域,但我想在同一个应用程序中混合这种行为。有谁知道一种方法吗?

这是针对 Rails 3 的,

我不想通过我的应用程序(即通过 cancan 或声明性授权)限制对子域的访问。理想情况下,应通过 cookie 在子域级别阻止访问。我相信这是一种更安全的方法,因为它不必再设置一个软件来控制对站点的访问。

I want in a multi-subdomain application to login admins and simple users. The admins should have access to all the subdomains (i.e. their session should be stored in a cookie that is available to all the subdomains). The users should have access only to the subdomain which they logged in.

so for example:

admin "administrator" signs in either at the domain url (mydomain.com) or at any subdomain (e.g. abc.mydomain.com) and remains logged in all the subdomains. He ("administrator") can therefore access efg.mydomain.com, abc.mydomain.com, mydomain.com etc. etc.

user "simpleuser" signs in at abc.mydomain.com. He ("simpleuser") can only access this subdomain (i.e. the cookie relates only to the abc.mydomain.com subdomain)

I know that it is possible to choose to associate to cookie either to subdomain or the domain, but I would like to mix this behavior in the same application. Does anyone know of an approach?

This is for Rails 3

I would not want to limit access to the subdomain through my application ( i.e. through cancan or declarative authorization ). Ideally access should be blocked at the subdomain level through the cookie. I believe that this is a safer approach since it does away with having to set up one more piece of software for controlling access to the site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-10-18 07:23:27

您希望 cookie 在所有子域中都有效,但使用授权来限制用户授予对子域的访问权限。

cancan 这样的东西可以在这里工作——如果你是管理员,你可以访问除用户之外的所有内容仅当您是该用户时才能访问。

在你的application_controller中的

before_filter :authorize_me

def authorize_me
  @user = User.find_by_subdomain(request.subdomain)
  authorize! :administer, @user
end

capability.rb中
班级能力
包括 CanCan::Ability

  def initialize(user)
    user ||= User.new

    can :manage, :all if user.admin?

    can :administer, User do |new_user|
      new_user.id == user.id
    end
  end
end

You'll want to have the cookie valid across all subdomains, but use authorization to restrict users to give access to the subdomain.

Something like cancan could work here -- where if you are an admin, you can access everything, but a user can access only if you are that user.

in your application_controller

before_filter :authorize_me

def authorize_me
  @user = User.find_by_subdomain(request.subdomain)
  authorize! :administer, @user
end

In ability.rb
class Ability
include CanCan::Ability

  def initialize(user)
    user ||= User.new

    can :manage, :all if user.admin?

    can :administer, User do |new_user|
      new_user.id == user.id
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文