USE `account`;
CREATE 
     OR REPLACE ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `account`.`__userRole` AS
    SELECT 
        `r`.`inheritsFrom` AS `id`
    FROM
        (`account`.`roleRole` `r`
        JOIN `account`.`user` `u` ON ((`u`.`role` = `r`.`role`)))
    WHERE
        (`u`.`id` = USERGETID());

DROP VIEW `account`.`userRole`;

CREATE TABLE `account`.`userRole` (
	`userFk` INT(10) UNSIGNED NOT NULL,
	`roleFk` INT(10) UNSIGNED NOT NULL,
	PRIMARY KEY (`userFk`, `roleFk`));

ALTER TABLE `account`.`userRole` 
	ADD INDEX `user` (`userFk` ASC),
	ADD INDEX `role` (`roleFk` ASC);

ALTER TABLE `account`.`userRole` 
	ADD CONSTRAINT `user`
		FOREIGN KEY (`userFk`)
		REFERENCES `account`.`user` (`id`)
		ON DELETE CASCADE
		ON UPDATE CASCADE,
	ADD CONSTRAINT `role`
		FOREIGN KEY (`roleFk`)
		REFERENCES `account`.`role` (`id`)
		ON DELETE CASCADE
		ON UPDATE CASCADE;
