14.2 實(shí)施保護(hù)領(lǐng)域?qū)ο蟮闹匾襟E
在熟悉Acegi中ACL子系統(tǒng)的各個(gè)術(shù)語及基本概念后,我們需要通過實(shí)際企業(yè)應(yīng)用驗(yàn)證它們,從而加深對它們的理解。
14.2.1 RDBMS表的建立
為了實(shí)施基于Acegi ACL領(lǐng)域?qū)ο笫跈?quán)支持的企業(yè)應(yīng)用,開發(fā)者必須建立好同ACL相關(guān)的4張RDBMS表。它們的名字和含義如下。
l acl_class:用于存儲領(lǐng)域?qū)ο髮?yīng)的全限定名,比如sample.contact.Contact。
l acl_sid:用于存儲ACL授權(quán)對象,或者是用戶名,或者是角色名。比如,marissa用戶、ROLE_USER角色。
l acl_object_identity:用于存儲領(lǐng)域?qū)ο髮?yīng)的Acl信息。
l acl_entry:用于存儲Acl(acl_object_identity)對應(yīng)的AccessControlEntry信息。默認(rèn)時(shí),acl_object_identity同acl_entry構(gòu)成了主從表關(guān)系,并以1:N關(guān)系存在。
圖14-8展示了這4張表間的具體關(guān)系,這是采用Hibernate Tools獲得的圖形化表示,此外,圖中還展示了Acegi contacts示例存儲領(lǐng)域?qū)ο蟮腸ontacts表、存儲用戶和角色信息的users和authorities表。值得開發(fā)者注意的是,contacts、users、authorities表與ACL對應(yīng)的4張表并無直接聯(lián)系。接下來,我們來一一研究這些表的設(shè)計(jì)和內(nèi)容。由于Acegi contacts示例采用了HSQLDB RDBMS,因此這些表對應(yīng)的SQL DDL語句不能夠直接用于其他生產(chǎn)數(shù)據(jù)庫,比如Oracle 10g、SQL Server 2005。在領(lǐng)會Acegi ACL的思想后,開發(fā)者能夠很輕松地給出特定數(shù)據(jù)庫版本的ACL表定義。

圖14-8 contacts示例設(shè)計(jì)的RDBMS表
其一,users表的定義如下:username列用于存儲用戶名,password列用于存儲密碼,enabled列用于存儲啟用標(biāo)志位。
create table users(
username varchar_ignorecase(50) not null primary key,
password varchar_ignorecase(50) not null,
enabled boolean not null
);
圖14-9給出了users表中已存儲的示例數(shù)據(jù)。這里的password列的內(nèi)容經(jīng)過了MD5加密處理。

圖14-9 users表已存儲的示例數(shù)據(jù)
其二,authorities表的定義如下:username列用于存儲用戶名,authority列用于存儲角色名。
create table authorities(
username varchar_ignorecase(50) not null,
authority varchar_ignorecase(50) not null,
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities(username,authority);
圖14-10給出了authorities表中已存儲的示例數(shù)據(jù),其中的username列數(shù)據(jù)引用到users表中username列的數(shù)據(jù)。默認(rèn)時(shí),users和authorities表處于1:N的關(guān)系。

圖14-10 authorities表已存儲的示例數(shù)據(jù)
其三,contacts表的定義如下:id列用于標(biāo)識領(lǐng)域?qū)ο?,contact_name列存儲聯(lián)系人的姓名,而email列用于存儲聯(lián)系人的郵件地址。
create table contacts(
id bigint not null primary key,
contact_name varchar_ignorecase(50) not null,
email varchar_ignorecase(50) not null
);
圖14-11給出了contacts表中已存儲的示例數(shù)據(jù)。

圖14-11 contacts表已存儲的示例數(shù)據(jù)
在實(shí)際企業(yè)應(yīng)用中,存在大量的領(lǐng)域?qū)ο?,因而會存在大量的RDBMS表來存儲它們。類似地,實(shí)際企業(yè)應(yīng)用存儲用戶、角色信息的RDBMS表也不一定是users、authorities表,相信仔細(xì)閱讀過本書相關(guān)章節(jié)的開發(fā)者都知道如何自定義它們。好了,再來研究4張ACL表的SQL DDL語句及相應(yīng)的示例數(shù)據(jù)。
其四,acl_class表的定義如下:id列用于標(biāo)識領(lǐng)域?qū)ο髮?yīng)的全限定名,而class列用于存儲領(lǐng)域?qū)ο髮?yīng)的全限定名。
create table acl_class(
id bigint generated by default as identity(start with 100) not null primary key,
class varchar_ignorecase(100) not null,
constraint unique_uk_2 unique(class)
);
圖14-12給出了acl_class表中已存儲的示例數(shù)據(jù)。由于Acegi contacts示例僅僅存在單個(gè)Contact領(lǐng)域?qū)ο箢愋?,因此這一表僅僅存在單條記錄。

