Rails 中的 method_missing 和 Association_proxy

发布于 2024-11-07 17:08:07 字数 1026 浏览 0 评论 0原文

所以,这是我的问题。我目前正在为 Rails 站点构建一个简单的身份验证系统。我为此设置了 3 个类:Person、Session 和 Role。在我的 Person 模型中,我定义了 method_missing 来根据 本指南

在我的 application_controller 中,我有一些逻辑来处理登录和注销,其结果通过以下方式为我提供当前登录的用户: @user = @application_session.person 其中 @application_session 是当前会话

现在在我的一个控制器中,我不希望任何人能够执行任何操作,除非他们是管理员,所以我包括: before_filter @user.is_an_admin?

这会引发 NoMethodError,即使我在模型中定义了 method_missing 。我尝试定义 is_an_admin?,让它始终返回 true 作为测试,这很有效。

根据 这个问题,我认为问题可能与代理有关协会。当我跑步时: 放置@user.proxy_owner 我得到一个会话对象,因为每个用户(Person)可以有许多会话,并且我从当前会话中获取了我的用户(Person)。

我很困惑为什么 @user.is_an_admin? 没有调用我的 Person 控制器中的 method_missing 方法。如果您需要更多信息或代码片段,请告诉我。

我在 Ruby 1.9 上使用 Rails 3

So, here's my problem. I currently am building a simple authentication system for a rails site. I have 3 classes for this: Person, Session, and Role. In my Person model I have defined method_missing to dynamically capture roles according to this guide.

In my application_controller I have some logic to deal with logins and log-outs, the result of which gives me the currently logged in user via:
@user = @application_session.person
Where @application_session is the current session

Now in one of my controllers, I don't want anyone to be able to do anything unless they are an admin, so I included:
before_filter @user.is_an_admin?

This raises a NoMethodError, even though I have method_missing defined in my model. I tried defining is_an_admin?, having it always return true as a test, and that works.

According to this question, I think the problem might have something to do with proxy associations. When I run:
puts @user.proxy_owner
I get a session object, since each user (Person) can have many sessions, and I got my user (Person) from the current session.

I am very confused why @user.is_an_admin? is not calling the method_missing method in my Person controller. Please let me know if you need more information or code snippets.

I am using Rails 3 on Ruby 1.9

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

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

发布评论

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

评论(3

旧伤还要旧人安 2024-11-14 17:08:07

我认为 method_missing 对于这样的任务来说是多余的。

现在,如果您有属于 User 的 Session 类,那么您可以这样:


class Session
  belongs_to :user, :extend => PermissionMixin
end

class User
  include PermissionMixin
end

module PermissionMixin
  def admin?
    if cond
      true
    else
     false
    end
  end
end

PS Check cancan,也许它会更好地满足您的需求。

I'd consider a method_missing an overkill for such task.

Now, if you have Session class, which belongs_to User, then you can have this:


class Session
  belongs_to :user, :extend => PermissionMixin
end

class User
  include PermissionMixin
end

module PermissionMixin
  def admin?
    if cond
      true
    else
     false
    end
  end
end

P.S. Check cancan, perhaps it'll suit your needs better.

我很坚强 2024-11-14 17:08:07

我在系统中使用类似的权限检查来检查 User >角色>权限关联:
User.current_user.can_sysadmin?

在我的控制器中,我必须使用:
User.current_user.send('can_sysadmin?')

这可能也适合您。

I use a similar permissions check in my system to check the User > Role > Permissions association:
User.current_user.can_sysadmin?

In my controllers I have to instead use:
User.current_user.send('can_sysadmin?')

This may work for you as well.

遥远的她 2024-11-14 17:08:07

我通过将 method_missing 方法移动到我的 application_controller.rb 解决了这个问题。我稍微改变了方法的逻辑来检查用户,如果找到,则动态检查角色。如果事情不合规矩,我会将方法重定向到 root_url ,或者在用户与请求的角色匹配时返回 true。

最后,在我的报告控制器中,我使用了 before_filter :is_an_admin? 并得到了我想要的结果。但是,我仍然不清楚为什么 method_missing 必须在我的应用程序控制器中定义,而不是直接在 Person (又名 @user)模型中定义?

I have solved this by moving the method_missing method to my application_controller.rb. I change the logic of the method a little to check for a user, and if found, dynamically check the role. If things were not kosher, I had the method redirect to root_url or return true if the user matched the requested roles.

Finally, in my reports controller, I used before_filter :is_an_admin? and got my desired results. However, I am still unclear as to why method_missing had to be defined in my application controller as opposed to directly in the Person (aka @user) model?

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