In a MySQL Trigger, how to get informations on the user sending the request?
I'm on MySQL 5.5, with a trigger, and I want to check if the user can do its request. It开发者_StackOverflow社区's just an exemple, how can I do with a code like this?
-- Trigger DDL Statements
DELIMITER $$
USE `database`$$
CREATE TRIGGER TBI_TEST BEFORE INSERT
ON tb_test FOR EACH ROW
BEGIN
DECLARE ER_BAD_USER CONDITION FOR SQLSTATE '45000';
IF NEW.host != {{HOW TO KNOW THE HOST PART OF THE CURRENT USER?}} THEN
SIGNAL ER_BAD_USER
SET MESSAGE_TEXT = 'forbidden', MYSQL_ERRNO = 401;
END IF;
END$$
Ok, i've found the solution:
USER()
[EDIT]
Warning:
MySQL store user
and host
values in with UTF8-BIN collation, after lowering them and USER()
return without lowering.
For example, USER()
return gqyy@MyTinnyHost-PC.local
when MySQL have stored gqyy
and mytinnyhost-pc.local
a problem described here (Bug #60166) arises when you use a SUBSTRING_INDEX()
inside a LOWER()
with the return of USER()
stored in an user-defined variable
For example:
mysql> SET @user_at_host = 'gqyy@mytinyhost-PC.local';
Query OK, 0 rows affected (0,00 sec)
mysql> SELECT LOWER(SUBSTRING_INDEX(@user_at_host, '@', -1));
+------------------------------------------------+
| LOWER(SUBSTRING_INDEX(@user_at_host, '@', -1)) |
+------------------------------------------------+
| mytinyhost-pc. ocal |
+------------------------------------------------+
1 row in set (0,00 sec)
精彩评论