Created
May 6, 2021 14:10
-
-
Save bitsprint/e27b8d686e370922221873468b15cafd to your computer and use it in GitHub Desktop.
SQL Row level security
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- DROP FUNCTION [dbo].[fn_tenantSecurityPredicate] | |
CREATE FUNCTION [dbo].[fn_tenantSecurityPredicate](@tenantId UNIQUEIDENTIFIER) | |
RETURNS TABLE | |
WITH SCHEMABINDING | |
AS | |
RETURN SELECT 1 AS [predicateResult] | |
FROM [dbo].[Users] | |
WHERE [Username] = CURRENT_USER | |
AND ([TenantId] IS NULL OR ([TenantId] IS NOT NULL AND @tenantId = [TenantId])); | |
GO | |
-- DROP SECURITY POLICY [dbo].[tenantSecurityPolicy] | |
CREATE SECURITY POLICY [dbo].[tenantSecurityPolicy] | |
ADD FILTER PREDICATE [dbo].[fn_tenantSecurityPredicate](TenantId) ON [dbo].[Customer], | |
ADD FILTER PREDICATE [dbo].[fn_tenantSecurityPredicate](TenantId) ON [dbo].[Orders] | |
WITH (STATE = ON); | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment