我在使用触发器的MySQL 5.5上,我想检查用户是否可以执行其请求.这只是一个例子,如何处理这样的代码?
-- 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$$
解决方法:
好的,我找到了解决方案:
[编辑]
警告:
降低用户和主机值后,MySQL会以UTF8-BIN排序规则存储它们,而USER()不会降低而返回.
例如,当MySQL已存储gqyy和mytinnyhost-pc.local时,USER()返回gqyy@MyTinnyHost-PC.local
当您在LOWER()中使用SUBSTRING_INDEX()并返回存储在用户定义变量中的USER()时,会出现问题described here(Bug #60166)
例如:
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)