Projet de stage 2021

This commit is contained in:
2021-06-18 10:37:33 +02:00
commit eb522143cf
314 changed files with 202141 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
using key4intranet.authentication.Models;
using key4intranet.authentication.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
namespace key4intranet.Api.Controllers
{
[ApiController]
[Authorize]
[Route("api/auth")]
public class AuthenticationController : ControllerBase
{
private readonly ILogger<AuthenticationController> _logger;
private readonly IAuthenticationService _authenticationService;
public AuthenticationController(ILogger<AuthenticationController> logger, IAuthenticationService authenticationService)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_authenticationService = authenticationService ?? throw new ArgumentNullException(nameof(authenticationService));
}
[AllowAnonymous]
[HttpPost]
public IActionResult Authenticate([FromBody] PostUserModel model)
{
if (model == null) { return Unauthorized(); }
if (string.IsNullOrWhiteSpace(model.User)) { return Unauthorized(); }
if (string.IsNullOrWhiteSpace(model.Pass)) { return Unauthorized(); }
var user = _authenticationService.Authenticate(model);
if (user == null) { return Unauthorized(); }
var returnUser = new ExposedUserModel
{
Id = user.Id,
Name = user.Name,
Roles = user.Roles,
};
var token = _authenticationService.GetToken(user);
if (string.IsNullOrWhiteSpace(token)) { return Unauthorized(); }
returnUser.Token = token;
return Ok(returnUser);
}
[HttpPost("encrypt")]
public string EncryptPhrase([FromBody] string phrase)
{
if (string.IsNullOrWhiteSpace(phrase)) { throw new ArgumentNullException(nameof(phrase)); }
return (new PasswordHasher<string>()).HashPassword(null, phrase);
}
}
}

View File

@@ -0,0 +1,74 @@
using key4.HttpExceptions;
using key4intranet.Api.Docs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading.Tasks;
namespace key4intranet.Api.Controllers
{
[ApiController]
[Authorize]
public class DownloadController : ControllerBase
{
private readonly IDocumentManager _documentManager;
public DownloadController(IDocumentManager documentManager)
{
_documentManager = documentManager ?? throw new ArgumentNullException(nameof(documentManager));
}
[Route("api/download/id/{fileId}/")]
public Task<ActionResult> SearchFile(String fileId)
{
if (_documentManager.dl.HasFile(fileId))
{
return DownloadFile(_documentManager.dl.GetDocumentFromLibrary(fileId));
}
else
{
throw new HttpNotFoundException("error - file not found in library");
}
}
[Route("api/download/getLibrary/")]
public IActionResult getFilesFromLibraryToJSON()
{
_documentManager.KnowAllDocuments();
return Ok(_documentManager.dl.GetAllDocuments());
}
[Route("api/download/getLibrary/{group}/")]
public IActionResult getFilesFromLibraryToJSONwithSpecificDepartment(String group)
{
_documentManager.KnowAllDocuments();
return Ok(_documentManager.dl.GetAllDocumentsFromGroup(_documentManager.GetDepartement(group)));
}
[HttpGet]
private async Task<ActionResult> DownloadFile(Document d)
{
// validation and get the file
var filePath = d.getPath();
if (!System.IO.File.Exists(filePath))
{
await System.IO.File.WriteAllTextAsync(filePath, d.fileName);
}
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(filePath, out var contentType))
{
contentType = "application/octet-stream";
}
var bytes = await System.IO.File.ReadAllBytesAsync(filePath);
return File(bytes, contentType, Path.GetFileName(filePath));
}
}
}

View File

@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace key4intranet.Api.Controllers
{
[ApiController]
[Authorize]
[Route("api/echo")]
public class EchoController : ControllerBase
{
[AllowAnonymous]
[HttpGet]
public IActionResult GetEcho()
{
return Ok($"echo { GetProjectName() }");
}
[HttpGet("authorized")]
public IActionResult GetAuthorizedEcho()
{
return Ok($"authorized echo { GetProjectName() }");
}
private string GetProjectName() => this.GetType().Namespace.Split('.')[0];
}
}