给定以下代码:
public class BackupsController : ApiController
{
private readonly IApiContext context;
private readonly IBackupService backupService;
public BackupsController(IApiContext context, IBackupService backupService)
{
this.context = context;
this.backupService = backupService;
}
public HttpResponseMessage Get(Guid id)
{
if (id == Guid.Empty)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
IBackupView backup = backupService.Get(id);
if (backup == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format("BackupId '{0}' not found.", id));
}
if (!IsAuthorizedForBackup(backup))
{
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
return Request.CreateResponse(HttpStatusCode.OK, backup);
}
private bool IsAuthorizedForBackup(IBackupView backup)
{
if (context.Principal.IsInRole(MembershipRole.Admin))
{
return true;
}
if (context.Principal.AllowDataSharing && backup.UserId == context.Principal.UserId)
{
return true;
}
if (backup.UserId == context.Principal.UserId && backup.Device.Uuid == context.DeviceUuid)
{
return true;
}
return false;
}
}
将几乎所有方法主体提取到授权过滤器中是否有意义?如果不检索两次备份,我看不到这样做的方法。
您将如何将授权问题与控制器操作分开?