Give a user access to ONE table

  • What's the safest way, not necessarily the most elegant, to grant a user account read-only access to only one table in a database? The total number of tables may change over time.

    I've tried various tests with REVOKE ALL TO Public, creating custom roles, etc., but not satisfied (and failed to create comfort level in the business unit) so far.

    Mike Hinds Lead Database Administrator1st Source BankMCP, MCTS

  • GRANT SELECT ON [Schema Name].[Table Name] to [Principal Name]

    I guess you tried this but the user has access to objects through other groups/roles. What you are trying to do cannot be done by design. I suggest doing an active directory and permission audit to see exactly what users and groups have what permissions and then working with Network support to clean this up. Nested groups can make this more difficult, but power shell and/or other scripting can help.

    If you are a domain admin you can just look all this up, but if not then scripts can help. For example the following VBS script will tell you what members are in a group assuming there are groups with access to your database:

    Set Arg = WScript.Arguments

    set oGroup = GetObject("WinNT://datacore_kc/"+Arg(0)+",group")

    for each oMem in oGroup.Members

    str = str + oMem.name + chr(9) + oMem.Class + chr(10)

    next

    msgbox(str)

  • What we finally did was:

    [font="Courier"]EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'

    GO

    GRANT SELECT ON [dbo].[AllowedTable] TO [TheUser]

    GO[/font]

    For anyone considering this, sp_msForEachTable is "non-supported".

    Mike Hinds Lead Database Administrator1st Source BankMCP, MCTS

  • I see. I guess if that's what you were dealing with you could have just as easily generated the script with

    SELECT 'DENY SELECT ON [' + s.name + '].[' + t.name + '] To [TheUser]'

    FROM sys.tables t

    JOIN sys.schemas s

    ON t.Schema_id = s.Schema_id

    UNION

    SELECT '[dbo].[AllowedTable] TO [TheUser]'

  • Mike Hinds (9/9/2010)


    What we finally did was:

    [font="Courier"]EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'

    GO

    GRANT SELECT ON [dbo].[AllowedTable] TO [TheUser]

    GO[/font]

    For anyone considering this, sp_msForEachTable is "non-supported".

    But what happens when you add another table in the future and haven't removed the user from whatever group has given him access? The user will then have access to the new table, no?

    Rich

  • First, don't grant rights to a user. If there's one, he/she will be replaced, or you'll add someone else. Take 10sec and write

    Create role MyDenyRole

    Then I'd do a GRANT on the single table to that role

    If the user doesn't have rights to the database, that's all that's needed. They don't get rights to other tables by default. If they have rights from another role, then you will need some DENY statements, and you'll have to handle that, or remove them from the other group/role.

    DO NOT grant rights to public. It's a bad idea. If you have rights set on public, create another role, transfer rights, remove them from public.

  • rmechaber (9/10/2010)


    Mike Hinds (9/9/2010)


    What we finally did was:

    [font="Courier"]EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'

    GO

    GRANT SELECT ON [dbo].[AllowedTable] TO [TheUser]

    GO[/font]

    For anyone considering this, sp_msForEachTable is "non-supported".

    But what happens when you add another table in the future and haven't removed the user from whatever group has given him access? The user will then have access to the new table, no?

    Rich

    You're exactly right, Rich. This was my big objection to this method. The database belongs to a 3rd party app, and tables will not change until the app gets an upgrade. The only thing is, the programmers who need this access expect me to remember to tell the engineer who is in charge of upgrades, that my script needs to be run AFTER the vendor's upgrade is complete.

    Mike Hinds Lead Database Administrator1st Source BankMCP, MCTS

  • Steve Jones - Editor (9/10/2010)


    First, don't grant rights to a user. If there's one, he/she will be replaced, or you'll add someone else. Take 10sec and write

    Create role MyDenyRole

    Then I'd do a GRANT on the single table to that role

    If the user doesn't have rights to the database, that's all that's needed. They don't get rights to other tables by default. If they have rights from another role, then you will need some DENY statements, and you'll have to handle that, or remove them from the other group/role.

    DO NOT grant rights to public. It's a bad idea. If you have rights set on public, create another role, transfer rights, remove them from public.

    First, I apologize - I failed to mention that this "user" is a native SQL service account, not a human. It will never change (unless I'm able to remove it at some point). Because of this, I believe the role is an unnecessary layer.

    I agree in not doing anything with public. Trouble is, public already has read/write. As soon as a (new, naked) user account is added to the DB, it inherits read/write from public. It would make my life easier if public didn't come fully loaded.

    Mike Hinds Lead Database Administrator1st Source BankMCP, MCTS

  • After reading a few articles and trial and error, the only procedure that worked was using

    sp_msForEachTable.

Viewing 9 posts - 1 through 8 (of 8 total)

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