AdminPermissionController. Add method to generate names of auto-permissions.

This commit is contained in:
Фролов 2022-01-14 17:59:07 +05:00
parent f81dcf2a46
commit e0bfc8d420

View File

@ -2,6 +2,10 @@ using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace AsbCloudWebApi.Controllers
{
@ -13,5 +17,30 @@ namespace AsbCloudWebApi.Controllers
public AdminPermissionController(ICrudService<PermissionDto> service)
:base(service)
{}
[HttpGet("Autogenerated")]
public IActionResult GetAutogenerated()
{
var controllers = Assembly.GetExecutingAssembly().GetTypes().
Where(type => typeof(ControllerBase).IsAssignableFrom(type) && !type.IsAbstract)
.ToList();
var permissions = new SortedSet<string>();
foreach (var controller in controllers)
{
var methods = controller.GetMethods().Where(m => m.IsPublic);
var controllerName = controller.Name.Replace("Controller", "");
foreach (var method in methods)
{
var httpMethod = method.GetCustomAttribute<HttpMethodAttribute>()?.HttpMethods?.First();
if (string.IsNullOrEmpty(httpMethod))
continue;
permissions.Add($"{controllerName}.{httpMethod.ToLower()}");
}
}
return Ok(permissions);
}
}
}