views:

115

answers:

3

I want to be able to have a Stored Procedure that can only be used from a particular page, without having to create a permissions / role for a user, when it is just a single stored procedure I want accessed in this way.

For example I want to have a proc_GetCustomerItems stored procedure which takes the Parameter of CustomerID and then returns a list of Items a Customer has purchased, however I want this to only work for the place which lists the Customer Items eg. the ASP Page which shows this list, but not work from anywhere else, such as Query Analyser etc.
Would this be done with another parameter to provide additional security information, the basic issue is how to allow only certain Methods and Pages access to a Stored Procedure, could this be done with a Role or Permissions?

The main issue is how to set the access rights to a Stored Procedure correctly to a particular method in code, which will help enforce the use of said Stored Procedure, so one has to explicitly allow a GetCustomerItems method (in ASP for example) access to use this Stored Procedure.

I'm guessing Roles and User Access will be the suggested alternative, but I would be interested if it is possible to restrict access to a Stored Procedure by where it is called from.

+2  A: 

No, this is not possible. The nearest I can imagine you getting to this is the page that only has access to the certain stored proc uses different credentials from the rest of the pages. If you want people to not access the proc using Query Analyser you can use the standard SQL Server security features.

RichardOD
I had a feeling this was the case, but thought I'd ask anyway, as it is something I've wanted to know about as a possible alternative. This may be useful anyway, as it shows the SQL security features can offer this on a connection-basis, which the page can use as mentioned.
RoguePlanetoid
A: 

The closest you could get would be to add code to the beginning of your stored procedure that queried the values of APP_NAME(), HOST_NAME(), HOST_ID(), CURRENT_USER and perhaps others, and if any didn't match the relevant values for your app then just return NULL or an empty row. This wouldn't be page-specific, of course, and there wouldn't be anything to stop someone using Query Analyzer to view the contents of the stored procedure itself.

CodeByMoonlight
Thanks for this, the main purpose of the question was to see if a page could use a stored procedure when allowed, and not before. This might be useful to have these when the context of the Stored Procedure has to be Page only.
RoguePlanetoid
Well, you could also make that page run the sp with different credentials to everywhere else, or just modify the ApplicationName in the connection string for that page to something unique and 'block' everything else.
CodeByMoonlight
A: 

Hard to know whether it would be suitable for you or not, and usually I'm pro-stored procedures but the only alternative I could suggest is that you don't use a stored procedure.

Instead, use parameterised sql from the page in question. This way, there's no stored procedure existing to be used from anywhere else.

This would mean, if not already the case, that the user account being used would need direct permissions on the underlying tables.

AdaTheDev