DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/AdminPermissionController.cs

61 lines
1.9 KiB
C#
Raw Normal View History

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;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// Редактор разрешений пользователей для админки
/// </summary>
[Route("api/admin/permission")]
[ApiController]
[Authorize]
public class AdminPermissionController : CrudController<PermissionDto, ICrudRepository<PermissionDto>>
{
2024-08-19 10:01:07 +05:00
public AdminPermissionController(ICrudRepository<PermissionDto> service)
: base(service)
{ }
[HttpGet("Autogenerated")]
[Permission]
public IActionResult GetAutogenerated()
2022-04-11 18:00:34 +05:00
{
2024-08-19 10:01:07 +05:00
var controllers = Assembly.GetExecutingAssembly().GetTypes().
Where(type => typeof(ControllerBase).IsAssignableFrom(type) && !type.IsAbstract)
.ToList();
2024-08-19 10:01:07 +05:00
var permissions = new SortedSet<string>();
foreach (var controller in controllers)
{
2024-08-19 10:01:07 +05:00
var methods = controller.GetMethods().Where(m => m.IsPublic);
var controllerName = controller.Name.Replace("Controller", "");
foreach (var method in methods)
{
2024-08-19 10:01:07 +05:00
var httpMethod = method.GetCustomAttribute<HttpMethodAttribute>()?.HttpMethods?.First().ToLower();
switch (httpMethod)
{
2024-08-19 10:01:07 +05:00
case "get":
case "delete":
permissions.Add($"{controllerName}.{httpMethod.ToLower()}");
continue;
case "post":
case "put":
case "patch":
permissions.Add($"{controllerName}.edit");
continue;
default:
continue;
}
2024-08-19 10:01:07 +05:00
}
2024-08-19 10:01:07 +05:00
}
2024-08-19 10:01:07 +05:00
return Ok(permissions);
2022-04-11 18:00:34 +05:00
}
}