Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Give a user access to ONE table Expand / Collapse
Author
Message
Posted Wednesday, September 8, 2010 5:20 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 8:37 AM
Points: 621, Visits: 877
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.


Regards, Mike
Post #982196
Posted Thursday, September 9, 2010 2:22 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 9, 2012 2:48 PM
Points: 493, Visits: 636
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)
Post #983429
Posted Thursday, September 9, 2010 3:29 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 8:37 AM
Points: 621, Visits: 877
What we finally did was:

EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'
GO

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


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


Regards, Mike
Post #983476
Posted Friday, September 10, 2010 7:13 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 9, 2012 2:48 PM
Points: 493, Visits: 636
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]'
Post #983719
Posted Friday, September 10, 2010 8:38 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 9:31 AM
Points: 717, Visits: 3,037
Mike Hinds (9/9/2010)
What we finally did was:

EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'
GO

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


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
Post #983822
Posted Friday, September 10, 2010 8:45 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 4:59 PM
Points: 31,367, Visits: 15,833
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #983830
Posted Friday, September 10, 2010 8:55 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 8:37 AM
Points: 621, Visits: 877
rmechaber (9/10/2010)
Mike Hinds (9/9/2010)
What we finally did was:

EXEC sp_msForEachTable 'DENY SELECT ON ? TO [TheUser]'
GO

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


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.


Regards, Mike
Post #983844
Posted Friday, September 10, 2010 9:04 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: 2 days ago @ 8:37 AM
Points: 621, Visits: 877
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.


Regards, Mike
Post #983861
Posted Tuesday, July 17, 2012 8:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 23, 2012 11:50 AM
Points: 1, Visits: 4
After reading a few articles and trial and error, the only procedure that worked was using
sp_msForEachTable.
Post #1330775
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse