存储过程中的登录方法

时间:2017-01-25 15:50:24

标签: mysql

你好几个小时都对这个问题感到困惑,我相信它很容易在MSSQL中完成,但MySQL出于某种原因不接受if语句中的插入。

我的伪代码

  • 如果用户存在
  • 成功登录登录表并返回关系编号
  • 如果不存在则向登录表添加失败并检查这是否是第3(或更多)时间。如果是阻止用户名。

这就是我的代码变成了:

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');

0 个答案:

没有答案