2020-04-22 15:46:35 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using zero.Core.Api;
|
|
|
|
|
using zero.Web.Models;
|
|
|
|
|
|
|
|
|
|
namespace zero.Web.Filters
|
|
|
|
|
{
|
|
|
|
|
public class AddTokenAttribute : TypeFilterAttribute
|
|
|
|
|
{
|
|
|
|
|
public AddTokenAttribute() : base(typeof(AddTokenAttributeImpl)) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class AddTokenAttributeImpl : IAsyncResultFilter
|
|
|
|
|
{
|
|
|
|
|
IToken token;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AddTokenAttributeImpl(IToken token)
|
|
|
|
|
{
|
|
|
|
|
this.token = token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
|
|
|
|
|
{
|
|
|
|
|
if (context.Result is JsonResult)
|
|
|
|
|
{
|
|
|
|
|
JsonResult result = context.Result as JsonResult;
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
if (result.Value is ObsoleteEditModel)
|
2020-04-22 15:46:35 +02:00
|
|
|
{
|
2020-05-22 15:06:17 +02:00
|
|
|
ObsoleteEditModel model = result.Value as ObsoleteEditModel;
|
|
|
|
|
|
|
|
|
|
model.Meta = model.Meta ?? new EditModelMeta();
|
|
|
|
|
model.Meta.Token = await token.Get(model.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.Value is ObsoleteEditModel)
|
|
|
|
|
{
|
|
|
|
|
ObsoleteEditModel model = result.Value as ObsoleteEditModel;
|
2020-04-22 15:46:35 +02:00
|
|
|
|
2020-04-28 14:51:17 +02:00
|
|
|
model.Meta = model.Meta ?? new EditModelMeta();
|
|
|
|
|
model.Meta.Token = await token.Get(model.Id);
|
2020-04-22 15:46:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|