MySQL User Privileges - Restrict Users to some tables - mysql

I have created a database with several tables, such as user, booking etc.
I have allocated privileges using the User tab within phpmyAdmin, wherein, Users are permitted View and Add privileges, Administrators have View, Add and Amend privileges and Managers have ALL privileges including Delete.
Using the MySQL database, is it possible to do some restrictions as below:
a) Restrict the User to View and Add to only certain tables.
b) Allow the User to view records solely for themselves and not those of any another user.
If so How?

You can give privileges to some users to some tables using mysql GRANT as below
GRANT SELECT, INSERT, UPDATE ON db.booking TO 'someuser'#'somehost';
For all privileges use ALL instead of SELECT, UPDATE etc.
GRANT ALL ON db.booking TO 'someuser'#'somehost';
For documentation please go through mysql GRANT

Related

Can a user have GRANT PERMISSION without having ALL PRIVILEGES?

I want to create a user who can register other new users, but I don't want him to have ALL PRIVILEGES, it looks like I'm doing something wrong here.
I've tried the following
GRANT SELECT, INSERT, UPDATE ON database.* TO 'USER' IDENTIFIED BY '123456' WITH GRANT OPTION
But when I login with this USER account and try to give privileges to another user MySQL/MariaDB return : You are not allowed to create a user with GRANT
Is it possible to have a user who can only select/insert/update on a single database to give those same privileges to new users?
Short answer: Yes. Not just to new accounts, but any account that exists.
Based on the documentation from MySQL:
The optional WITH clause is used to enable a user to grant privileges to other users. The WITH GRANT OPTION clause gives the user the ability to give to other users any privileges the user has at the specified privilege level.
[...]
Be aware that when you grant a user the GRANT OPTION privilege at a particular privilege level, any privileges the user possesses (or may be given in the future) at that level can also be granted by that user to other users.
So, as the documentation notes, an account that has SELECT, INSERT, and UPDATE privileges with the option to GRANT, can provide those same permissions to other accounts.
However, there are some important details in the documentation to note:
Be careful to whom you give the GRANT OPTION privilege because two users with different privileges may be able to combine privileges!
Suppose that you grant a user the INSERT privilege on a database. If you then grant the SELECT privilege on the database and specify WITH GRANT OPTION, that user can give to other users not only the SELECT privilege, but also INSERT. If you then grant the UPDATE privilege to the user on the database, the user can grant INSERT, SELECT, and UPDATE.
If you are going to allow other accounts to administer privileges, such as a junior DBA or a member of HR, consider reviewing your database permissions auditing process to ensure accidental grants are identified and corrected.

Give user permission to drop and create specific databases

This question refers to MySQL and/or MariaDB specifically.
Is it possible to permit certain users to drop/create one (or more) specific databases, but not all databases? By this, I do not mean how to give users all privileges for certain specific databases with a query like:
GRANT ALL PRIVILEGES ON `example-db`.* TO 'example-user'#'localhost';
I've found some methods on how to do this for MSSQL here and a similar post for PostgreSQL.
grant privileges separately for a user account in MySQL.
When specifying the database name and table name, separate them with a. (period) and no spaces. This will give the root user fine-grain control over certain data.
Also, replace the PERMISSION_TYPE value with the kind of access you want to grant to your new user account.
Here are the most used commands in MySQL:
CREATE — enable users to create a database or table
SELECT — permit users to retrieve data
INSERT — let users add new entries in tables
UPDATE — allow users to modify existing entries in tables
DELETE — enable users to erase table entries
DROP — let users delete entire database tables
NOTE: Using the ALL PRIVILEGES permission type from before will allow
all of the permissions listed above.
To use any of these options, simply replace PERMISSION_TYPE with the appropriate keyword. To apply multiple privileges, separate them with a comma. For example, we can assign CREATE and SELECT to our non-root MySQL user account with this command:
GRANT CREATE, SELECT ON * . * TO 'user_name'#'localhost';
Sometimes, you might come across a situation where you need to revoke given privileges from a user. You can do so by entering:
REVOKE PERMISSION_TYPE ON database_name.table_name FROM ‘user_name’#‘localhost’;
For example, to withdraw all privileges for our non-root user we should use:
REVOKE ALL PRIVILEGES ON * . * FROM 'user_name'#'localhost';
Finally, you can entirely delete an existing user account by using the following command:
DROP USER ‘user_name’#‘localhost’;
Don't forget to FLUSH PRIVILEGES;
In order to find what privileges have already been granted to a MySQL user, you can use the SHOW GRANTS command:
SHOW GRANTS FOR 'user_name'#'localhost';
Let’s start by making a new user within the MySQL shell:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
To provide a specific user with permission, you can use this framework:
GRANT type_of_permission ON database_name.table_name TO 'username'#'localhost'
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;

