136 lines
4.1 KiB
C#
136 lines
4.1 KiB
C#
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using DomainResults.Common;
|
|
using DomainResults.Mvc;
|
|
|
|
using DataProviders.Buckets;
|
|
using WeatherForecast.Services;
|
|
using WeatherForecast.Models.Template.Requests;
|
|
using Core.Binders;
|
|
using Core.Enumerations;
|
|
using DomainObjects.Documents.Users;
|
|
using WeatherForecast.Policies;
|
|
|
|
namespace WeatherForecast.Controllers;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[AllowAnonymous]
|
|
[Route("api/[controller]")]
|
|
public class TemplateController : ControllerBase {
|
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
private readonly ITemplateBucketDataProvider _templateBucketDataProvider;
|
|
private readonly ITemplateService _templateService;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="authorizationService"></param>
|
|
/// <param name="templateBucketDataProvider"></param>
|
|
/// <param name="templateService"></param>
|
|
public TemplateController(
|
|
IAuthorizationService authorizationService,
|
|
ITemplateBucketDataProvider templateBucketDataProvider,
|
|
ITemplateService templateService
|
|
) {
|
|
_authorizationService = authorizationService;
|
|
_templateBucketDataProvider = templateBucketDataProvider;
|
|
_templateService = templateService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Allows to upload private dkim certificate
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="requestData"></param>
|
|
/// <param name="formFile"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("{siteId}")]
|
|
public async Task<IActionResult> Post([FromRoute] Guid siteId, [ModelBinder(typeof(JsonModelBinder<PostTemplateRequestModel>))] PostTemplateRequestModel? requestData, IFormFile formFile) {
|
|
|
|
if (!(formFile.Length > 0))
|
|
return IDomainResult.Failed().ToActionResult();
|
|
|
|
using var ms = new MemoryStream();
|
|
formFile.CopyTo(ms);
|
|
|
|
var newFile = new BucketFile(siteId, formFile.FileName, ms.ToArray(), formFile.ContentType);
|
|
|
|
var authorizationResult = await _authorizationService.AuthorizeAsync(User, new List<BucketFile> { newFile }, new TemplateAuthorisationRequirement {
|
|
Action = CrudActions.Create,
|
|
Roles = new List<TemplateRole> {
|
|
new TemplateRole { Role = Roles.Admin }
|
|
}
|
|
});
|
|
|
|
if (authorizationResult.Succeeded) {
|
|
var result = _templateService.Post(newFile);
|
|
return result.ToActionResult();
|
|
}
|
|
|
|
return Unauthorized();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{siteId}/{fileId}")]
|
|
public async Task<IActionResult> Get([FromRoute] Guid siteId, [FromRoute] Guid fileId) {
|
|
|
|
var (file, result) = _templateBucketDataProvider.Download(siteId, fileId);
|
|
if (!result.IsSuccess || file == null)
|
|
return result.ToActionResult();
|
|
|
|
var authorizationResult = await _authorizationService.AuthorizeAsync(User, new List<BucketFile> { file }, new TemplateAuthorisationRequirement {
|
|
Action = CrudActions.Read,
|
|
Roles = new List<TemplateRole> {
|
|
new TemplateRole { Role = Roles.Admin }
|
|
}
|
|
});
|
|
|
|
if (authorizationResult.Succeeded) {
|
|
var stream = new MemoryStream(file.Bytes);
|
|
return new FileStreamResult(stream, file.ContentType) {
|
|
FileDownloadName = file.Name
|
|
};
|
|
}
|
|
|
|
return Unauthorized();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <param name="requestData"></param>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{siteId}/{fileId}")]
|
|
public IActionResult Put([FromRoute] Guid siteId, [FromRoute] Guid fileId, [ModelBinder(typeof(JsonModelBinder<PutTemplateRequestModel>))] PutTemplateRequestModel requestData, IFormFile file) {
|
|
return BadRequest();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete template
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="fileId"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{siteId}/{fileId}")]
|
|
public IActionResult Delete([FromRoute] Guid siteId, [FromRoute] Guid fileId) {
|
|
|
|
var result = _templateService.Delete(siteId, fileId);
|
|
return result.ToActionResult();
|
|
}
|
|
}
|