DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `account`.`user_hasRoleId`(vUser VARCHAR(255), vRoleId INT) RETURNS tinyint(1) DETERMINISTIC BEGIN /** * Checks if user has/inherits a role. * * @param vUserName The user name * @param vRoleId Role id 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` = vUser AND r.id = vRoleId; RETURN vHasRole; END$$ DELIMITER ;