123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using Azure.Cosmos;
- using Azure.Storage.Blobs;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Table;
- namespace TEAMModelBI.Controllers.BITable
- {
- [Route("iesoauth")]
- [ApiController]
- public class IES5OAuthController : ControllerBase
- {
- public readonly AzureStorageFactory _azureStorage;
- private readonly AzureCosmosFactory _azureCosmos;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- public IES5OAuthController(AzureStorageFactory azureStorage, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureStorage = azureStorage;
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 获取信息授权信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-oauthshows")]
- public async Task<IActionResult> GetOAuthShows(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("type", out JsonElement type);
- jsonElement.TryGetProperty("scope", out JsonElement scope);
- jsonElement.TryGetProperty("code", out JsonElement code);
- jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
- List<OAuthShow> oAouthShows = new();
- var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOAuth");
- StringBuilder tableSql = new(" PartitionKey eq 'OAuthShow'");
- if (!string.IsNullOrEmpty($"{type}"))
- tableSql.Append($" and Type eq '{type}'");
- if (!string.IsNullOrEmpty($"{scope}"))
- tableSql.Append($" and Scope eq '{scope}'");
- if (!string.IsNullOrEmpty($"{scope}"))
- tableSql.Append($" and Code eq '{code}'");
- if (!string.IsNullOrEmpty($"{scope}"))
- tableSql.Append($" and RowKey eq '{rowKey}'");
- //lambda 表达式排序
- oAouthShows = await table.QueryWhereString<OAuthShow>(tableSql.ToString());
- return Ok(new { state = RespondCode.Ok, oAouthShows });
- }
- /// <summary>
- /// 添加和修改权限
- /// </summary>
- /// <param name="oAuthShow"></param>
- /// <returns></returns>
- [AuthToken(Roles = "admin,rdc,sales")]
- [HttpPost("set-oauthshow")]
- public async Task<IActionResult> UpOAuthShow(OAuthShow oAuthShow)
- {
- var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOAuth");
- var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- var cosmosClinet = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- StringBuilder msg = new($"{_tmdName}[{_tmdId}]");
- List<OAuthShow> oAuthShows = new();
- List<OAuthShow> queryOs = new();
- ArtSetting artSetting = new();
- if (!string.IsNullOrEmpty(oAuthShow.RowKey))
- {
- string osSql = $" PartitionKey eq 'OAuthShow' and RowKey eq '{oAuthShow.RowKey}'";
- queryOs = await table.QueryWhereString<OAuthShow>(osSql);
- msg.Append($"修改RowKy为{oAuthShow.RowKey}授权信息");
- if (queryOs.Count > 0)
- {
- foreach (var item in queryOs)
- {
- item.Type = oAuthShow.Type;
- item.Scope = oAuthShow.Scope;
- item.Code = oAuthShow.Code;
- item.Domain = oAuthShow.Domain;
- OAuthShow upOs = await table.SaveOrUpdate<OAuthShow>(item);
- oAuthShows.Add(upOs);
- }
- }
- else
- return Ok(new { state = RespondCode.NotFound, msg = "未找到需要修改的授权。" });
- }
- else
- {
- string osSql = $" PartitionKey eq 'OAuthShow' and RowKey eq '{oAuthShow.Type}-{oAuthShow.Domain}-{oAuthShow.Code}'";
- queryOs = await table.QueryWhereString<OAuthShow>(osSql);
- if (queryOs.Count == 0)
- {
- oAuthShow.PartitionKey = "OAuthShow";
- oAuthShow.Status = 1;
- oAuthShow.RowKey = $"{oAuthShow.Type}-{oAuthShow.Domain}-{oAuthShow.Code}";
- msg.Append($"添加RowKy为{oAuthShow.RowKey}授权信息");
- oAuthShow = await table.SaveOrUpdate<OAuthShow>(oAuthShow);
- oAuthShows.Add(oAuthShow);
- if (oAuthShow.Type.Equals("art"))
- {
- var isExist = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{oAuthShow.Code}", new PartitionKey("ArtSetting"));
- if (isExist.Status != 200)
- {
- artSetting = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<ArtSetting>("default", new PartitionKey("ArtSetting"));
- artSetting.id = oAuthShow.Code;
- artSetting = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<ArtSetting>(artSetting, new PartitionKey("ArtSetting"));
- }
- else
- {
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "schoolTeacher-add", msg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Created, msg = "已授权,区级艺术设置文件已存在!" });
- }
- }
- }
- else
- return Ok(new { state = RespondCode.Conflict, msg = "已存在该授权。" });
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "schoolTeacher-add", msg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Ok, oAuthShows });
- }
- }
- }
|