English 中文(简体)
Can I change execution context in a SP from OWNER to CALLER? [SQL Server 2005]
原标题:

Is it possible to change execution context to CALLER in a stored procedure, which is created with EXECUTE AS OWNER? Here is what I mean with a simplified example:

There are several logins and databases in a server instance. Assume that everybody has a table named current_work in his own database, but nobody can access to no other s table. Even the Boss isn t allow to access to the employees databases, unless an employer decides that his table is ready for the Boss to see. When the employer thinks his work is done, he is supposed to commit its table contents to Boss database. However, the Boss doesn t want to allow his employers to INSERT anything into his table, because there is a kind of business logic he wants to implement (let s say he wants to log the commits somewhere). So he wants this to happen only under his control, i.e. within an SP he wrote. He writes an SP like below, creates users in his database for each of the logins (for each employee), and then gives GRANT EXECUTE for this SP to all his employers. When an employee is done, he just calls this SP, and everything should be fine:

CREATE PROCEDURE boss.dbo.commit_work
WITH EXECUTE AS OWNER AS
BEGIN
   EXECUTE AS CALLER  -- It should be OK, since this is the default way of calling an SP anyway, thinks the boss
   SELECT * INTO ##tmptable FROM current_work  -- No prefix should mean default DB/Schema of the caller, thinks the boss
   REVERT  -- Back to OWNER s context, which happens to be the boss

   INSERT INTO boss.allwork
   SELECT * FROM ##tmptable
END

However, things doesn t go as he expected. The employers can execute the SP, but they get an error saying The server principal "Boss" is not able to had access the database "boss" under the current security context. , which makes the boss very upset (BTW Boss is the Login for the boss.)

Do you have an idea of what went wrong? Or do you see another way of implementing such a scenario, in which nobody is supposed to access to no other s DB unless the execution happens in the SP? I would have done such a thing in application level, when I had had the choice, but I am bound to database level because of the requirements of our current environment. Thanks!

最佳回答

I found the answer here in this great article of Erlang Sommarskog "Giving Permissions through Stored Procedures". I recommend you to read the whole article to grasp the idea.. All I had to do was creating a local proxy user and giving it the appropriate rights:

CREATE USER boss_proxy WITHOUT LOGIN
GRANT INSERT ON boss.allwork TO boss_proxy 

CREATE PROCEDURE boss.dbo.commit_work
WITH EXECUTE AS boss_proxy AS
BEGIN
   EXECUTE AS CALLER  -- It IS OK, indeed
   SELECT * INTO ##tmptable FROM current_work  -- No prefix means default DB/Schema of the caller
   REVERT  -- Back to boss_proxy s context

   INSERT INTO boss.allwork
   SELECT * FROM ##tmptable
END
问题回答

暂无回答




相关问题
SQL Server database is not visible

I have installed ASP.NET application with database on our server. ASP.NET application created database using connection string below. The problem is that I do not see database in SQL Server Management ...

Most efficient way to store number 11.111 in SQL Server

What datatype is the most efficient to store a number such as 11.111. The numbers will have up 2 digits before the point and up to three after. Currently the column is a bigint , I am guessing that ...

表格和数据表

是否需要在提瓜表之后更新表格统计数据,还是自动更新?

Inconsistent Generate Change Script

I add a column of type tinyint and being set to not allow nulls in a table and generate the change scripts. The table has data in it at this time. The script has code that creates a temp table and ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...

selecting one value out of an xml column

I have a particularly troublesome xml column to query data from. The schema is fixed by the Quebec Ministry of Revenue so "it is what it is" The important part of the query looks like this: with ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签