|
-
Dec 5th, 2005, 07:01 AM
#1
Thread Starter
Member
SQL Server 2005 User Permissions
I am develoing a system which cannot permit users performing adhoc queries,
how can I create a user in SQL 2005 with permissions to run stored procedures only?
Thx
-
Dec 5th, 2005, 07:37 AM
#2
Re: SQL Server 2005 User Permissions
 Originally Posted by Walter_Ego
I am develoing a system which cannot permit users performing adhoc queries,
how can I create a user in SQL 2005 with permissions to run stored procedures only?
Thx
We do exactly that in our app's.
The USERS are granted EXEC permission to the SPROCS - that permits them only access to stored procedures, no table access without it being wrapped in a SPROC.
This also means that your UI app cannot perform adhoc queries - but everything done through a stored procedure.
We like it that way here.
-
Dec 5th, 2005, 07:55 AM
#3
Thread Starter
Member
Re: SQL Server 2005 User Permissions
Must I grant permission for each procedure?
(Theres quite a few in here)
-
Dec 5th, 2005, 08:11 AM
#4
Re: SQL Server 2005 User Permissions
We script all our stored procedures in TEXT files - see post #5 in this thread:
http://www.vbforums.com/showthread.p...ight=fundsuser
With that done, then every script grant's it's own access - it's one of our SQL RULES TO LIVE BY.
It would also be very easy to loop through all the SPROCS and UDF's in the object table and grant permissions as well. I've got a sample of code for that at the office if you would consider writing a script to loop through those objects.
-
Dec 5th, 2005, 08:24 AM
#5
Thread Starter
Member
Re: SQL Server 2005 User Permissions
We also create all of our procs in text files, but dont do the access grants there, I do think it is a good idea and will consult it with the other developers.
(It'd also be nice to have a look at that loop script)
-
Dec 5th, 2005, 08:58 AM
#6
Thread Starter
Member
Re: SQL Server 2005 User Permissions
I already done it, is this OK?
Code:
DECLARE MY_CURSOR Cursor
FOR
Select name from sysobjects where xtype = 'P'
Open My_Cursor
DECLARE @ProcName VARCHAR(60),
@Command VARCHAR(100)
Fetch NEXT FROM MY_Cursor INTO @ProcName
While (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
SET @Command = 'GRANT EXECUTE ON ' + @ProcName + 'TO MyUser'
EXEC(@Command)
FETCH NEXT FROM MY_CURSOR INTO @ProcName
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
GO
-
Jan 3rd, 2006, 02:16 PM
#7
Re: SQL Server 2005 User Permissions
 Originally Posted by szlamany
The USERS are granted EXEC permission to the SPROCS - that permits them only access to stored procedures, no table access without it being wrapped in a SPROC.
Just keep in mind that if a SPROC accesses objects in other databases, then the user will need explicit access to those objects.
-
Jan 3rd, 2006, 02:18 PM
#8
Re: SQL Server 2005 User Permissions
 Originally Posted by kaffenils
Just keep in mind that if a SPROC accesses objects in other databases, then the user will need explicit access to those objects.
But that is specifically other databases - right??
-
Jan 3rd, 2006, 02:41 PM
#9
Re: SQL Server 2005 User Permissions
 Originally Posted by szlamany
But that is specifically other databases - right??
Yes.
-
Jan 3rd, 2006, 02:56 PM
#10
Re: SQL Server 2005 User Permissions
 Originally Posted by szlamany
But that is specifically other databases - right??
To clarify a little bit more:
If a view, function or sproc in DB_1 accesses an object (that be a table,view, sproc...) in DB_2, then the user in DB_1 will need appropriate access the this object in DB_2.
That means that if you have a sproc called SPROC_1 in DB_1 that reads data from a table named TABLE_2 in DB_2, the user that exeutes SPROC_1 will need permission to execute this sproc and SELECT from TABLE_2 in DB_2. If there is a sproc called SPROC_2 in DB_2 that selects from TABLE_2 in DB_2, and SPROC_1 in DB_1 executes SPROC_2 in DB_2, then the user will need execute permission on SPROC_1 in DB_1 and SPROC_2 in DB_2. The user no longer need SELECT access to TABLE_2 in DB_2.
-
Jan 3rd, 2006, 02:58 PM
#11
Re: SQL Server 2005 User Permissions
That makes complete sense - thanks for filling my greymatter further
-
Dec 5th, 2005, 09:53 AM
#12
Thread Starter
Member
Re: SQL Server 2005 User Permissions
We plan to use Application Roles, yes.
I can mold this script to use a Role instead of a User correct?
Last edited by Walter_Ego; Jan 4th, 2006 at 05:24 AM.
-
Dec 5th, 2005, 10:31 AM
#13
Re: SQL Server 2005 User Permissions
 Originally Posted by Walter_Ego
We plan to use Application Roles, yes.
I can mold this script to use a Role instead of a User correct?
We usually have just one ROLE in our DB - like StufilesUser or FundUser or AcctfilesUser.
We do:
GRANT EXECUTE ON frmExceptions_Inquire TO FundsUser
for example - giving the FundsUser ROLE EXECUTE to a SPROC (frmExceptions_Inquire in the example).
Then it's up to the customer to chose to either put each user into SQL security and associate them with the ROLE, or to create WINDOWS GROUPS, associate the ROLE with the GROUP and then assign user to the GROUP.
[edit] - Although ours are DATABASE ROLES not APPLICATION ROLES - never used APPLICATION ROLES (they require password to turn on - right?)
-
Jan 3rd, 2006, 12:16 PM
#14
Thread Starter
Member
Re: SQL Server 2005 User Permissions
Hi there again - update
Am testing this now, created a SQL Login called tstLogin
Created a user in this database called tstUser
tstLogin is mapped to tstUser
tstUser is in the tstRole group
tstRole group has execute permission on the GetRecords SPROC
When I login as tstLogin, and execute the GetRecords SPROC, I get the 'SELECT permission denied' error on the table being queried from the SPROC. What am I doing wrong?
-
Jan 3rd, 2006, 12:19 PM
#15
Re: SQL Server 2005 User Permissions
tstLogin is mapped to tstUser?
What does that mean?
-
Jan 3rd, 2006, 01:16 PM
#16
Thread Starter
Member
Re: SQL Server 2005 User Permissions
I was asked for the Login Name when I created tstUser.
Now tstUser shows in the tstLogin properties under the 'User Mapping' tab.
-
Jan 3rd, 2006, 01:38 PM
#17
Re: SQL Server 2005 User Permissions
Ok - first of all I've never used SQL 2005 Management studio - so I'm just getting into it now on my laptop - we've got one SQL 2005 instance running on a server here at the office...
BTW - why are you using a SQL LOGIN and not a WINDOWS LOGIN? Do you really want to ask the user for a username and password when they get into the app?
ok - I went to the SECURITY branch - opened up LOGINS - right clicked and selected NEW LOGIN...
What did you do at that point?
-
Jan 4th, 2006, 05:56 AM
#18
Thread Starter
Member
Re: SQL Server 2005 User Permissions
 Originally Posted by szlamany
BTW - why are you using a SQL LOGIN and not a WINDOWS LOGIN? Do you really want to ask the user for a username and password when they get into the app?
ok - I went to the SECURITY branch - opened up LOGINS - right clicked and selected NEW LOGIN...
What did you do at that point?
After that go to one of the databases and open up the security>Users Branch and create a new user. It asks you for a login. If you go to the properties of the login itself and click on the 'User Mappings' tab you will see this user.
We need to use SQL Server security logins because not all of the users will belong in the same domain.
Guys, its been solved using the EXECUTE AS OWNER clause:
Code:
CREATE PROCEDURE HRGetRecords
@Dept int
WITH EXECUTE AS OWNER
AS
SELECT ... ... ...
Thanks for all the help!
-
Jan 4th, 2006, 06:06 AM
#19
Re: SQL Server 2005 User Permissions
 Originally Posted by Walter_Ego
Guys, its been solved using the EXECUTE AS OWNER clause
Nice. Thats a new SQL Server 2005 feature.
-
Jan 4th, 2006, 07:30 AM
#20
Re: SQL Server 2005 User Permissions
btw - there is a link in my signature on how to create "non-domain" windows authenticaton access to servers...
You do not have to be in the domain to use WINDOWS AUTHENTICAION - if you have WINDOWS XP PRO as the operating system.
-
Jan 4th, 2006, 08:01 AM
#21
Re: SQL Server 2005 User Permissions
 Originally Posted by kaffenils
Nice. Thats a new SQL Server 2005 feature.
I just looked in my MSDN library and I can see how EXECUTE AS changes the "login credentials" of the user - but that would seem to me that it fixed the symptom and not the problem...
I'm certainly not looking forward to adding EXECUTE AS statements to over a 1000 SPROCS to make SQL 2005 work
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|