2021-12-01 17:10:17 +05:00
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-01-14 17:59:07 +05:00
|
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2021-12-01 17:10:17 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
{
|
2021-12-03 09:44:10 +05:00
|
|
|
[Route("api/admin/permission")]
|
2021-12-01 17:10:17 +05:00
|
|
|
[ApiController]
|
|
|
|
[Authorize]
|
2021-12-20 15:17:09 +05:00
|
|
|
public class AdminPermissionController : CrudController<PermissionDto, ICrudService<PermissionDto>>
|
2022-04-11 18:00:34 +05:00
|
|
|
{
|
2022-01-12 13:33:16 +05:00
|
|
|
public AdminPermissionController(ICrudService<PermissionDto> service)
|
2022-04-11 18:00:34 +05:00
|
|
|
: base(service)
|
|
|
|
{ }
|
2022-01-14 17:59:07 +05:00
|
|
|
|
|
|
|
[HttpGet("Autogenerated")]
|
2022-01-19 11:42:26 +05:00
|
|
|
[Permission]
|
2022-01-14 17:59:07 +05:00
|
|
|
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)
|
|
|
|
{
|
2022-01-18 17:21:05 +05:00
|
|
|
var httpMethod = method.GetCustomAttribute<HttpMethodAttribute>()?.HttpMethods?.First().ToLower();
|
|
|
|
switch (httpMethod)
|
|
|
|
{
|
|
|
|
case "get":
|
|
|
|
case "delete":
|
|
|
|
permissions.Add($"{controllerName}.{httpMethod.ToLower()}");
|
|
|
|
continue;
|
|
|
|
case "post":
|
|
|
|
case "put":
|
|
|
|
case "patch":
|
|
|
|
permissions.Add($"{controllerName}.edit");
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
2022-01-14 17:59:07 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(permissions);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
2022-01-14 17:59:07 +05:00
|
|
|
}
|
2021-12-01 17:10:17 +05:00
|
|
|
}
|
|
|
|
}
|