天天看点

MS SQL 批量给存储过程/函数授权

  在工作当中遇到一个类似这样的问题:要对数据库账户的权限进行清理、设置,其中有一个用户test,只能拥有数据库myassistant的dml(更新、插入、删除等)操作权限,另外拥有执行数据库存储过程、函数的权限,但是不能进行ddl操作(包括新建、修改表、存储过程等...),于是需要设置登录名test的相关权限:

1:右键单击登录名test的属性.

MS SQL 批量给存储过程/函数授权

2: 在服务器角色里面选择"public"服务器角色。

MS SQL 批量给存储过程/函数授权

3:在用户映射选项当中,选择"db_datareader"、"db_datawriter"、"public"三个数据库角色成员。

MS SQL 批量给存储过程/函数授权

此时,已经实现了拥有dml操作权限,如果需要拥有存储过程和函数的执行权限,必须使用grant语句去授权,一个生产库的存储过程和函数加起来成千上百,如果手工执行的话,那将是一个辛苦的体力活,而我手头有十几个库,所以必须用脚本去实现授权过程。下面是我写的一个存储过程,亮点主要在于会判断存储过程、函数是否已经授予了exe或select权限给某个用户。这里主要用到了安全目录试图sys.database_permissions,例如,数据库里面有个存储过程dbo.sp_authorize_right,如果这个存储过程授权给test用户了话,那么在目录试图sys.database_permissions里面会有一条记录,如下所示:

MS SQL 批量给存储过程/函数授权

如果我将该存储过程授予exec权限给test1,那么

grant exec on dbo.sp_diskcapacity_cal to test;

grant exec on dbo.sp_diskcapacity_cal to test1;

MS SQL 批量给存储过程/函数授权

select * from sys.sysusers where name ='test' or name ='test1'

MS SQL 批量给存储过程/函数授权

其实grantee_principal_id代表向其授予权限的数据库主体 id ,所以我就能通过上面两个视图来判断存储过程是否授予执行权限给用户test与否,同理,对于函数也是如此,存储过程如下所示,其实这个存储过程还可以扩展,如果您有特殊的需要的话。

code snippet

use myassistant;

go

set ansi_nulls on;

set quoted_identifier on

if exists(select 1 from sysobjects where id=object_id(n'sp_authorize_right')

                                     and objectproperty(id, 'isprocedure') =1)

    drop procedure sp_authorize_right;

--====================================================================================================

--        procedurename        :            sp_authorize_right

--        author               :            kerry    

--        createdate           :            2013-05-10                

--        blog                 :            www.cnblogs.com/kerrycode/        

--        description          :            将数据库的所有自定义存储过程或自定义函数赋权给某个用户(可以继续扩展)

/*****************************************************************************************************

        parameter              :                         参数说明

******************************************************************************************************

        @type                  :  'p'  代表存储过程 , 'f' 代表存储过程,如果需要可以扩展其它对象

        @user                  :  某个用户账户

    modified date        modified user        version                    modified reason

    2013-05-13           kerry                 v01.00.01             排除系统存储过程和系统函数的授权处理

    2013-05-14           kerry                 v01.00.02             增加判断,如果某个存储过程已经赋予权限

                                                                     则不做任何操作

*****************************************************************************************************/

create procedure sp_authorize_right

(

    @type        as   char(10)    ,

    @user        as     varchar(20)

)

as

  declare @sqltextvarchar(1000);

  declare @userid    int;

select @userid = uid from sys.sysusers where name=@user;

    if @type = 'p'

      begin

        create table #procedurename( sqltext  varchar(max));

            insert  into #procedurename

            select  'grant execute on ' + p.name + ' to ' + @user + ';'

            from    sys.procedures p

            where   not exists( select 1

                                 from   sys.database_permissions r

                                 where  r.major_id = p.object_id

                                        and r.grantee_principal_id = @userid

                                        and r.permission_name is not  null )

            select * from #procedurename;

            --select  'grant execute on ' + name + ' to ' +@user +';'

            --from    sys.procedures;

            --select 'grant execute on ' + [name] + ' to ' +@user +';'

            -- from sys.all_objects

            --where [type]='p' or [type]='x' or [type]='pc'

        declare cr_procedure cursor for

        open cr_procedure;

        fetch next from cr_procedure  into @sqltext;

        while @@fetch_status = 0

        begin

            execute(@sqltext);

            fetch next from cr_procedure into @sqltext;

        end    

        close cr_procedure;

        deallocate cr_procedure;

      end

    else

        if @type='f'

           begin

               create table #functionset( functionname varchar(1000));

            insert  into #functionset

            select  'grant exec on ' + name + ' to ' + @user + ';'

            from    sys.all_objects s

                                 from   sys.database_permissions p

                                 where  p.major_id = s.object_id

                                    and  p.grantee_principal_id = @userid)

                    and schema_id = schema_id('dbo')

                    and( s.[type] = 'fn'

                          or s.[type] = 'af'

                          or s.[type] = 'fs'

                          or s.[type] = 'ft'

                        ) ;

              select * from #functionset;

                    --select 'grant exec on ' + name + ' to ' + @user +';' from sys.all_objects

                    -- where schema_id =schema_id('dbo')

                    --     and ([type]='fn' or [type] ='af' or [type]='fs' or [type]='ft' );

            select  'grant select on ' + name + ' to ' + @user + ';'

                    and( s.[type] = 'tf'

                          or s.[type] = 'if'

                        ) ;    

                select * from #functionset;

                --select 'grant select on ' + name + ' to ' + @user +';' from sys.all_objects

                -- where schema_id =schema_id('dbo')

                --     and ([type]='tf' or  [type]='if') ;         

               declare cr_function cursor for

                    select functionname from #functionset;

                open cr_function;

                fetch next from cr_function into @sqltext;

                while @@fetch_status = 0

                begin    

                    print(@sqltext);

                    exec(@sqltext);

                    fetch next from cr_function into @sqltext;

                end

                close cr_function;

                deallocate cr_function;

           end