In this scenario, I seek assistance in implementing row-level permissions for a table named 'user' with columns 'username' and 'role'. The objective is to establish a security model where access to individual rows is controlled based on the role assigned to each user. Specifically, the permissions should adhere to the following criteria: role='members' can only view 1 row when @username=USER_NAME(), while role='admins' can view their own row when @username=USER_NAME() and rows with role='members'. For role='ceos', they can view their own row and rows with role='admins'.
CREATE FUNCTION Security.fn_securitypredicate3
(@username AS nvarchar(50), @role AS varchar(10))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (
SELECT 1 AS Result
FROM dbo.user
WHERE
(@username = USER_NAME() AND @role = 'members' AND username = @username)
OR
(@username = USER_NAME() AND @role = 'admins' AND (username = @username OR role = 'members'))
OR
(@username = USER_NAME() AND @role = 'ceos' AND (username = @username OR role = 'admins'))
);
When I log in with the admin account, I can only see its own row, and cannot see rows with role='members'
usertable, or for another table but based on theusertable? Please show definitions for all relevant tables, also please show yourCREATE SECURITY POLICY