You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Api/SiteController.cs

85 lines
3.2 KiB

using Application.Domain.Entities;
using Infrastructure.Application.Services.Settings;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
namespace IoTCenter.Api.Controllers
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class SiteController : ControllerBase
{
private readonly IConfiguration _cfg;
private readonly ILogger<OrganController> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IRepository<OrganSceneTimer> _organSceneTimerRepo;
private readonly ISettingService _settingService;
private readonly IRepository<User> _userRepo;
public SiteController(IConfiguration cfg,
IHttpClientFactory httpClientFactory,
IRepository<OrganSceneTimer> organSceneTimerRepo,
ILogger<OrganController> logger,
ISettingService settingService,
IRepository<User> userRepo)
{
this._cfg = cfg;
this._httpClientFactory = httpClientFactory;
this._organSceneTimerRepo = organSceneTimerRepo;
this._logger = logger;
this._settingService = settingService;
this._userRepo = userRepo;
}
[HttpPost]
public IActionResult GetSite()
{
try
{
var username = User.Identity.IsAuthenticated ? this.HttpContext.User.Identity.Name : null;
var roles = new List<string>();
var permissions = new List<string>();
if (User.Identity.IsAuthenticated)
{
roles = this._userRepo.ReadOnlyTable()
.Where(o => o.UserName == username)
.SelectMany(o => o.UserRoles)
.Select(o => o.Role.Name)
.ToList();
permissions = this._userRepo.ReadOnlyTable()
.Where(o => o.UserName == username)
.SelectMany(o => o.UserRoles)
.Select(o => o.Role)
.SelectMany(o => o.RolePermissions)
.Select(o => o.Permission)
.Select(o => o.Number)
.ToList();
}
return Ok(new
{
logo = this._settingService.GetSetting("logo").Value,
name = this._settingService.GetSetting("name").Value,
copyright = this._settingService.GetSetting("copyright").Value,
version = Helper.Instance.GetVersion(),
username,
roles,
permissions,
sso = this._settingService.GetSetting("sso")?.Value
});
}
catch (Exception ex)
{
this._logger.LogError(ex, ex.Message);
return Problem(ex.Message);
}
}
}
}