圖14-12 acl_class表已存儲的示例數(shù)據(jù)
其五,acl_sid表的定義如下:principal列用于給定sid列的類型,即同一記錄中存儲了用戶名還是角色名。相比之下,sid列存儲用戶名或者角色名。如果sid列存儲了用戶名,則principal列取值為true;如果sid列存儲了角色名,則principal列取值為false。
create table acl_sid(
id bigint generated by default as identity(start with 100) not null primary key,
principal boolean not null,
sid varchar_ignorecase(100) not null,
constraint unique_uk_1 unique(sid,principal)
);
圖14-13給出了acl_sid表中已存儲的示例數(shù)據(jù)。由于sid列存儲的數(shù)據(jù)都是用戶名,因此principal列取值都為true。在某種程度上,acl_sid對應(yīng)于Sid對象。

圖14-13 acl_sid表已存儲的示例數(shù)據(jù)
其六,acl_object_identity表的定義如下:id列為主鍵。其中,object_id_class引用到acl_class中的id列取值,圖14-14中的object_id_class列取值都是100,即Contact領(lǐng)域?qū)ο箢愋?。類似地,object_id_identity列的內(nèi)容起到綁定特定領(lǐng)域?qū)ο蟮淖饔谩1热?,在contacts示例應(yīng)用中,object_id_identity列的內(nèi)容同contacts表中id列的內(nèi)容保持一致。注意,開發(fā)者也可以通過其他策略維護(hù)object_id_identity列同特定領(lǐng)域?qū)ο箝g的綁定關(guān)系。默認(rèn)時(shí),我們建議領(lǐng)域?qū)ο蠖寄軌蚝衖d屬性,因?yàn)镺bjectIdentityImpl可能會通過反射機(jī)制調(diào)用到領(lǐng)域?qū)ο蟮膅etId()方法,并將返回結(jié)果存儲到object_id_identity列。最后,parent_object列用于指定當(dāng)前ACL的父ACL,而own_sid列用于存儲當(dāng)前ACL的主人,entries_inheriting列用于指定當(dāng)前ACL是否繼承父ACL(含有的ACE集合)??梢钥闯?,owner_sid列取值引用到acl_sid中id列取值。
create table acl_object_identity(
id bigint generated by default as identity(start with 100) not null primary key,
object_id_class bigint not null,
object_id_identity bigint not null,
parent_object bigint,
owner_sid bigint,
entries_inheriting boolean not null,
constraint unique_uk_3 unique(object_id_class,object_id_identity),
constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id)
);
圖14-14給出了RDBMS acl_object_identity表中已存儲的示例數(shù)據(jù)。在某種程度上,acl_object_identity對應(yīng)于Acl對象。

圖14-14 acl_object_identity表已存儲的示例數(shù)據(jù)
其七,acl_entry表的定義如下:id列是主鍵。其中,acl_object_identity表指定當(dāng)前ACE所屬的ACL,即對應(yīng)到acl_object_identity表中的id列。類似地,sid列用于存儲當(dāng)前ACE所屬的授權(quán)對象,它對應(yīng)于acl_sid表中的id列;ace_order列對ACE集合進(jìn)行排序;mask列存儲ACE權(quán)限信息,具體情況請參考BasePermission和CumulativePermission類;granting列表明當(dāng)前的ACE權(quán)限是否已經(jīng)授給了同一記錄持有的sid列(用戶名或角色);audit_success和audit_failure列用于審計(jì)目的,供各種基于Acegi ACL的管理工具在審計(jì)ACL(ACE)時(shí)使用。開發(fā)者可以借助于Acegi暴露的API開發(fā)出相應(yīng)的審計(jì)(管理)工具。
create table acl_entry(
id bigint generated by default as identity(start with 100) not null primary key,
acl_object_identity bigint not null,
ace_order int not null,
sid bigint not null,
mask integer not null,
granting boolean not null,
audit_success boolean not null,
audit_failure boolean not null,
constraint unique_uk_4 unique(acl_object_identity,ace_order),
constraint foreign_fk_4 foreign key(acl_object_identity) references acl_object_identity(id),
constraint foreign_fk_5 foreign key(sid) references acl_sid(id)
);
圖14-15給出了RDBMS acl_entry表中已存儲的示例數(shù)據(jù)。在某種程度上,acl_entry對應(yīng)于AccessControlEntry對象。

圖14-15 acl_entry表已存儲的示例數(shù)據(jù)