diff --git a/AsbCloudInfrastructure/Services/AuthService.cs b/AsbCloudInfrastructure/Services/AuthService.cs index a8bf9448..d54c3f2e 100644 --- a/AsbCloudInfrastructure/Services/AuthService.cs +++ b/AsbCloudInfrastructure/Services/AuthService.cs @@ -27,6 +27,7 @@ namespace AsbCloudInfrastructure.Services private static readonly TimeSpan expiresTimespan = TimeSpan.FromDays(365.25); private static readonly Encoding encoding = Encoding.UTF8; private const int PasswordSaltLength = 5; + private const string claimIdUser = "id"; private const string claimNameidCompany = "idCompany"; private readonly HashAlgorithm hashAlgoritm; private readonly Random rnd; @@ -157,6 +158,7 @@ namespace AsbCloudInfrastructure.Services var claims = new List { + new Claim(claimIdUser, user.Id.ToString()), new Claim(ClaimsIdentity.DefaultNameClaimType, user.Login), new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role?.Caption??"GUEST"), new Claim(claimNameidCompany, user.IdCompany.ToString()), diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index 45a7accc..8ca5d949 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -37,11 +37,12 @@ namespace AsbCloudWebApi.Controllers [HttpPost] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task SaveFilesAsync(int idWell, int idCategory, - int idUser, [FromForm] IFormFileCollection files, CancellationToken token = default) + [FromForm] IFormFileCollection files, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); + int? idUser = User.GetUserId(); - if (idCompany is null) + if (idCompany is null || idUser is null) return Forbid(); if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, @@ -49,7 +50,7 @@ namespace AsbCloudWebApi.Controllers return Forbid(); var fileInfoCollection = files.Select(f => - (f.FileName, idWell, idCategory, DateTime.Now, idUser)); + (f.FileName, idWell, idCategory, DateTime.Now, (int)idUser)); var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(idWell, idCategory, fileInfoCollection); diff --git a/AsbCloudWebApi/Extensions.cs b/AsbCloudWebApi/Extensions.cs index ffcebc3c..6ec5e0c0 100644 --- a/AsbCloudWebApi/Extensions.cs +++ b/AsbCloudWebApi/Extensions.cs @@ -15,5 +15,16 @@ namespace AsbCloudWebApi ? uid : null; } + + public static int? GetUserId(this ClaimsPrincipal user) + { + var userId = user.FindFirst(nameof(User.Id)); + if (userId is null) + return null; + + return int.TryParse(userId.Value, out int uid) + ? uid + : null; + } } }