Rails STI for User 模型,具有关联和身份验证

发布于 2025-01-04 21:59:10 字数 292 浏览 1 评论 0原文

我正在开发一个网站,医生可以在其中管理患者、预约等。 一名医生可以有多名助手来帮助他们完成这些活动,并且一名助手可以属于多名医生。

医生和助理都应该能够从同一个登录表单登录,但由于显而易见的原因,他们根据自己的角色拥有不同的权限和关联,例如患者与医生相关联,而不是与助理相关联。

您建议如何解决这个问题的最佳方法?我正在考虑将 CanCan 用于授权部分,也许将 STI 用于用户模型,医生和助理可以从该模型继承身份验证部分,但是 STI 可以处理这两者之间的 HABTM 以及每个模型可能具有的其他关联吗?

预先非常感谢

I am developing a site where doctors can manage their patients, appointments, etc.
A doctor can have multiple assistants which help them with these activities, and an assistant can belong to multiple doctors.

Both doctors and assistants should be able to sign in from the same login form but for obvious reasons they have different permissions and associations depending on their role, for example patients are associated to the doctors, not to the assistant.

How you suggest would be the best way to approach this? I am contemplating CanCan for the authorization part, and maybe STI for a User model which Doctor and Assistant can inherit from for the authentication part, but can STI handle the HABTM between these 2, plus the other associations each model might have?

Thanks a lot in advance

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

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

发布评论

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

评论(1

红尘作伴 2025-01-11 21:59:10

这应该可以解决问题。

只需确保使用“类型”来设置用户是谁。然后,您可以使用以下内容:

current_user.is_doctor?

确定对区域的访问权限。

module User

  def is_doctor?
   true if self.type == Doctor
  end

  def is_assistant?
   true if self.type == Assistant
  end
end


module Doctor < User
  has_and_belongs_to_many :assistants, :through => "assistant_assignments"
end


module Assistant < User
  has_and_belongs_to_many :doctors, :through => "assistant_assignments"
end

如果您需要更多信息,请大声喊叫。

This should do the trick.

Just make sure you use 'type' to set which the user is. You can then use things like:

current_user.is_doctor?

To determine access to areas.

module User

  def is_doctor?
   true if self.type == Doctor
  end

  def is_assistant?
   true if self.type == Assistant
  end
end


module Doctor < User
  has_and_belongs_to_many :assistants, :through => "assistant_assignments"
end


module Assistant < User
  has_and_belongs_to_many :doctors, :through => "assistant_assignments"
end

Shout if you need more info.

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