In a MySQL Trigger, how to get informations on the user sending the request?

℡╲_俬逩灬. 提交于 2019-12-06 07:12:07

问题


I'm on MySQL 5.5, with a trigger, and I want to check if the user can do its request. It'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$$

回答1:


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)


来源:https://stackoverflow.com/questions/4872681/in-a-mysql-trigger-how-to-get-informations-on-the-user-sending-the-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!