Rails3、Authlogic 和authentiates_many 如何编写current_account_session 帮助器方法?

发布于 2024-10-15 10:08:22 字数 1246 浏览 7 评论 0原文

启动信息:

  • 我的系统不使用子域来获取正确的帐户!
  • 我使用 Rails 3.0.x
  • 我使用 authlogic 2.1.6
  • 模型帐户和模型用户
  • cookie 的名称为 account_1_user_credentials,没错!

模型 Account.rb

class Account < ActiveRecord::Base
  authenticates_many :user_sessions, :scope_cookies => true
  has_many :users
end

模型 User.rb

class User < ActiveRecord::Base
  acts_as_authentic do |c|
    c.validations_scope = :account_id
  end
  belongs_to :account
  ...
end

问题:如何对应用程序帮助器方法进行编码?

Authlogic的文档仅显示了没有authentiates_many和scope_cookies的正常实现:

class ApplicationController
  helper_method :current_user_session, :current_user

  private
    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end
 end

但是session_controller.rb(设置current_account_session)application_controller.rb(def current_account_session的实现...结束)< /strong> 看起来像什么?

Startup info:

  • My system don't user subdomains to get the right account!
  • I use Rails 3.0.x
  • I use authlogic 2.1.6
  • Model Account and model User
  • The cookie is present with name e.g. account_1_user_credentials and thats right!

Model Account.rb

class Account < ActiveRecord::Base
  authenticates_many :user_sessions, :scope_cookies => true
  has_many :users
end

Model User.rb

class User < ActiveRecord::Base
  acts_as_authentic do |c|
    c.validations_scope = :account_id
  end
  belongs_to :account
  ...
end

QUESTION: How can I code the application helper methods?

The documentation of Authlogic shows only the normal implementation without authenticates_many with scope_cookies:

class ApplicationController
  helper_method :current_user_session, :current_user

  private
    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end
 end

BUT how does the session_controller.rb (setting current_account_session) and application_controller.rb (implementation of def current_account_session ... end) look like?

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

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

发布评论

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

评论(1

榕城若虚 2024-10-22 10:08:22

如果您的所有用户都获得相同的登录信息,您需要根据 current_user 查找帐户。为此,您不需要在帐户中使用authentiates_many。只需验证您的用户身份,然后获取其帐户即可。

要设置控制器,请查看示例 https://github。 com/binarylogic/authlogic_example/blob/master/app/controllers/user_sessions_controller.rb

注意:您还可以检查视图,...以获得更多灵感。

这将允许您验证用户身份并管理其会话。
登录后,您需要能够获取他的帐户,以便您可以确定每个帐户的其他请求。

要实现此目的,请添加 current_account helper_method,方法是将以下内容添加到 application_controller.rb

class ApplicationController
  helper_method :current_account

  private

    def current_account
      current_user.account
    end
    memoize :current_account
end

不要忘记还添加默认的 current_user 和 current_user_session helper_method。

这样,您始终可以在所有控制器中找到经过身份验证的用户的 current_account。

If all your users get the same signin, you'll need to find the account based on the current_user. For this, you don't need to use authenticates_many in accounts. Just authenticate your user, then get it's account.

To setup your controller, look at the example https://github.com/binarylogic/authlogic_example/blob/master/app/controllers/user_sessions_controller.rb

Note: You can also check the views, ... for more inspiration.

That will allow you to authenticate the user and manage it's session.
Once logged in, you need to be able to get his account, so you can scope other request per account.

To achieve this, add the current_account helper_method, by adding the following to your application_controller.rb

class ApplicationController
  helper_method :current_account

  private

    def current_account
      current_user.account
    end
    memoize :current_account
end

Don't forget to also add the default current_user and current_user_session helper_method.

This way, you can always find the current_account of the authenticated user, in all your controllers.

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