ALTER TABLE `account`.`user` ADD passExpired DATE DEFAULT NULL;

DROP PROCEDURE `account`.`myUser_changePassword`;
DROP PROCEDURE `account`.`myUser_restorePassword`;
DROP PROCEDURE `account`.`user_changePassword`;
DROP PROCEDURE `account`.`user_restorePassword`;
DROP PROCEDURE `account`.`user_setPassword`;

ALTER TABLE account.`user` CHANGE password password__ char(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL COMMENT 'Deprecated';
ALTER TABLE account.`user` CHANGE bcryptPassword password varchar(512) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT NULL NULL;

DELIMITER $$
$$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `account`.`user_beforeUpdate`
	BEFORE UPDATE ON `user`
	FOR EACH ROW
BEGIN
	SET NEW.editorFk = account.myUser_getId();

	IF !(NEW.`name` <=> OLD.`name`) THEN
		CALL user_checkName (NEW.`name`);
	END IF;

	IF !(NEW.`password` <=> OLD.`password`) THEN
		SET NEW.lastPassChange = util.VN_NOW();
	END IF;
END$$
DELIMITER ;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
    VIEW `account`.`accountDovecot` AS
select
    `u`.`name` AS `name`,
    `u`.`password` AS `password`
from
    (`account`.`user` `u`
join `account`.`account` `a` on
    (`a`.`id` = `u`.`id`))
where
    `u`.`active` <> 0;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
    VIEW `salix`.`User` AS
select
    `account`.`user`.`id` AS `id`,
    `account`.`user`.`realm` AS `realm`,
    `account`.`user`.`name` AS `username`,
    `account`.`user`.`password` AS `password`,
    `account`.`user`.`email` AS `email`,
    `account`.`user`.`emailVerified` AS `emailVerified`,
    `account`.`user`.`verificationToken` AS `verificationToken`
from
    `account`.`user`;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
    VIEW `vn`.`workerTimeControlUserInfo` AS
select
    `u`.`id` AS `userFk`,
    `w`.`firstName` AS `name`,
    `w`.`lastName` AS `surname`,
    `u`.`name` AS `user`,
    `u`.`password` AS `password`,
    `wd`.`departmentFk` AS `departmentFk`,
    left(`c`.`fi`,
    8) AS `dni`
from
    (((`account`.`user` `u`
join `vn`.`worker` `w` on
    (`w`.`userFk` = `u`.`id`))
join `vn`.`client` `c` on
    (`c`.`id` = `u`.`id`))
left join `vn`.`workerDepartment` `wd` on
    (`wd`.`workerFk` = `w`.`id`));
