我有多个帐户,每个帐户都有自己的用户,并且希望每个帐户的用户电子邮件地址都是唯一的

发布于 2024-09-28 10:27:30 字数 1078 浏览 6 评论 0原文

我正在使用 Rails 构建一个 Web 应用程序,为每个企业提供自己的帐户和子域,类似于 basecamp(和其他 37 个信号应用程序)lighthouseapp.com 等。

现在我知道有很多关于如何在 Rails 中创建子域的不错的教程,甚至还有 subdomain-fu 插件。

问题是我想使用用户的电子邮件地址作为他们的登录名,这意味着它必须是唯一的,但我只希望它对于每个帐户都是唯一的,而不是在所有帐户中都是唯一的。

我正在使用 devise 进行身份验证,并且创建了一个代表帐户的“帐户”模型,以及一个代表该帐户用户的用户模型。 Devise 在用户模型上运行,它似乎要求每个用户都有一个唯一的电子邮件地址。

我把问题说得够清楚了吗?我很乐意提供澄清。

有人能指出我正确的方向吗?


向 Tim Q 澄清一些细节

  • :一个人可以拥有多少个账户 业务有吗?
  • 答:每个企业都会有一个帐户。例如“Springfield Mathsgrins”

  • 问:子域名和帐户之间是否存在一对一的关联?

  • 答:是的,例如 springfield-maths-grinds.myapp.com

  • 问:“用户”是否对应于自然人,还是账户中的角色?

  • 答:我应该更好地解释这一点。我实际上有两个独立的模型,例如导师和学生。每个帐户都有多个导师和学生。

企业、帐户、子域 企业与帐户实际上是同一件事。可以说是“企业帐户”。 我使用子域来访问每个“企业帐户”

登录信息、用户、电子邮件地址 我应该澄清一下,我有两种类型的用户,它们由不同的模型表示,即导师和学生。这些人需要登录应用程序,我想使用电子邮件地址作为他们的登录名,而不是用户名。

希望事情能够得到解决,而不是让事情变得更糟。

我的实际问题如下, 如果有人要为“斯普林菲尔德物理研究”设置一个企业帐户,并且“斯普林菲尔德数学研究”的一名学生尝试注册,那么他们将无法这样做,因为电子邮件地址已被使用并且需要是独一无二的。我想知道如何在企业帐户中确定学生和导师的范围。

I am using rails to build a web app which provides each business with its own account and subdomain, Similar to how basecamp (and other 37 signals apps) lighthouseapp.com, and so on.

Now I know that there are lots of decent tutorials on how to do subdomains in rails, and even the subdomain-fu plugin.

The thing is that I want to use the users email address as their login, which means it needs to be unique, however I only want it to be unique to each account rather than across all accounts.

I am using devise for authentication, and I have created an "Accounts" model which represents the account, and a user model to represent the users for that account. Devise operates on the user model, and it would appear that it would require each user to have a unique email address.

Have I stated the issue clearly enough? I can gladly provide clarification.

Can anyone point me in the right direction?


Clarifying some details for Tim

  • Q: How many accounts can an individual
    business have?
  • A: Each individual business would have one account. Such as "Springfield Maths grinds"

  • Q: Is there a one-to-one correlation between subdomains and accounts?

  • A: Yes, such as springfield-maths-grinds.myapp.com

  • Q: Does a "user" correspond to a physical person, or a role within an account?

  • A: I should have explained this better. I actually have two separate models, e.g. tutor and student. Each account would have multiple tutors and students.

businesses, accounts, subdomains
businesses & accounts are effectively the same thing. A 'business account' so to speak.
I am using subdomains to access each 'business account'

logins, users, email addresses
I should clarify that I have two types of user which are represented by separate models, these are tutor and student. These will need to login to the app and I want to use an email address as their login, instead of a username.

Hopefully that clears things up rather than making it worse.

