[推荐] 得到一个给定用户使用了的权限脚本
[推荐] 得到一个给定用户使用了的权限脚本
——通过知识共享树立个人品牌。
得到一个给定用户使用了的权限脚本,直接上代码,自己研究。
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[p_user_permissions_script_get]
-----------------------------------------------------------
-- OBJECT NAME :dbo.p_user_permissions_script_get
-- AUTHOR: EricHu
-- DATE: 01/05/2012
-- INPUT PARAMETERS:
@userName VARCHAR(500)
--
-- OUTPUT PARAMETERS: none
-- DEPENDENCIES: none
-- DESCRIPTION: Used to script out permissions for a given user
-- MODIFICATION HISTORY:
-------------------------------------------------------------
AS
SET NOCOUNT ON
DECLARE @DatabaseUserName [SYSNAME];
SET @DatabaseUserName = @userName;
DECLARE @errStatement VARCHAR(1000),
@msgStatement VARCHAR(1000),
@DatabaseUserID SMALLINT,
@ServerUserName SYSNAME,
@RoleName VARCHAR(1000),
@ObjectID INT,
@ObjectName VARCHAR(1000),
@StateDesc VARCHAR(1000),
@permissionName VARCHAR(1000)
SELECT @DatabaseUserID = su.[uid],
@ServerUserName = sl.[loginname]
FROM dbo.[sysusers] su
INNER JOIN [master].dbo.[syslogins] sl
ON su.[sid] = sl.[sid]
WHERE su.[name] = @DatabaseUserName
IF @DatabaseUserID IS NULL
BEGIN
SET @errStatement = 'User ' + @DatabaseUserName + ' does not exist in ' + DB_NAME()
+ CHAR(13) + 'Please provide the name of a current user in ' + DB_NAME()
+ ' you wish to script.'
RAISERROR(@errStatement,
16,
1)
END
ELSE
BEGIN
SET @msgStatement = '--Security creation script for user ' + @ServerUserName + CHAR(13)
+ '--Created At: ' + CONVERT(VARCHAR, GETDATE(), 100)
+ REPLACE(CONVERT(VARCHAR, GETDATE(), 108), ':', '') + CHAR(13)
+ '--Created By: ' + SUSER_NAME() + CHAR(13) + '--Add User To Database'
+ CHAR(13) + 'USE [' + DB_NAME() + ']' + CHAR(13)
+ 'EXEC [sp_grantdbaccess]' + CHAR(13) + CHAR(9)
+ '@loginame = ''' + @ServerUserName + ''',' + CHAR(13) + CHAR(9)
+ '@name_in_db = ''' + @DatabaseUserName + '''' + ';'+ CHAR(13) + 'GO'
+ CHAR(13) + '--Add User To Roles'
PRINT @msgStatement
DECLARE _sysusers CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
SELECT [name]
FROM [dbo].[sysusers]
WHERE [uid] IN (SELECT [groupuid]
FROM [dbo].[sysmembers]
WHERE [memberuid] = @DatabaseUserID)
OPEN _sysusers
FETCH NEXT FROM _sysusers INTO @RoleName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @msgStatement = 'EXEC [sp_addrolemember]' + CHAR(13) + CHAR(9) + '@rolename = '''
+ @RoleName + ''',' + CHAR(13) + CHAR(9) + '@membername = '''
+ @DatabaseUserName + '''' + ';' ;
PRINT @msgStatement
FETCH NEXT FROM _sysusers INTO @RoleName
END
CLOSE _sysusers;
DEALLOCATE _sysusers;
--Database level perms;
PRINT '--Set Database level Permissions';
DECLARE _databaselevelperms CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
SELECT
sdp.state_desc,
sdp.permission_name
FROM
sys.database_permissions sdp WITH(NOLOCK)
JOIN sysusers su WITH(NOLOCK)
ON su.uid = sdp.grantee_principal_id
WHERE
su.name = @userName
AND sdp.class_desc = 'DATABASE';
OPEN _databaselevelperms;
FETCH NEXT FROM _databaselevelperms INTO @StateDesc, @PermissionName;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @StateDesc + CHAR(13) + CHAR(9) + @PermissionName + CHAR(13) + CHAR(9)
+ 'TO ' + @userName + ';';
FETCH NEXT FROM _databaselevelperms INTO @StateDesc, @PermissionName
END
CLOSE _databaselevelperms;
DEALLOCATE _databaselevelperms;
SET @msgStatement = 'GO' + CHAR(13) + '--Set Object Specific Permissions'
PRINT @msgStatement;
DECLARE _sysobjects CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
SELECT DISTINCT( [sysobjects].[id] ),
'[' + USER_NAME([sysobjects].[uid]) + '].[' + [sysobjects].[name] + ']'
FROM [dbo].[sysprotects]
INNER JOIN [dbo].[sysobjects]
ON [sysprotects].[id] = [sysobjects].[id]
WHERE [sysprotects].[uid] = @DatabaseUserID;
OPEN _sysobjects;
FETCH NEXT FROM _sysobjects INTO @ObjectID, @ObjectName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @msgStatement = '';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 193
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'SELECT,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 195
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'INSERT,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 197
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'UPDATE,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 196
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'DELETE,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 224
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'EXECUTE,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 26
AND [protecttype] = 205)
SET @msgStatement = @msgStatement + 'REFERENCES,';
IF LEN(@msgStatement) > 0
BEGIN
IF RIGHT(@msgStatement, 1) = ','
SET @msgStatement = LEFT(@msgStatement, LEN(@msgStatement) - 1);
SET @msgStatement = 'GRANT' + CHAR(13) + CHAR(9) + @msgStatement + CHAR(13)
+ CHAR(9) + 'ON ' + @ObjectName + CHAR(13) + CHAR(9) + 'TO '
+ @DatabaseUserName + ';' ;
PRINT @msgStatement;
END
SET @msgStatement = '';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 193
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'SELECT,'
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 195
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'INSERT,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 197
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'UPDATE,';
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 196
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'DELETE,'
IF EXISTS(SELECT 1
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 224
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'EXECUTE,';
IF EXISTS(SELECT *
FROM [dbo].[sysprotects]
WHERE [id] = @ObjectID
AND [uid] = @DatabaseUserID
AND [action] = 26
AND [protecttype] = 206)
SET @msgStatement = @msgStatement + 'REFERENCES,';
IF LEN(@msgStatement) > 0
BEGIN
IF RIGHT(@msgStatement, 1) = ','
SET @msgStatement = LEFT(@msgStatement, LEN(@msgStatement) - 1)
SET @msgStatement = 'DENY' + CHAR(13) + CHAR(9) + @msgStatement + CHAR(13)
+ CHAR(9) + 'ON ' + @ObjectName + CHAR(13) + CHAR(9) + 'TO '
+ @DatabaseUserName + ';' ;
PRINT @msgStatement;
END
FETCH NEXT FROM _sysobjects INTO @ObjectID, @ObjectName;
END
CLOSE _sysobjects;
DEALLOCATE _sysobjects;
PRINT 'GO'
END
SET NOCOUNT OFF
RETURN 0
运行结果实例如下:
EXEC [p_user_permissions_script_get] 'dbo'
--Security creation script for user sa
--Created At: 01 13 2012 4:37PM163729
--Created By: sa
--Add User To Database
USE [DB_TEST]
EXEC [sp_grantdbaccess]
@loginame = 'sa',
@name_in_db = 'dbo';
GO
--Add User To Roles
EXEC [sp_addrolemember]
@rolename = 'db_owner',
@membername = 'dbo';
--Set Database level Permissions
GRANT
CONNECT
TO dbo;
GO
--Set Object Specific Permissions
GO
作者:
RDIF
出处:
http://www.cnblogs.com/huyong/
Email:
406590790@qq.com
QQ:
406590790
微信:
13005007127(同手机号)
框架官网:
http://www.guosisoft.com/
http://www.rdiframework.net/
框架其他博客:
http://blog.csdn.net/chinahuyong
http://www.cnblogs.com/huyong
国思RDIF开发框架
,
给用户和开发者最佳的.Net框架平台方案,为企业快速构建跨平台、企业级的应用提供强大支持。
关于作者:系统架构师、信息系统项目管理师、DBA。专注于微软平台项目架构、管理和企业解决方案,多年项目开发与管理经验,曾多次组织并开发多个大型项目,在面向对象、面向服务以及数据库领域有一定的造诣。现主要从事基于
RDIF
框架的技术开发、咨询工作,主要服务于金融、医疗卫生、铁路、电信、物流、物联网、制造、零售等行业。
如有问题或建议,请多多赐教!
本文版权归作者和CNBLOGS博客共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过微信、邮箱、QQ等联系我,非常感谢。