登录时如何处理不同类型的用户? [英] How to handle different types of users when loggin in?

查看:144
本文介绍了登录时如何处理不同类型的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在构建的应用程序中,我有一个 users 表。用户代表公司的员工。我还实施了角色/权限系统。权限分配给角色(工作人员,经理,主管等),角色分配给用户。权限禁止在应用程序中使用某些操作。

In an application I've been building, I have a users table. The users represent a firm's staff. I also implemented a role/permission system. Permissions are assigned to roles (staff, manager, director, etc.), and roles are assigned to users. Permissions prohibit the use of certain actions in the app.

现在,我要介绍另一种用户类型:客户(也可以登录应用程序)。客户字段与用户字段不同,因此我无法将客户信息存储在 users 表中(并为客户创建角色)。

Now, I'm introducing another user type: customer (will also be able to log into the app). The customer fields are different from the user fields, so I can't store the customer information in users table (and create a role for customers).

(通常,只需为客户创建客户角色并将客户存储在 users 表中就可以了,但是由于字段不同,我不认为这是一个选择。)

(Normally, just creating a customer role for the customers and store the custumers in the users table would be fine, but since the fields are different, I don't think that's an option.)

首先,我考虑过为客户创建一个单独的表,但是我我不确定是否可以,因为有人尝试登录该应用程序时,我必须检查两个表( users customers )作为登录凭据。如果引入第三种用户类型怎么办?检查三个表?

First, I thought about creating a seperate table for the customers, but I'm not sure if that's okay, because when someone tries to log into the app, I have to check two tables (users and customers) for the log-in credentials. What if I introduce a third user type? Check three tables? That doesn't seem practical/efficient.

所以我考虑过将登录凭据与用户分开,最后得到了三个表:

So I thought about seperating the log-in credentials from users, and ended up with three tables:


  • 个用户将保留登录凭据

  • 职员将保存职员元数据/个人资料信息

  • 顾客将保存客户信息(与 staff

  • users will hold log-in credentials
  • staff will hold staff meta/profile information
  • customers will hold customer information (same as staff)

这样,我可以介绍许多不同类型的用户。而且,如果我知道自己要寻找的东西,就可以得到记录。例如,说我想获取/查询职员,我可以这样做:

This way, I can introduce many different types of users. And if I know what I'm looking for, I can get the record. For example, say I want to get/query a staff, I can just do:

SELECT * FROM staff
JOIN users USING (user_id);

问题是如何查询个用户当我不知道要寻找什么时?我的意思是,登录的用户可以是员工,客户等。我需要执行以下操作:

The problem is how do I query users when I don't know what I'm looking for? I mean, the logged user can be a staff, customer, etc. I need to do something like this:

SELECT * FROM users
JOIN [specific_table_name] USING (user_id);

我怎么知道刚刚登录的用户类型?我可以将用户类型(目标表名称?)存储在 users 中,但是对(单个)查询有帮助吗?我的意思是,在 users 中找到用户(使用凭据),然后从另一个表( staff )中加入用户信息,客户等)?

How do I know which type of user just logged in? I could store the user type (the target table name?) in users, but will it help in a (single) query? I mean, find the user in users (using credentials), and then join the user information from another table (staff, customers, etc.)?

当前,我正在考虑进行两个查询。第一个是获取用户(凭据)记录,第二个是获取用户(例如个人资料)信息(使用用户记录中的字段 type )。

Currently, I'm thinking about doing two queries. First is to get the user (credential) record, and the second is to get user (say, profile) information (using a field type from user record).

当然,我会在PHP中执行此操作。例如(非真实代码):

Of course I'll be doing this in PHP. For example (not real code):

$email = "someone@example.com";
$user  = get_user($email); // SELECT * FROM users WHERE email = "someone@example.com"

switch ($user["type"]) {
    case "staff":
        $user = get_staff($email); // SELECT * FROM staff JOIN users USING (user_id) WHERE email = "someone@example.com"
        break;
    case "customer":
        $user = get_customer($email);
        break;
    // ...
}

// how it's done doesn't really matter. the thing is "type" needs to be checked to get the corresponding user info

这是最好的方法处理这个(事情?有没有办法在SQL中进行查询(不求助于PHP)?像 JOIN WHERE 之后一样吗?还是在一个查询中进行两个查询(保存第一个查询结果,并将第一个结果中的列值用作第二个查询中的表名)?

Is this best way to handle this? Is there a way to make the queries in SQL (without resorting to PHP)? Like JOIN after WHERE? Or make two queries in one (save the first query result, and use a column value from the first result as a table name in the second query)?

提及表:

我仍在研究中,发现使用表进行的操作称为(?)类表继承。对于与非登录相关的实体来说(从孩子到父母;例如,员工->用户)似乎很聪明,但是相反(对于孩子的父母,例如,用户-> staff | customer等)则显得很麻烦。既然我在走动时就想到了这些东西,所以我现在就陷入了困境。

一个解决方案是我在打字时就想到的使用特定于用户类型的不同登录表单/页面。表单/页面可以让我事先知道用户类型,但是我宁愿不使用这种方法。因此,适用于所有用户的单一登录表单。

我正在呼叫个用户作为基础/父表,而 stuff customers 等作为子表,因为首先插入发生在个用户,子表使用来自个用户 user_id

I'm calling users as the base/parent table, and the stuff, customers, etc. as the child tables, because first insert happens at users, and child tables use user_id from users.

推荐答案

我将设置 all 个用户的登录信息并通过电子邮件将其发送到父表(客户和员工)和两个带有外键ID的表(用于其他数据),并按您的建议使用users.type。也可以通过一个查询来完成,并且不需要 Switch 条件:

I would set all users login info and email to the parent table (customers and staff) and two tables with foreign id key for other data, with users.type as you suggested. It could be done with one query, too and Switch condition is not needed:

SELECT users.email,users.type,staff.field2,customers.field3 from users 
LEFT JOIN staff ON(users.type='staff' and users.id=staff.uid) 
LEFT JOIN customers ON(users.type='customers' AND users.id=customers.uid);

这篇关于登录时如何处理不同类型的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