Search

Wednesday, April 17, 2013

What all Database Permissions are being assigned to a particular login ?

SCRIPT to FIND all Permissions assigned to a individual LOGIN ID

EXECUTE AS LOGIN = 'INSERT LOGIN NAME HERE'
   
    CREATE TABLE ##ObjectLevel
    (
        DatabaseName    NVARCHAR(128),
        Name            NVARCHAR(128),
        SubEntityName    NVARCHAR(128),
        PermissionName    NVARCHAR(128)
    )

    EXECUTE sp_msforeachdb 'USE [?] 
        INSERT INTO ##ObjectLevel
        SELECT db_name(), t.name,  c.subentity_name, c.permission_name 
        FROM sys.objects t
        CROSS APPLY fn_my_permissions(QUOTENAME(t.name), ''OBJECT'') c'
        
    SELECT    NULL AS 'Database Owning Object', 
            @@SERVERNAME AS 'Securable Name', 
            a.subentity_name  COLLATE Latin1_General_100_CI_AI AS 'Subentity Name', 
            a.permission_name COLLATE Latin1_General_100_CI_AI AS 'Permission Name'
    FROM fn_my_permissions(NULL, 'SERVER') a
    UNION ALL
    SELECT    NULL,
            d.name COLLATE Latin1_General_100_CI_AI, 
            b.subentity_name COLLATE Latin1_General_100_CI_AI, 
            b.permission_name COLLATE Latin1_General_100_CI_AI
    FROM sys.databases d
        CROSS APPLY fn_my_permissions(QUOTENAME(d.name), 'DATABASE') b
    UNION ALL
    SELECT    o.DatabaseName COLLATE Latin1_General_100_CI_AI,
            o.Name COLLATE Latin1_General_100_CI_AI, 
            o.SubentityName COLLATE Latin1_General_100_CI_AI, 
            o.PermissionName COLLATE Latin1_General_100_CI_AI
    FROM ##ObjectLevel o
    
    DROP TABLE ##ObjectLevel

    REVERT


 

No comments:

Post a Comment