Hi have been boggling my mind about this one for hours, I am sure it would be easily done in MSSQL but MySQL for some reason does not accept an insert inside an ifstatement.
my pseudo code
- If user exists
- Add successfull login to logins table and return the relationnumber
- if it does not exist add a fail to the logins table and check if this is the 3th (or more) time. if it is block the username.
This is what my code turned into:
SET @maxtries = 3;
SET @maxBlockTime = 15;
SET @username = 'test';
SET @ipAddress = '0.0.0.0';
SET @salt = 'salt';
SET @passwordHash = 'pswhsh';
SET @numOfUsernameFails = 0;
SET @relationNumber = NULL;
SET @emailConfirmed = NULL;
SET @foundStatus = 'Failed';
SELECT @relationNumber := `RelationNumber`, @emailConfirmed := `EmailConfirmed`
FROM accounts
WHERE
Username=@username AND
PasswordHash=@passwordHash AND
Salt=@salt;
SET @message := IF(ISNULL(@relationNumber), 'UnkownUsername', IF(@emailConfirmed = 0, 'NotConfirmed', 'Success'));
SET @foundStatus := IF(@message != 'Success', 'Failed', 'Succeeded');
INSERT INTO logins (`Username`, `Date`, `Status`, `IPAddress`) VALUES (@username, UTC_TIMESTAMP(), @foundStatus, @ipAddress);
-- if status is failed, check the number of times it has failed
-- if this is > @maxtries add to blocked table (sp_add_blocked)
-- if this is not, return table with message(s)
-- if status = success return table with username & relationnumber
SET @t := IF(@foundStatus = 'Failed'
, IF((SELECT COUNT(*) FROM (SELECT * FROM `logins` WHERE `Username`= @username order by `Date` DESC LIMIT 0, 3) `l` WHERE `l`.`Status` = 'Failed' AND (TIME_TO_SEC(TIMEDIFF(UTC_TIMESTAMP(), DATE)) / 60) < 15) >= 3,
'Blocked',
@message)
, @relationNumber);
SELECT IF(@t = 'Blocked',
'insert into blocked stored procedure and return blocked as message',
'return message unkown user');