授予 MySQL 中单个字段的权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16674715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:42:09  来源:igfitidea点击:

Granting permission to individual fields in MySQL

mysqlpermissions

提问by Tanoshimi

I have MySQL Database and have several tables in it. One, of course, is the users table for storing the username and passwords (which also has the rest of their information). For all the tables in the schema, I want to be able to grant users (or groups) permission to individual fields. Maybe an example will help:

我有 MySQL 数据库,里面有几个表。一个,当然,是用于存储用户名和密码的用户表(也有他们的其余信息)。对于架构中的所有表,我希望能够授予用户(或组)对各个字段的权限。也许一个例子会有所帮助:

There;'s a table called ACCOUNTS. In this table, the user fills out (and keeps up to date) all the data about their company (name, address, POC, etc). But I also want to have fields attached to this table that are read only for those users, such as LastPaymentDate (can't have them changing that!). Further, among those users, permissions differ. For example, the admin/superuser can change the name and address of the company, but standard users should not.

有一个叫做 ACCOUNTS 的表。在此表中,用户填写(并保持最新)有关其公司的所有数据(名称、地址、POC 等)。但我也希望将字段附加到该表中,这些字段仅供这些用户读取,例如 LastPaymentDate(不能让他们更改!)。此外,在这些用户之间,权限不同。例如,管理员/超级用户可以更改公司的名称和地址,但标准用户不应该。

I'm thinking this might need to be done by making several Views for the table, one for each level of permission (group). I'm relatively new to MySQL, so I don't know if this is the best way. I can also see a lookup table that says which fields is allowed to view/edit.

我认为这可能需要通过为表创建多个视图来完成,每个权限级别(组)一个。我对 MySQL 比较陌生,所以我不知道这是否是最好的方法。我还可以看到一个查找表,说明允许查看/编辑哪些字段。

My initial thought was to include in the comments (or the name of the field) a value from 0-5, and then the user would have a permission level (0-can't see; 1-Read only; 2-Read-write; 3-(not used); 4-(not used); 5-Edit/Delete the field itself.

我最初的想法是在评论(或字段名称)中包含一个 0-5 的值,然后用户将拥有一个权限级别(0-看不到;1-只读;2-读取-写;3-(未使用);4-(未使用);5-编辑/删除字段本身。

Any suggestions? Views? Lookup table to determine which fields to display? Again, it'd not for the whole table, for each column within a table.

有什么建议?观看次数?查找表以确定要显示哪些字段?同样,它不是针对整个表,而是针对表中的每一列。

回答by user4035

You can GRANT the rights to individual columns to a user, using this code:

您可以使用以下代码将单个列的权限授予用户:

GRANT SELECT (col1), INSERT (col1,col2) ON mydb.mytbl TO 'someuser'@'somehost';

Example taken from here:

取自此处的示例:

http://dev.mysql.com/doc/refman/5.1/en/grant.html

http://dev.mysql.com/doc/refman/5.1/en/grant.html

Also there is no support for groups of users or SQL ROLES (which are groups of privileges) in MySQL.

MySQL 也不支持用户组或 SQL ROLES(权限组)。