2020-03-22 14:47:59 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-10-07 16:08:09 +02:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2020-05-11 15:34:47 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-06-03 12:57:29 +02:00
|
|
|
using System;
|
2020-04-24 12:46:25 +02:00
|
|
|
using System.Collections.Generic;
|
2020-10-07 16:08:09 +02:00
|
|
|
using System.IO;
|
2020-04-19 18:22:44 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-06-03 12:57:29 +02:00
|
|
|
using zero.Core.Api;
|
2020-04-22 15:46:35 +02:00
|
|
|
using zero.Core.Entities;
|
2020-04-16 00:56:22 +02:00
|
|
|
using zero.Core.Identity;
|
2020-05-14 13:32:33 +02:00
|
|
|
using zero.Core.Options;
|
2020-04-28 14:51:17 +02:00
|
|
|
using zero.Web.Filters;
|
2020-04-22 15:46:35 +02:00
|
|
|
using zero.Web.Models;
|
2020-03-22 14:47:59 +01:00
|
|
|
|
2020-03-24 23:09:29 +01:00
|
|
|
namespace zero.Web.Controllers
|
2020-03-22 14:47:59 +01:00
|
|
|
{
|
2020-05-28 00:36:52 +02:00
|
|
|
[ZeroAuthorize]
|
2020-08-13 12:03:24 +02:00
|
|
|
[ServiceFilter(typeof(ModelStateValidationFilterAttribute))]
|
2020-10-27 15:47:23 +01:00
|
|
|
[ApiController]
|
|
|
|
|
[Route("getsreplaced/[controller]/[action]")]
|
2020-11-18 12:00:51 +01:00
|
|
|
[ServiceFilter(typeof(BackofficeFilterAttribute))]
|
2020-10-27 15:47:23 +01:00
|
|
|
public abstract class BackofficeController : ControllerBase
|
2020-03-22 14:47:59 +01:00
|
|
|
{
|
2020-05-11 15:34:47 +02:00
|
|
|
IZeroOptions _options;
|
2020-04-19 18:22:44 +02:00
|
|
|
|
2021-09-23 11:29:10 +02:00
|
|
|
public bool IsCoreDatabase { get; protected set; }
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
protected IZeroOptions Options => _options ?? (_options = HttpContext?.RequestServices?.GetService<IZeroOptions>());
|
2020-04-19 18:22:44 +02:00
|
|
|
|
2020-05-27 18:29:36 +02:00
|
|
|
|
2020-11-18 12:00:51 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Is execuated when the scope changes.
|
|
|
|
|
/// The scope is evaluated by the BackofficeFilterAttribute.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void OnScopeChanged(string scope) { }
|
|
|
|
|
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an edit model with appropriate options and permissions
|
|
|
|
|
/// </summary>
|
2021-05-04 17:23:52 +02:00
|
|
|
public EditModel<T> Edit<T>(T data, bool typed = true, Action<EditModel<T>> transform = null) where T : ZeroIdEntity
|
2020-05-22 15:06:17 +02:00
|
|
|
{
|
2020-06-03 12:57:29 +02:00
|
|
|
Type type = typeof(T);
|
|
|
|
|
|
|
|
|
|
//ControllerContext.ActionDescriptor.FilterDescriptors[0].
|
|
|
|
|
|
2020-09-06 15:34:47 +02:00
|
|
|
if (data == null)
|
|
|
|
|
{
|
2020-10-27 15:47:23 +01:00
|
|
|
return null;
|
2020-09-06 15:34:47 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 14:35:08 +02:00
|
|
|
EditModel<T> model = new EditModel<T>();
|
2020-06-17 15:55:58 +02:00
|
|
|
model.Entity = data;
|
2021-10-13 12:47:18 +02:00
|
|
|
model.Meta.Token = null; // Token.Get(data);
|
2020-11-14 12:34:26 +01:00
|
|
|
//model.Meta.IsAppAware = AppAwareType.IsAssignableFrom(type); // TODO appx
|
|
|
|
|
//model.Meta.CanBeShared = canBeShared;
|
2020-06-24 13:00:50 +02:00
|
|
|
model.Meta.CanCreate = true;
|
2020-11-14 12:34:26 +01:00
|
|
|
//model.Meta.CanCreateShared = canBeShared;
|
2020-06-24 13:00:50 +02:00
|
|
|
model.Meta.CanEdit = true;
|
|
|
|
|
model.Meta.CanDelete = true;
|
2021-09-23 11:29:10 +02:00
|
|
|
model.Meta.IsShared = IsCoreDatabase;
|
2020-06-17 15:55:58 +02:00
|
|
|
|
|
|
|
|
transform?.Invoke(model);
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
return model;
|
2020-05-22 15:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-20 14:35:08 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an edit model with appropriate options and permissions
|
|
|
|
|
/// </summary>
|
2020-10-27 15:47:23 +01:00
|
|
|
public TWrapper Edit<T, TWrapper>(TWrapper data, bool typed = true, Action<EditModel<T>> transform = null)
|
2021-05-04 17:23:52 +02:00
|
|
|
where T : ZeroIdEntity
|
2020-08-20 14:35:08 +02:00
|
|
|
where TWrapper : EditModel<T>, new()
|
|
|
|
|
{
|
|
|
|
|
Type type = typeof(T);
|
|
|
|
|
|
|
|
|
|
//ControllerContext.ActionDescriptor.FilterDescriptors[0].
|
|
|
|
|
|
2021-10-13 12:47:18 +02:00
|
|
|
data.Meta.Token = null; // Token.Get(data.Entity);
|
2020-11-14 12:34:26 +01:00
|
|
|
//data.Meta.IsAppAware = AppAwareType.IsAssignableFrom(type); // TODO appx
|
|
|
|
|
//data.Meta.CanBeShared = canBeShared;
|
2020-08-20 14:35:08 +02:00
|
|
|
data.Meta.CanCreate = true;
|
2020-11-14 12:34:26 +01:00
|
|
|
//data.Meta.CanCreateShared = canBeShared;
|
2020-08-20 14:35:08 +02:00
|
|
|
data.Meta.CanEdit = true;
|
|
|
|
|
data.Meta.CanDelete = true;
|
2021-09-23 11:29:10 +02:00
|
|
|
data.Meta.IsShared = IsCoreDatabase;
|
2020-08-20 14:35:08 +02:00
|
|
|
|
|
|
|
|
transform?.Invoke(data);
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
return data;
|
2020-08-20 14:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public IList<PreviewModel> Previews<T>(Dictionary<string, T> items, Func<T, PreviewModel> transform)
|
2020-06-12 16:13:47 +02:00
|
|
|
{
|
|
|
|
|
IList<PreviewModel> previews = new List<PreviewModel>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
bool exists = item.Value != null;
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
{
|
|
|
|
|
previews.Add(new PreviewModel()
|
|
|
|
|
{
|
|
|
|
|
HasError = true,
|
|
|
|
|
Icon = "fth-alert-circle color-red",
|
|
|
|
|
Id = item.Key,
|
|
|
|
|
Name = "@errors.preview.notfound",
|
|
|
|
|
Text = "@errors.preview.notfound_text"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
previews.Add(transform(item.Value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
return previews;
|
2020-06-12 16:13:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-04 17:23:52 +02:00
|
|
|
public IList<PreviewModel> Previews<T>(Dictionary<string, T> items, Action<T, PreviewModel> transform = null) where T : ZeroEntity
|
2020-11-18 12:00:51 +01:00
|
|
|
{
|
|
|
|
|
IList<PreviewModel> previews = new List<PreviewModel>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
if (item.Value == null)
|
|
|
|
|
{
|
|
|
|
|
previews.Add(new PreviewModel()
|
|
|
|
|
{
|
|
|
|
|
HasError = true,
|
|
|
|
|
Icon = "fth-alert-circle color-red",
|
|
|
|
|
Id = item.Key,
|
|
|
|
|
Name = "@errors.preview.notfound",
|
|
|
|
|
Text = "@errors.preview.notfound_text"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-13 13:51:55 +01:00
|
|
|
PreviewModel model = new()
|
2020-11-18 12:00:51 +01:00
|
|
|
{
|
|
|
|
|
Id = item.Value.Id,
|
|
|
|
|
Name = item.Value.Name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
transform?.Invoke(item.Value, model);
|
|
|
|
|
|
|
|
|
|
previews.Add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return previews;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-05-22 15:06:17 +02:00
|
|
|
|
2021-05-04 17:23:52 +02:00
|
|
|
public async Task<IList<SelectModel>> SelectList<T>(IAsyncEnumerable<T> enumerable, Action<T, SelectModel> transform = null) where T : ZeroEntity
|
2020-11-18 01:26:12 +01:00
|
|
|
{
|
|
|
|
|
List<SelectModel> items = new List<SelectModel>();
|
|
|
|
|
|
|
|
|
|
await foreach (T item in enumerable)
|
|
|
|
|
{
|
2021-01-13 13:51:55 +01:00
|
|
|
SelectModel model = new()
|
2020-11-18 01:26:12 +01:00
|
|
|
{
|
|
|
|
|
Id = item.Id,
|
|
|
|
|
Name = item.Name,
|
|
|
|
|
IsActive = item.IsActive
|
2021-01-13 13:51:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
transform?.Invoke(item, model);
|
|
|
|
|
|
|
|
|
|
items.Add(model);
|
2020-11-18 01:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<IList<PreviewModel>> Previews<T>(Dictionary<string, T> items, Func<T, Task<PreviewModel>> transform)
|
2020-06-17 15:55:58 +02:00
|
|
|
{
|
|
|
|
|
IList<PreviewModel> previews = new List<PreviewModel>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
bool exists = item.Value != null;
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
{
|
|
|
|
|
previews.Add(new PreviewModel()
|
|
|
|
|
{
|
|
|
|
|
HasError = true,
|
|
|
|
|
Icon = "fth-alert-circle color-red",
|
|
|
|
|
Id = item.Key,
|
|
|
|
|
Name = "@errors.preview.notfound",
|
|
|
|
|
Text = "@errors.preview.notfound_text"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
previews.Add(await transform(item.Value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
return previews;
|
2020-06-17 15:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-10-07 16:08:09 +02:00
|
|
|
protected IActionResult Download(Stream stream, string filename)
|
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
{
|
|
|
|
|
// TODO add success property + return error response
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filename.Contains("{date}"))
|
|
|
|
|
{
|
|
|
|
|
filename = filename.Replace("{date}", DateTimeOffset.Now.ToString("yyyy-MM-dd"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
|
|
|
if (filename == null || !provider.TryGetContentType(Path.GetFileName(filename), out string contentType))
|
|
|
|
|
{
|
|
|
|
|
contentType = "application/octet-stream";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Response.Headers.Add("zero-filename", filename);
|
|
|
|
|
|
|
|
|
|
return base.File(stream, contentType, filename, true);
|
|
|
|
|
}
|
2021-09-14 14:27:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
protected IActionResult File(Core.Entities.FileResult file)
|
|
|
|
|
{
|
|
|
|
|
if (file == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileStream stream = System.IO.File.OpenRead(file.Path);
|
|
|
|
|
return File(stream, file.ContentType, file.Filename);
|
|
|
|
|
}
|
2020-03-22 14:47:59 +01:00
|
|
|
}
|
|
|
|
|
}
|