How to handle users and logins in Visual Studio Database Project?

后端 未结 1 475
悲&欢浪女
悲&欢浪女 2021-01-11 14:14

I\'ve built a database in SQL Server 2008 R2 and am using Visual Studio 2010 Ultimate to create a database project for it.

I\'ve created both a SQL Server project an

相关标签:
1条回答
  • 2021-01-11 14:48

    We've had to mess with this before and ended up using Jamie Thompson's idea of creating post-deployment scripts to handle permissions based on a variable containing the name of the environment/configuration. You can find the (archived) article here: https://web.archive.org/web/20190222004817/http://sqlblog.com/blogs/jamie_thomson/archive/2010/07/21/a-strategy-for-managing-security-for-different-environments-using-the-database-development-tools-in-visual-studio-2010.aspx

    Note: Jamie's link is apparently dead. I wrote up something based on it here: http://schottsql.com/2013/05/14/ssdt-setting-different-permissions-per-environment/

    I also wrote a script to handle scripting of permissions:

    SELECT
    state_desc + ' ' + permission_name +
    ' on ['+ ss.name + '].[' + so.name + ']
    to [' + sdpr.name + ']'
    COLLATE LATIN1_General_CI_AS as [Permissions T-SQL]
    FROM SYS.DATABASE_PERMISSIONS AS sdp
    JOIN sys.objects AS so
         ON sdp.major_id = so.OBJECT_ID
    JOIN SYS.SCHEMAS AS ss
         ON so.SCHEMA_ID = ss.SCHEMA_ID
    JOIN SYS.DATABASE_PRINCIPALS AS sdpr
         ON sdp.grantee_principal_id = sdpr.principal_id
    
    UNION
    
    SELECT
    state_desc + ' ' + permission_name +
    ' on Schema::['+ ss.name + ']
    to [' + sdpr.name + ']'
    COLLATE LATIN1_General_CI_AS as [Permissions T-SQL]
    FROM SYS.DATABASE_PERMISSIONS AS sdp
    JOIN SYS.SCHEMAS AS ss
         ON sdp.major_id = ss.SCHEMA_ID
         AND sdp.class_desc = 'Schema'
    JOIN SYS.DATABASE_PRINCIPALS AS sdpr
         ON sdp.grantee_principal_id = sdpr.principal_id
    order by [Permissions T-SQL]
    GO
    

    Together, I set up the permissions into post-deploy scripts and folders that will recreate users/roles/permissions based on the environment. We call a "wrapper" script from the main post-deploy section that goes through the rest to figure out which section to run.

    0 讨论(0)
提交回复
热议问题