Copy Permissions for a database

  • Comments posted to this topic are about the item Copy Permissions for a database

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • The function bo.f_lookup_system_id is not defined and the link http://www.sqlservercentral.com/columnists/awarren/sqlpermissionspublicrole_2.asp is no longer working.

    Any suggestions on where I can find this function?:)

    --Georgianne

  • I got the same thing, missing f_lookup_system_id.

    Can I get that code?

  • Gosh. This is a long time ago.

    I'd forgotten all about this until I got an email from smorgan just now. And I can barely remember anything about it, so my starting point is not that much different from you guys.

    I did quite a bit of digging but didn't find much which is directly useful.

    I can't be sure, but I think this article might now be a link for the one referred to:

    http://www.sqlservercentral.com/articles/Security/sqlpermissionspublicrole/116/

    Given that, I think the f_lookup_system_id function probably looked something like this (this is a guess, but it should at least give the idea):

    create function dbo.f_lookup_system_id(@name varchar(50), @id int) returns varchar(50) as

    /*

    select dbo.f_lookup_system_id('sysprotects_protecttype', 206)

    select dbo.f_lookup_system_id('sysprotects_action', 197)

    */

    begin

    declare @s-2 varchar(50)

    if @name = 'sysprotects_protecttype'

    select @s-2 =

    case @id

    when 204 then 'GRANT_W_GRANT'

    when 205 then 'GRANT'

    when 206 then 'REVOKE'

    end

    if @name = 'sysprotects_action'

    select @s-2 =

    case @id

    when 26 then 'REFERENCES'

    when 178 then 'CREATE FUNCTION'

    when 193 then 'SELECT'

    when 195 then 'INSERT'

    when 196 then 'DELETE'

    when 197 then 'UPDATE'

    when 198 then 'CREATE TABLE'

    when 203 then 'CREATE DATABASE'

    when 207 then 'CREATE VIEW'

    when 222 then 'CREATE PROCEDURE'

    when 224 then 'EXECUTE'

    when 228 then 'BACKUP DATABASE'

    when 233 then 'CREATE DEFAULT'

    when 235 then 'BACKUP LOG'

    when 236 then 'CREATE RULE'

    end

    return @s-2

    end

    Sorry I can't be more helpful.

    If you manage to get anything useful out of this, please let us know! 🙂

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Thanks! I may need to get back into that.

    For now, I used some of your code in the previous article and modified it. I am trying to copy Roles (and some users) from one DB to another on the same server. Here's what I got to work for me (this is a SQL 2000 server):

    [font="Courier New"]---------------------------------------------------------------

    -- COPY USERS & ROLES

    --from 1 db on this server to another db on this server

    ---------------------------------------------------------------

    -----------------------

    -- change all CA_AugME to "from" DataBase(OLD db)

    -- change all CA_SeptME to "to" DataBase(NEW db)

    --

    -- If the user had no permissions to DB, this will copy that from the "from" db and

    -- print a msg in grid 'ADDED USER ', @user_name

    --

    -- All Roles on the "from" db will be applied to the "to" db

    -------------------------------------------------------

    -- based on code from http://www.sqlservercentral.com/scripts/T-SQL+Aids/30754/

    -------------------------------------------------------

    -- Copy Roles

    USE CA_SeptME

    declare @s-2 varchar(1000)

    DECLARE c_from_user_roles CURSOR

    READ_ONLY

    FOR

    select u.name as UserName, a_ROLE.name as RoleName --, *

    from CA_AugME.dbo.sysusers as u

    join CA_AugME.dbo.sysmembers AS Person

    on u.uid = Person.memberuid

    JOIN CA_AugME.dbo.sysusers AS a_ROLE

    on a_ROLE.uid = groupuid

    WHERE U.NAME + A_ROLE.NAME

    NOT IN (SELECT U.NAME + A_ROLE.NAME

    from CA_SeptME.dbo.sysusers as u

    join CA_SeptME.dbo.sysmembers AS Person

    on u.uid = Person.memberuid

    JOIN CA_SeptME.dbo.sysusers AS a_ROLE

    on a_ROLE.uid = groupuid)

    --AND U.NAME = 'jduffy'-- TEST only !

    DECLARE @role_name varchar(40)

    DECLARE @user_name varchar(40)

    OPEN c_from_user_roles

    FETCH NEXT FROM c_from_user_roles INTO @user_name, @role_name

    WHILE (@@fetch_status <> -1)

    BEGIN

    IF (@@fetch_status <> -2)

    BEGIN

    BEGIN

    if NOT exists (select * from CA_SeptME.dbo.sysusers where name = @user_name)

    begin

    exec sp_grantdbaccess @user_name-- add user to DB

    -- print 'ADDED USER ' @user_name

    SELECT 'ADDED USER ', @user_name

    end

    END

    BEGIN

    set @s-2 = 'exec sp_addrolemember ''' + @role_name + ''', ''' + @user_name + ''''

    print @s-2

    exec(@s)-- copy Role for user

    END

    END

    FETCH NEXT FROM c_from_user_roles INTO @user_name, @role_name

    END

    CLOSE c_from_user_roles

    DEALLOCATE c_from_user_roles[/font]

  • works great with the function as was posted.

    I just added double quotes on the line #92:

    set @s-2 = @protect_type_name + ' ' + @action_name + ' ON ' + @object_name + ' TO "' + @to_user + '"'

    to support "DOMAIN\user" format.

    thanks.

    Roberto Figueroa.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply