FalconSSO/FAuth/Controllers/api/AppController.cs

106 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using FAuth.Extensions.Account;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
/// <summary>
/// 应用程序控制器
/// </summary>
public class AppController:ApiControllerBase<AppController>
{
public AccountHelper Account { get; set; }
public AppController(ILogger<AppController> logger,IServiceProvider service) : base(logger,service) {
this.Account = this.Services.GetService<AccountHelper>();
}
/// <summary>
/// 添加新应用
/// </summary>
/// <param name="appName">应用名称</param>
/// <param name="description">应用说明</param>
/// <returns>应用信息</returns>
[HttpPost]
[ProducesResponseType(typeof(Apps),200)]
public Apps AddNewApp(string appName,string description) {
if(appName.IsNullOrEmpty()) {
throw new ApiArgumentNullException(nameof(appName));
}
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
throw new ApiException($"应用{appName}已经存在,不可以重复创建!");
}
var newApp = new Apps {
Name = appName,
Description = description,
};
this.Db.Entry(newApp).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
return newApp;
}
2020-05-06 16:02:54 +08:00
/// <summary>
/// 删除一个应用
/// </summary>
/// <param name="id">应用编号</param>
2020-05-06 16:02:54 +08:00
/// <returns>删除结果</returns>
[HttpPost]
public bool RemoveApp(int id) {
if(id == 1) {
throw new ApiException("系统应用不可以删除");
}
var qu = this.Db.Apps.Where(m => m.Id == id);
foreach(var item in qu) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
return true;
}
/// <summary>
/// 查询app是否注册
/// </summary>
/// <param name="appName">app名称</param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(Apps),200)]
public Apps GetApps(string appName) {
if(string.IsNullOrEmpty(appName))
throw new ApiArgumentNullException(nameof(appName));
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
return qu.First();
} else {
throw new ApiException("没有找到该应用");
}
}
/// <summary>
/// 通过管理员凭据获取应用列表
/// </summary>
/// <param name="adminTicket">管理员凭据</param>
/// <returns>应用列表</returns>
[HttpGet]
[ProducesResponseType(typeof(IEquatable<Apps>),200)]
public IEnumerable<Apps> GetAppList(string adminTicket) {
if(this.Account.IsNotSystemAdmin(adminTicket)) {
throw new ApiException("需要提供管理员票据");
}
var list = this.Cache.GetObj<List<Apps>>("");
var qu = this.Db.Apps;
return qu.ToList();
}
}
}