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;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public static IActionResult MakeBadRequest(this ControllerBase controller, string paramName, params string[] errors)
|
|
|
|
|
{
|
|
|
|
|
return controller.BadRequest(new
|
|
|
|
|
{
|
|
|
|
|
name = paramName,
|
|
|
|
|
errors,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|