What are the basic privileges needed to acces user_privileges table from information_schema database

I want to know the minimum set of privileges requires to be able to see all the entries inside user_privileges table as a root user would see without granting root privileges.
If I grant select privileges on all the tables of mysql database, I am able to see all the entries but I need to know the minimum set of table that I would need to grant select privileges on, for all the information.
Basically, I want to extract out number of privileges granted to each user present in the database with minimum privileges.

Is there a specific MySQL grant permission to allow user to create and drop other users?

I was looking through the MySQL documentation for information on how to grant a user the ability to create and drop other users. The examples given are for creating and dropping databases and tables.
Does the GRANT ALL PRIVILEGES permission assigned to a user also implicitly mean that they can create and drop other users? If so, is there any other GRANT privilege that allows this without automatically making the user a superuser? Or would the process be first making them a superuser and then revoking specific permissions?
To use DROP USER, you must have the global CREATE USER privilege or the DELETE privilege for the mysql database.
http://dev.mysql.com/doc/refman/5.7/en/drop-user.html
To use the first REVOKE syntax, you must have the GRANT OPTION privilege, and you must have the privileges that you are revoking.
http://dev.mysql.com/doc/refman/5.7/en/revoke.html
The GRANT OPTION privilege is not part of GRANT ALL PRIVILEGES .... It has to be specified at the end of the GRANT statement, ... WITH GRANT OPTION. This privilege allows a user possessing it to grant any privileges that the user possesses... to other users. Or to revoke them. (You can't grant a privilege that you, yourself, lack... or revoke one, as noted above.)

Allow MySQL user with limited database access to create more users with similar access

I know that I can create a user with the create user privileges like so:
create user primary_user identified by 'pass';
grant all on *.* to primary_user with grant option;
This will in turn allow me to create users whilst logged in with primary_user.
Now let's assume I have a subset of databases, all with prefix abc_, and that I want my primary_user to only be able to access these databases.
Then the grant query above would look like so:
grant all on `abc_%`.* to `primary_user` with grant option;
The problem now, however, is that when I log in with my primary_user and try to create a secondary_user, I get the following error:
ERROR 1227 (42000): Access denied; you need (at least one of) the
CREATE USER privilege(s) for this operation
TL;DR
Basically what I want is primary_user to only be able to access databases with the abc_ prefix, and to also be able to create secondary users that in turn also only have access to databases with an abc_ prefix.
Is this possible? Or is my only option to create the secondary users with a user account that has create user privileges on *.*?
As mentioned in MySQL Documentation:
To use CREATE USER, you must have the global CREATE USER privilege or
the INSERT privilege for the mysql database.
Thus, users with privileges in specific databases cannot create users. Ofcourse, since you have with grant option, your primary_user can grant all database level privileges to other already created users (for the abc_ databases).
Note #1: Here you can find an interesting table with the various privileges and the different levels that they can be granted.
Note #2: Be extra cautious when giving GRANT OPTIONS to non-admin users because they can modify the privileges of other users which can lead to chaos. The Open Web Application Security Project states:
[Grant privilege]... should be appropriately restricted to the DBA and Data (Table)
owners. Give specific permissions on an as needed basis and use
different logins for different purposes.