1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-09 04:30:36 -05:00
bitwarden/util/Migrator/DbScripts/2025-06-04-00_AddReadPendingAuthRequestsByUserId.sql

23 lines
694 B
Transact-SQL

-- Adds a stored procedure to read pending authentication requests by user ID.
CREATE OR ALTER PROCEDURE [dbo].[AuthRequest_ReadPendingByUserId]
@UserId UNIQUEIDENTIFIER,
@ExpirationMinutes INT
AS
BEGIN
SET NOCOUNT ON;
;WITH PendingRequests AS (
SELECT
AR.*,
ROW_NUMBER() OVER (PARTITION BY RequestDeviceIdentifier ORDER BY CreationDate DESC) AS rn
FROM dbo.AuthRequestView AR
WHERE Type IN (0, 1)
AND AR.CreationDate >= DATEADD(MINUTE, -@ExpirationMinutes, GETUTCDATE())
AND AR.UserId = @UserId
AND AR.Approved IS NULL
)
SELECT PR.*
FROM PendingRequests PR
WHERE rn = 1;
END;