我一直在玩 RavenDB,想使用授权包来控制权限。但是,我无法确定将复杂代码(复杂的业务逻辑繁重)组合成可重用块的最佳方法,同时保持授权不变。
对于复杂的代码,我将它们提取到一组通用的方法中,以便可以重用代码。这些方法中的每一个都大致映射到一个授权操作,并且方法本身执行自己的session.SecureFor
但是,它似乎session.SecureFor
适用于会话级别,并且仅适用于SecureFor
SaveChanges 之前进行的最后一次调用。
例如
public void AuthorizeAndSchedule()
{
using (var session = store.OpenSession())
{
dynamic patient = new ExpandoObject();
session.Store(patient);
session.SetAuthorizationFor((object)patient, new DocumentAuthorization
{
Tags = {"Patient" }
});
ScheduleAppointment(session, "Authorization/Users/DrHowser", patient);
Hospitalize(session, "Authorization/Users/DrHowser", patient);
session.SaveChanges();
}
}
protected void Hospitalize(IDocumentSession session, string requester, dynamic patient)
{
session.SecureFor(requester, "Hospitalization/Authorize");
//assume other more complicated things are going on here
patient.HospitilizationAuthorized = true;
session.Store(patient);
}
protected void ScheduleAppointment(IDocumentSession session, string requester, dynamic patient)
{
session.SecureFor(requester, "Hospitalization/ScheduleAppointment");
//assume other more complicated things are going on here
patient.AppointmentScheduledFor = DateTime.Now.AddDays(1);
session.Store(patient);
}
在上面的代码中,如果 Howser 博士有权进行住院治疗但没有 ScheduleAppointment 的权限,则此代码仍将成功并且患者被安排预约 - 这是因为 Hospitlize 已覆盖任何安排预约的权限要求
每个会话只允许一次操作吗?如果我想执行两个单独的操作,我必须打开两个不同的会话吗?...或者我只是在接近这个完全错误的