forked from ddrilling/AsbCloudServer
51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using AsbCloudApp.Data.User;
|
|
using AsbCloudWebApi.Converters;
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Security.Claims;
|
|
|
|
namespace Microsoft.AspNetCore.Mvc
|
|
{
|
|
public static class Extentions
|
|
{
|
|
public static int? GetCompanyId(this ClaimsPrincipal user)
|
|
{
|
|
var claimIdCompany = user.FindFirst(nameof(UserDto.IdCompany));
|
|
if (claimIdCompany is null)
|
|
return null;
|
|
|
|
return int.TryParse(claimIdCompany.Value, out int uid)
|
|
? uid
|
|
: null;
|
|
}
|
|
|
|
public static int? GetUserId(this ClaimsPrincipal user)
|
|
{
|
|
var userId = user.FindFirst(nameof(UserDto.Id));
|
|
if (userId is null)
|
|
return null;
|
|
|
|
return int.TryParse(userId.Value, out int uid)
|
|
? uid
|
|
: null;
|
|
}
|
|
|
|
public static IActionResult MakeBadRequest(this ControllerBase controller, string paramName, params string[] errors)
|
|
{
|
|
return controller.BadRequest(new
|
|
{
|
|
name = paramName,
|
|
errors,
|
|
});
|
|
}
|
|
|
|
public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options)
|
|
{
|
|
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
|
return options;
|
|
}
|
|
|
|
}
|
|
}
|
|
|