I am requiring that the user's current password cannot match the last five used passwords. The expired passwords will be stored in a table similar to this.
################################################################
# table_A #
################################################################
# id # label # attr_string # attr_datetime # u_id #
################################################################
# 1 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 2 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 3 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 4 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
# 5 # expired_pass # some_hash # YYYY-MM-DD HH:MM:SS # 1 #
################################################################
Now I am wanting a trigger that upon input checks if there are >= 5 expired passwords already stored if so then remove the oldest password. This is what I have come up with so far.
DELIMITER //
CREATE TRIGGER removePass AFTER INSERT ON `user_attributes`(
SET @user_id := SELECT u_id FROM `user_attributes` WHERE id = LAST_INSERT_ID()//
SET @num := SELECT COUNT(id) FROM `user_attributes` WHERE u_id = @user_id AND label = 'expired_password'//
IF @num >= 5 THEN
DELETE FROM `user_attributes`
WHERE id IN(
SELECT id
FROM `user_attributes`
WHERE u_id = @user_id
AND label = 'expired_pass'
ORDER BY attr_datetime DESC
LIMIT 1
)
END IF
)//
DELIMITER ;