2023-06-21 12:33:18 +05:00
|
|
|
|
using AsbCloudApp.Data.User;
|
2023-03-30 12:57:32 +05:00
|
|
|
|
using AsbCloudWebApi.Converters;
|
|
|
|
|
using System;
|
2023-09-28 16:25:29 +05:00
|
|
|
|
using System.Collections.Generic;
|
2023-03-30 12:57:32 +05:00
|
|
|
|
using System.ComponentModel;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
2023-03-30 12:57:32 +05:00
|
|
|
|
namespace Microsoft.AspNetCore.Mvc
|
2021-05-12 16:03:14 +05:00
|
|
|
|
{
|
2022-02-12 11:28:16 +05:00
|
|
|
|
public static class Extentions
|
2021-05-12 16:03:14 +05:00
|
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
|
public static int? GetCompanyId(this ClaimsPrincipal user)
|
2021-05-12 16:03:14 +05:00
|
|
|
|
{
|
2021-10-12 11:03:05 +05:00
|
|
|
|
var claimIdCompany = user.FindFirst(nameof(UserDto.IdCompany));
|
2021-07-21 15:22:58 +05:00
|
|
|
|
if (claimIdCompany is null)
|
2021-05-12 16:03:14 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
|
return int.TryParse(claimIdCompany.Value, out int uid)
|
2021-05-12 16:03:14 +05:00
|
|
|
|
? uid
|
|
|
|
|
: null;
|
|
|
|
|
}
|
2021-08-17 13:03:17 +05:00
|
|
|
|
|
|
|
|
|
public static int? GetUserId(this ClaimsPrincipal user)
|
|
|
|
|
{
|
2021-10-12 11:03:05 +05:00
|
|
|
|
var userId = user.FindFirst(nameof(UserDto.Id));
|
2021-08-17 13:03:17 +05:00
|
|
|
|
if (userId is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return int.TryParse(userId.Value, out int uid)
|
|
|
|
|
? uid
|
|
|
|
|
: null;
|
|
|
|
|
}
|
2022-08-04 15:04:55 +05:00
|
|
|
|
|
2023-09-28 16:25:29 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Returns BadRequest with ValidationProblemDetails as body
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Используйте этот метод только если валидацию нельзя сделать через
|
|
|
|
|
/// атрибуты валидации или IValidatableObject модели.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="controller"></param>
|
|
|
|
|
/// <param name="paramName"></param>
|
|
|
|
|
/// <param name="error"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static BadRequestObjectResult ValidationBadRequest(this ControllerBase controller, string paramName, string error)
|
2022-08-04 15:04:55 +05:00
|
|
|
|
{
|
2023-09-28 16:25:29 +05:00
|
|
|
|
var errors = new Dictionary<string, string[]> {
|
|
|
|
|
{ paramName, new[]{ error } }
|
|
|
|
|
};
|
|
|
|
|
var problem = new ValidationProblemDetails(errors);
|
|
|
|
|
return controller.BadRequest(problem);
|
2022-08-04 15:04:55 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 12:57:32 +05:00
|
|
|
|
public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options)
|
2022-08-04 15:04:55 +05:00
|
|
|
|
{
|
2023-03-30 12:57:32 +05:00
|
|
|
|
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
|
|
|
|
return options;
|
2022-08-04 15:04:55 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:03:14 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-04 15:04:55 +05:00
|
|
|
|
|