In MySQL Trigger, how to get information about the user sending the request?

I am in MySQL 5.5 with a trigger and I want to check if the user can fulfill his request. This is just an example, how can I do with such code?

-- 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$$
+3
source share
1 answer

Ok, I found a solution:

USER ()

[EDIT]

Attention:

MySQL stores the values userand hostin accordance with the setting of UTF8-BIN after lowering them and USER()returns without lowering.

For example, USER()return gqyy@MyTinnyHost-PC.localwhen MySQL saved gqyyandmytinnyhost-pc.local

( # 60166) , a SUBSTRING_INDEX() a LOWER() USER(),

:

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)
+4

Source: https://habr.com/ru/post/1789446/


All Articles