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$$
Ok, i've found the solution:
USER()
[EDIT]
Warning:
MySQL store
user
andhost
values in with UTF8-BIN collation, after lowering them andUSER()
return without lowering.For example,
USER()
return[email protected]
when MySQL have storedgqyy
andmytinnyhost-pc.local
a problem described here (Bug #60166) arises when you use a
SUBSTRING_INDEX()
inside aLOWER()
with the return ofUSER()
stored in an user-defined variableFor example: