Tải bản đầy đủ (.pdf) (11 trang)

Câu lệnh EXECUTE AS trong SQL Server 2005 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (324.64 KB, 11 trang )



II




Câu lệnh EXECUTE
AS trong SQL
Server
2005






















Câu lệnh EXECUTE AS trong SQL Server
2005

Simpo PDF Merge and Split Unregistered Version -
Trong SQL Server 2005, bạn hoàn toàn có thể xác định
được rõ ràng ngữ cảnh thực thi của một người dùng cụ thể.
Như đã biết, một session (phiên làm việc) bắt đầu khi người
dùng đăng nhập vào SQLServer hoặc kết nối tới
SQLServer
Tất cả hoạt động trong sesstion đó dùng các ủy nhiệm đăng
nhập được sử dụng để kết nối tới SQL Server. Khi câu lệnh
EXECUTE AS chạy, ngữ cảnh thực thi của session sẽ
chuyển tới login hay tên người dùng.

Đây là một tiện ích rất có lợi cho những người quản trị cơ
sở dữ liệu SQL Server 2005 khi họ cần kiểm tra sự cho
phép của từng người dùng cụ thể. Nó còn rất hữu ích khi
người dùng muốn thực thi một hàm được lưu trữ trong ngữ
cảnh của người dùng khác. Bài này sẽ giải thích tính hữu
ích của câu lệnh EXECUTE AS đối với người quản trị cơ
sở dữ liệu.

Giả dụ quản trị cơ sở dữ liệu Mr.Smith muốn tạo một tài
khoản đăng nhập SQL Server cho Shiraishi và cấp phát truy
cập tới các cửa sổ đăng nhập “SQL2005/Shiraishi” một
Simpo PDF Merge and Split Unregistered Version -
cách dễ dàng. Thêm vào đó, anh ấy trao quyền chỉ đọc tới
bảng products trong giản đồ CompanyProducts. Tuy nhiên,
người quản lý không muốn Ms.Shiraishi có thể truy cập

vào bảng productprice trong cùng giản đồ đó.

Với tình huống đó chúng ta phải có cơ sở dữ liệu dưới,
CompanyProducts:
USE [master]
GO
/****** Object: Database [CompanyProducts]
Script Date: 03/26/2006 19:32:40 ******/
IF EXISTS (SELECT name FROM sys.databases
WHERE name = N'CompanyProducts')
DROP DATABASE [CompanyProducts]
go
create database CompanyProducts
go
USE [CompanyProducts]
GO
/****** Object: Schema [CompanyCustomers]
Script Date: 03/26/2006 19:33:45 ******/
Simpo PDF Merge and Split Unregistered Version -
IF EXISTS (SELECT * FROM sys.schemas
WHERE name = N'CompanyCustomers')
DROP SCHEMA [CompanyCustomers]
go
create Schema CompanyProducts
go
USE [CompanyProducts]
GO
/****** Object: Table
[CompanyProducts].[Products]
Script Date: 03/26/2006 19:34:32 ******/

IF EXISTS (SELECT * FROM sys.objects
WHERE object_id =
OBJECT_ID(N'[CompanyProducts].[Products]')
AND type in (N'U'))
DROP TABLE [CompanyProducts].[Products]
go
Create table CompanyProducts.Products
(id int, Name varchar(100))
go
insert into CompanyProducts.Products
Simpo PDF Merge and Split Unregistered Version -
select 1,'Refrigerator'
go
insert into CompanyProducts.Products
select 2,'Washing Machine'
go
insert into CompanyProducts.Products
select 3,'Dryer'
go
insert into CompanyProducts.Products
select 4,'Lawn Mower'
go
USE [CompanyProducts]
GO
/****** Object: Table [CompanyProducts].[ProductPrice]
Script Date: 03/26/2006 19:34:12 ******/
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id =
OBJECT_ID(N'[CompanyProducts].[ProductPrice]')
AND type in (N'U'))

DROP TABLE [CompanyProducts].[ProductPrice]
go
Simpo PDF Merge and Split Unregistered Version -
Create table CompanyProducts.ProductPrice
(id int, Price money)
go
insert into CompanyProducts.ProductPrice
select 1,7000
go
insert into CompanyProducts.ProductPrice
select 2,1000
go
insert into CompanyProducts.ProductPrice
select 3,1000
go
insert into CompanyProducts.ProductPrice
select 4,2500
go
Khi Mr.Smith là một người quản trị cơ sở dữ liệu, anh ấy
log on tới Management Studio sử dụng SA để đăng nhập.
Mr.Smith thực thi các câu lệnh dưới để tạo một tài khoản
đăng nhập và người dùng cho Ms.Shiraishi
Simpo PDF Merge and Split Unregistered Version -
use master
go
create login Shiraishi with password ='Sh!r@!sh!'
go
create login [SQL2005\Shiraishi] from windows
go
use CompanyProducts

go
Create user SQL_Shiraishi for LOGIN Shiraishi
go
Create user WIN_Shiraishi for LOGIN
[SQL2005\Shiraishi]
go
GRANT SELECT on CompanyProducts.Products to
SQL_Shiraishi,WIN_Shiraishi
go
DENY SELECT on CompanyProducts.ProductPrice to
SQL_Shiraishi,WIN_Shiraishi
Go
Mr.Smith muốn kiểm tra quyền cấp phép cho cả đăng nhập
SQL và Windows của Ms.Shiraishi. Khi đăng nhập SQL đã
Simpo PDF Merge and Split Unregistered Version -
được tạo bởi Smith, anh ấy sẽ biết password đăng nhập
Shiraishi và có thể kiểm tra quyền cấp phép trong Login
bằng cách sử dụng tiện ích SQLCMD hoặc Management
Studio.

Thật không hay là SQL2005\Shiraishi là một đăng nhập
Windows và Mr.Smith (hay bất kỳ một người quản trị hệ
thống nào) cũng không có password. Chỉ Ms.Shiraishi là có
nó và tất nhiên là không thể tự dưng hỏi một người nào đó
password của họ được.

Mr.Smith có thể kiểm tra các cấp phép trong cả đăng nhập
SQL và Windows bằng cách sử dụng câu lệnh EXECUTE
AS mới trong SQL Server 2005, vậy là vấn đề trên đã được
giải quyết.

use CompanyProducts
go
Execute as user = 'SQL_Shiraishi'
select * from CompanyProducts.Products
RESULT
1 Refrigerator
Simpo PDF Merge and Split Unregistered Version -
2 Washing Machine
3 Dryer
4 Lawn Mower
select * from CompanyProducts.ProductPrice
RESULT
Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'ProductPrice',
database 'CompanyProducts', schema 'CompanyProducts'.
Mr.Smith mở cửa sổ truy vấn mới và thực thi các câu lệnh
dưới đây
Execute as user = 'WIN_Shiraishi'
select * from CompanyProducts.Products
RESULT
1 Refrigerator
2 Washing Machine
3 Dryer
4 Lawn Mower

select * from CompanyProducts.ProductPrice
RESULT
Simpo PDF Merge and Split Unregistered Version -
Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'ProductPrice',

database 'CompanyProducts', schema 'CompanyProducts'.
Kết luận

Trong bài này, chúng tôi đã giải thích tác dụng của câu lệnh
EXECUTE AS mới trong SQL Server 2005. Nó sẽ rất có
ích cho các quản trị viên cơ sở dữ liệu khi cần kiểm tra các
cấp phép của một người dùng cụ thể.

Simpo PDF Merge and Split Unregistered Version -

×