English 中文(简体)
确定是否可以基于AD登录执行存储过程
原标题:Determing if stored procedure can execute based on AD login

我有一个存储过程,用于更新表中特定记录的数据。我有几个用户将使用此存储过程,但我只希望他们能够更新分配给他们的记录。

要由存储过程更新的每个记录都有一个名为“UserID”的字段,该字段定义了谁可以控制该记录。我还有一个映射表,它将active directory登录映射到UserID s。

我正在使用Active Directory,以便SQL Server知道谁正在尝试执行存储过程。在存储过程中是否有方法在另一个表中查找用户的active directory登录名,然后确定他们是否有权访问试图更新的记录?

最佳回答

您可以通过调用SYSTEM_user并将其合并到更新行的查询中来找出进程中的当前用户。

问题回答

这篇文章有用吗在SQL Server中授予行级权限

它建议采取以下步骤

  • Create the table, adding an additional column to store the name.
  • Create a view that has a WHERE clause based on the user name column. This will restrict the rows returned to those with the specified value. Use one of the built-in functions to specify a database user or login name. This eliminates the need to create different views for different users.
  • Create stored procedures to select, insert, update, and delete data based on the view, not the base tables. The view provides a filter that restricts the rows returned or modified.
  • For stored procedures that insert data, capture the user name using the same function specified in the WHERE clause of the view and insert that value into the UserName column.
  • Deny all permissions on the tables and views to the public role. Users will not be able to inherit permissions from other database roles, because the WHERE clause is based on user or login names, not on roles.
  • Grant EXECUTE on the stored procedures to database roles. Users can only access data through the stored procedures provided.

我不是应用程序设计师,但从表面上看,你的解决方案听起来不必要地复杂。

也就是说,您可以发出以下查询来获取当前执行存储过程的用户的Windows AD登录名。您可以使用此信息与映射表进行交叉引用,以确定AD帐户是否具有执行该操作所需的权限。

SELECT SYSTEM_USER

请记住,这将返回当前正在执行的上下文的名称,因此请记住,可以使用“执行为”语句显式更改此名称。

编辑

我想知道是否可以使用视图来限制给定AD帐户可见的数据。然后,存储过程逻辑可以专注于数据修改方面,而不是安全实现。使用视图控制数据访问还可以确保在需要时跨多个存储过程使用一致的安全访问方法,而不是必须在每个存储过程中实施安全检查。

自撰写本文以来,Martin Smith提供的链接详细介绍了如何实现此解决方案:

在SQL Server中授予行级权限

总之,两者的结合就是微软建议您实现问题解决方案的方式。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签