salix/db/changes/10221-accountModule/00-user_hasRole.sql

26 lines
666 B
MySQL
Raw Normal View History

DROP FUNCTION IF EXISTS account.user_hasRole;
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `account`.`user_hasRole`(vUserName VARCHAR(255), vRoleName VARCHAR(255)) RETURNS tinyint(1)
DETERMINISTIC
BEGIN
/**
* Checks if user has/inherits a role.
*
* @param vUserName The user name
* @param vRoleName Role to check
* @return %TRUE if it has role, %FALSE otherwise
*/
DECLARE vHasRole BOOL DEFAULT FALSE;
SELECT COUNT(*) > 0 INTO vHasRole
FROM user u
JOIN roleRole rr ON rr.role = u.role
JOIN role r ON r.id = rr.inheritsFrom
WHERE u.`name` = vUserName
AND r.`name` = vRoleName COLLATE 'utf8_unicode_ci';
RETURN vHasRole;
END$$
DELIMITER ;