My actual problem is as follows,
If someone were to setup a business account for "Springfield physics grinds" and one of the students of "Springfield maths grinds" tried to sign-up, then they would be unable to do so because the email address is already in use and needs to be unique. I want to know how to scope the students and tutors within a business account.

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

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

发布评论

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

评论(2

雨的味道风的声音 2024-10-05 10:27:30

有两件事你需要考虑。数据完整性和身份验证。

Rails 允许您将唯一性范围限定为一列或多列。通过范围界定,您可以在数据库的限制是每个帐户只能有一个。我已经包含了相应的 SQL 可能看起来的样子,以演示其工作原理。

# Without scoping
validates :email, :uniqueness => true
SELECT 1 FROM `users` WHERE `users`.email = '[email protected]'

# With scoping
validates :email, :uniqueness => true, :scope => :account_id
SELECT 1 FROM `users` WHERE `users`.account_id = 1 AND `users`.email = '[email protected]'

我假设您使用的是 Rails 3。如果您使用的是 Rails 2,则可以将相同的选项传递给 validates_uniqueness_of

为了进行身份验证,您需要找到该帐户,确保用户属于到该帐户并检查其密码是否匹配。当您向用户提供登录表单时,您已经知道他们针对哪个帐户进行身份验证。你的动作可能是这样的。

def create
  @account = Account.find_by_subdomain(params[:subdomain])
  @user    = @account.users.find_by_email(params[:user][:email])
  if @user.authenticate(params[:user][:password])
    redirect_to dashboard_path
  else
    render :new
  end
end

There's two things you need to think about. Data integrity and authentication.

Rails lets you scope uniqueness to one or more columns. With scoping you could have multiple [email protected] e-mail addresses in the database with the constraint of only one per account. I've included what the corresponding SQL might look this to demonstrate how this works.

# Without scoping
validates :email, :uniqueness => true
SELECT 1 FROM `users` WHERE `users`.email = '[email protected]'

# With scoping
validates :email, :uniqueness => true, :scope => :account_id
SELECT 1 FROM `users` WHERE `users`.account_id = 1 AND `users`.email = '[email protected]'

I'm assuming you're using Rails 3. You can pass the same option to validates_uniqueness_of if you're using Rails 2.

For authentication, you'll want to find the account, ensure the user belongs to that account and check that their password matches. When you present the login form to the user, you'll already know which account their authenticating against. Your action might look like this.

def create
  @account = Account.find_by_subdomain(params[:subdomain])
  @user    = @account.users.find_by_email(params[:user][:email])
  if @user.authenticate(params[:user][:password])
    redirect_to dashboard_path
  else
    render :new
  end
end
泅人 2024-10-05 10:27:30

我认为你需要更清楚你在做什么以及你到底在问什么。首先重要的是解释您的需求和数据模型,而不是您正在使用的包。在您的问题中,我们有:

  • 企业
  • 帐户
  • 子域
  • 登录
  • 用户
  • 电子邮件地址

我猜其中一些是相同的东西。因此,也许您可​​以首先概述这些东西如何组合在一起。这里有一些问题:

  • 个体工商户可以有多少个账户?
  • 子域和帐户之间是否存在一对一的关联?
  • “用户”是否对应于自然人,或者帐户中的角色?

一旦你清楚这些东西是如何组合在一起的,并且你可以说清楚,你的问题就会更容易回答。

I think you need to be a little clearer about what you are doing and exactly what you are asking. What's important to start with is explaining your requirements and your data model, rather than what packages you're using. In your question, we have:

  • businesses
  • accounts
  • subdomains
  • logins
  • users
  • email addresses

I'm guessing that some of these are the same things. So perhaps you can start by outlining how these things fit together. Here are some questions:

  • How many accounts can an individual business have?
  • Is there a one-to-one correlation between subdomains and accounts?
  • Does a "user" correspond to a physical person, or a role within an account?

Once you're clear in your mind how these things fit together, and you can make this clear, your question will be substantially easier to answer.

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