資訊系統開發平台 OpenExpressApp (OEA) 採用 RBAC 權限模型,基礎在於使用者管理。CSLA 框架提供 BusinessPrincipalBase、CslaIdentity 等基礎類別,快速實作自訂 Principal/Identity。
繼承 BusinessPrincipalBase,實作靜態 Login/Logout:
public class OEAPrincipal : BusinessPrincipalBase {
private OEAPrincipal(IIdentity identity) : base(identity) { }
public static bool Login(string username, string password) {
var identity = OEAIdentity.GetIdentity(username, password);
if (identity.IsAuthenticated) {
Csla.ApplicationContext.User = new OEAPrincipal(identity);
return true;
}
Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
return false;
}
public static void Logout() => Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
}
繼承 CslaIdentity,透過 DataPortal.Fetch 向伺服器驗證並載入角色:
[Serializable]
public class OEAIdentity : CslaIdentity {
public User User { get; set; }
public new OrgPositions Roles { get; private set; }
private OEAIdentity() { }
internal static OEAIdentity GetIdentity(string username, string password) =>
DataPortal.Fetch<OEAIdentity>(new UsernameCriteria(username, password));
private void DataPortal_Fetch(UsernameCriteria criteria) {
User = UserList.Get(criteria.Username, criteria.Password);
if (User != null) {
base.Name = User.Name; base.IsAuthenticated = true;
Roles = OrgPositions.GetList(User.Id); // 從安全存儲載入角色
} else { base.Name = string.Empty; base.IsAuthenticated = false; Roles = null; }
}
}
登入成功後,Csla.ApplicationContext.User 保存 Principal,後續授權檢查可透過 (Csla.ApplicationContext.User.Identity as OEAIdentity).Roles 取得角色集合。下一篇將介紹功能權限控制。