Mysql priviléges - mysql

for user boss12 i associate the right to modify only the name, But I want to know how to associate that renaming only the student with idStu = 12, i.e he can change the name of the student (idStu = 12) but he can not for the student (idStu = 13).
create database gcr;
use gcr;
create table Student(
idStu int primary key,
nom varchar(30),
moyen real)engine=innodb;
insert into Student values(12,'hassen',15.0);
insert into Student values(13,'ouss',12.0);
create user boss12#localhost
identified by 'enig';
create user prof#localhost
identified by 'gcrgcr';
use mysql;
grant select, update,insert on gcr.Student
to prof#localhost with grant option;
grant update (nom) on gcr.Student
to boss12#localhost with grant option;

Impossible in any straightforward way.
There's no row-level security in the very vast majority of RDBMS.
However, theoretically, you can build the additional level that encapsulates the main dataset + additional row level security data (e.g. table(s) that describes the access grants/policies/restrictions for particular rows + reference to these rules from the main data tables) - and then
But that's quite tricky part, let me tell you - even for reads.
And I'm really not sure is it possible for updates in such a case.
The other way is to set a before-update trigger on the given table, then check whatever conditions you want against the requestor's user - and fail the transaction if needed.
But that, honestly, is a dirty hack in your case - heavily data-depending (thus error prone) and hardly maintainable.

Direct solution for you question is not available . According to my understanding you want user boss12 should able to update only some data in table Student.
for ex, update Student set nom='' where idstu =12 should work but update Student set nom='' where idstu =13 should not work .
This is not possible to control via privilege control You can create view on student table where you want to provide update privilege to user idstu.
sql reference for column level privilege

Related

Node.js MySQL - why does UPDATE statement also run SELECT, how can I avoid this for a write-only MySQL user [duplicate]

I want to execute something like this:
UPDATE grades SET status="pass" WHERE recno=123;
However, I want the user account doing the update to have write-only access to the database which means it does not have SELECT access. This causes the WHERE clause to fail.
If necessary, I can actually re-write the entire record, but recno is the primary key, which would cause the write to fail. Is there a way to do
INSERT INTO grades (recno,name,status,...) VALUES (123, 'chemistry', 'pass',...)
ON DUPLICATE KEY UPDATE <everything>;
or is this the wrong approach? Besides, it's not the general solution to the "update a specific field in a specific record" problem.
That is a peculiar way to protect a table. It might make sense in some cases, though usually you want to provide some window of visibility so users are not blind to their effects on data.
To completely implement a write-only database, use stored procedures. The procedures can have full access to the database, and users can be granted access only to stored procedures.
Oooh! I've got it. I'll give my user SELECT privilege for just the recno column.
Leaving the question open though, just in case someone comes up with a better answer.
Create a VIEW and grant the user full access to it. This view should only contain the rows which that you want the user to be able to edit.
A different, and possibly more suitable approach, would be to completely abstract this mechanism away from the DBMS and instead create an API that you allow your users to query.
This is easily achieved by granting the correct level of access:
REVOKE ALL ON TABLE GRADES FROM USER1; -- to clear out all privileges
GRANT INSERT, UPDATE, DELETE ON TABLE GRADES TO USER1;
This will allow USER1 to insert, update and delete, but not select, from the grades table.
Change USER1 to PUBLIC or some group or whatever as you need.
Change the permissions list as you need.
Your situation is a version of a classic example in an IBM database course I used to teach:
Financial staff are allowed to give a % pay rise, but not to see what the pay level is, for employees, so
GRANT UPDATE ON TABLE EMPLOYEE TO ACCT_STAFF; -- an no other privileges
Which allows them to give a 15% pay rise by executing:
UPDATE EMPLOYEE SET PAY = PAY * 1.15 WHERE EMPLOYEE_ID = 666;

MySql: Restrict update permission on one column in one table

I have a table, lets call it student, in a schema called enrollment. Table student has a column called address that I don't want a certain user to update (other permissions are fine such as select, insert). All other columns in that table AND in that schema should have the update privilege.
Is this doable?
You can set privileges on database / table / column. But I really would not try to use MySQL's privilege mechanism at that level. I would instead write application code to decide who can see/change what. This is more flexible in the long run. And more graceful to the user -- instead of getting a cryptic MySQL error message about permissions, the UI would simply not show what should not be shown. For updating, the UI would not even give the user the option.
In my case, I wanted a specific application to be able to update only 1 field (my_field) in only 1 table (table_name) while being able to read the entire database.
I created a special user for that purpose:
CREATE USER 'restrictedUser'#'%' IDENTIFIED BY 'PASSWORD_HERE';
SET PASSWORD FOR 'restrictedUser'#'%' = PASSWORD('PASSWORD_HERE');
GRANT USAGE ON *.* TO 'restrictedUser'#'%';
GRANT SELECT ON DATABASE_NAME.* TO 'restrictedUser'#'%';
GRANT UPDATE (my_field) ON DATABASE_NAME.table_name TO 'restrictedUser'#'%';
Documentation for Column privilege can be found here for mariaDb and here for mysql

Mysql temporary table for different users

Is there any table type that can only be accessed by a particular user?
This table can only be viewed and accessed only by the user who created it
Yes you can.
But you can create a table that has user created column so you can use it on your where condition.
I think the answer is Yes
You can set privileges for that particular table like who can access that table. Like,
SELECT, INSERT, UPDATE, DELETE, .
I have tried in via phpmyadmin.
I was wrong. Yes you can set user specific access for particular table. Its syntax is as follow
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost';
Temporary tables are available under particular session and not accessible by any other session. It will be dropped on session close.
For more information read http://dev.mysql.com/doc/refman/5.5/en/grant.html

MySQL: How do I lock individual rows in a table?

To increase the security of the app I'm working on, I want to be able to protect against any sort of db modification. I know I can lock entire tables from mysql users, but what about individual rows?
Would I still be able to add new rows to the table? maybe for just one particular table, only the SELECT and INSERT commands are allowed?
It sounds like you want to permit inserting new rows and querying existing rows, but you do not want to permit updating or deleting rows. If that is correct, then you'll want to create a MySQL user that has only INSERT and SELECT privileges on the table(s) in question. Do not grant UPDATE and DELETE privileges.
To grant INSERT and SELECT privileges to user foo on my_table:
GRANT SELECT, INSERT ON my_table TO 'foo'#'localhost';
To revoke UPDATE and DELETE privileges from user foo on my_table:
REVOKE UPDATE, DELETE ON my_table FROM 'foo'#'localhost';
This would be "Row level security". MySQL doesn't have it, so you'd need to implement yourself.
For example, an "AddedBy" column can be used to restrict data changes to other members in the same group. Of course, if the Addedby user changes group you have to track this
To restrict allow INSERT and SELECT only, just GRANT these permissions.
Otherwise, please add more use cases
You could use a specific database users for your application with limited rights (No INSERT, DELETE) for the desired tables.

MySQL users privileges

Is there any permission or grant command on a table which make users not able to delete records (row) that inserted by another user. But allow them to delete or update their own record?
Thanks,
Stephen
No, this should be addressed by the application that runs on top of MySQL.
Not entirely sure the following's workable for your case, but I thought I might as well suggest it -- perhaps it can help. You could create for each user an updatable view (with sql security definer) that selects only those user's records, and grant the user the ability to delete on the view but not the table it selects from -- with sql security definer, the user should then be able to delete from the view causing deletion from the table. You'll also have to update the view each time the user creates a new record (otherwise the view is "frozen" at the time it was created). See the docs for create view and updatable views.
u should implement acl with a programming language. some libraries like zend do the job for u (using php language).