PermissionsMiddlware Add ulimate admin? check by idUser == 1

This commit is contained in:
Фролов 2022-01-21 17:33:28 +05:00
parent 8633616481
commit c6bfeb4e04

View File

@ -34,39 +34,47 @@ namespace AsbCloudWebApi.Middlewares
return;
}
var permissionName = permission.Name;
if (string.IsNullOrEmpty(permissionName))
bool isAuthorized;
if (idUser == 1)
isAuthorized = true;
else
{
var controller = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor>()
?.ControllerName;
var httpMethod = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Routing.HttpMethodMetadata>()
.HttpMethods[0]
.ToLower();
permissionName = httpMethod switch
var permissionName = permission.Name;
if (string.IsNullOrEmpty(permissionName))
{
"get" or "delete" => $"{controller}.{httpMethod}",
"post" or "put" or "patch" => $"{controller}.edit",
_ => throw new NotImplementedException(),
};
PermissionAttribute.Registered.Add(permissionName);
}
else if(permissionName.Contains("[controller]"))
{
var controller = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor>()
?.ControllerName;
permissionName = permissionName.Replace("[controller]", controller);
PermissionAttribute.Registered.Add(permissionName);
var controller = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor>()
?.ControllerName;
var httpMethod = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Routing.HttpMethodMetadata>()
.HttpMethods[0]
.ToLower();
permissionName = httpMethod switch
{
"get" or "delete" => $"{controller}.{httpMethod}",
"post" or "put" or "patch" => $"{controller}.edit",
_ => throw new NotImplementedException(),
};
PermissionAttribute.Registered.Add(permissionName);
}
else if (permissionName.Contains("[controller]"))
{
var controller = endpoint.Metadata
.GetMetadata<Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor>()
?.ControllerName;
permissionName = permissionName.Replace("[controller]", controller);
PermissionAttribute.Registered.Add(permissionName);
}
var userService = context.RequestServices.GetRequiredService<IUserService>();
isAuthorized = userService.HasPermission((int)idUser, permissionName);
}
var userService = context.RequestServices.GetRequiredService<IUserService>();
var isAuthorized = userService.HasPermission((int)idUser, permissionName);
if(isAuthorized)
#warning Проверка прав отключена.
isAuthorized = true;
if (isAuthorized)
await next?.Invoke(context);
else
await context.ForbidAsync();