關於級聯(樹形權限)的基本實現思路



本文介紹Portal里實現級聯權限的思路。其實windows 文件夾權限本身就是一個典型的級聯權限,參考下圖。

在windows文件夾權限里,在“安全‘里,有一項”包括可從改對象的父項集成的權限“。默認是選中的。

這樣的好處是,假如我們有一個”人事部“文件夾,設置只有人事部的人員可以訪問,我們可以在人事部里建立無限個子文件夾,這些文件夾默認也都是只能是人事部人員可以訪問,不需要管理員單獨賦予權限。

 

在我們Portal系統里,也采用了類似簡化的權限設置

下面介紹實現方式:

 很顯然,我們需要有一張權限表(tabsecurity存儲每一個頁面的具體記錄,數據庫結構如下)

下面顯示了填充數據后的結果,

attribute取值為group或者user,表示節點是組或者用戶屬性,

name為組名或者用戶名。

Roletype為0是只讀,為1為可寫,如果為2表示可以設計。

 

 

 

接下來,還需要設計tab表,他記錄頁面權限是否是繼承的--inherit字段,
這里inherit並沒有放置到tabsecurity表里,我們僅把權限當做頁面的一個屬性。

如果是1表示自動繼承父權限,為0表示非繼承。

 

 

有了上面的結構,即可在用戶訪問頁面時,獲取其權限,下面就是實現這個功能核心的SQL語句。

 

當用戶訪問頁面時,會建立一個臨時表#a, 其中 while @parentid>0 and @inherit>0 表示如果如果當前節點是支持繼承的,而且他還有父節點 就繼續循環父節點的權限,然后給此節點 系統執行完畢后,會得到當前節點實際權限列表

 

   public static DataView GetTabSecurityRolesTree(int tabid, string sectype)
{
string sql = @"
declare @parentid int
declare @id int
declare @inherit int

if object_id('tempdb..#a') is not null
Begin
drop table #a
End

create table #a(id int, parentid int)
set @id=
"+tabid+ @"
select @parentid=parentid, @id=id ,@inherit=inherit from portal_pages where id=@id

insert into #a(id,parentid) values(@id,@id)
while @parentid>0 and @inherit>0
begin select @parentid=parentid, @id=id, @inherit=inherit from portal_pages where id=@parentid
insert into #a(id,parentid) values(@id,@parentid)
set @id=@parentid
end

select * from portal_tabsecurity inner join #a on #a.id=tabid


";

DataView dv = new DataView( DBHelper.Instance.ExeDataSet(sql).Tables[0] );
dv.RowFilter = " sectype='" + sectype + "' ";
return dv;
}

 

有上面得到頁面的權限,然后在獲取當前用戶已有的權限,一比較,就能夠確認用戶是否可以訪問

 

 

然后檢查他是否有權限。



private bool CheckAuthenticatedPermission(string[] PageRoles, string[] myroles)

{

if (Array.IndexOf(PageRoles, "everyone") != -1)

return true;


if (Array.IndexOf(PageRoles, "authenticated") != -1)

return true;



foreach (string myrole in myroles)

{

if (!string.IsNullOrEmpty(myrole))

{

foreach (string searchrole in PageRoles)

{

if (myrole == searchrole)

{
return true;

}

}
}





}





return false;

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM