网站首页 网站源码
using Dpz.Core.Auth.Models;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Infrastructure;
using Dpz.Core.MongodbAccess;
using Dpz.Core.Public.Entity;
using Dpz.Core.Public.Entity.Auth;
using Dpz.Core.Service;
using Dpz.Core.Service.RepositoryService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver.Linq;
using OpenIddict.Abstractions;
namespace Dpz.Core.Auth.Controllers;
[Authorize(nameof(Permissions.System))]
public class GrantController(
IRepository<AllowedClient> allowedClientRepository,
IRepository<DpzApplication> applicationRepository,
IOpenIddictApplicationManager applicationManager,
IAccountService accountService
) : Controller
{
public async Task<IActionResult> Index()
{
var applications = await applicationRepository
.SearchFor(x => true)
.Select(x => new AuthApplicationModel(x.Id.ToString(), x.ClientId, x.DisplayName))
.ToListAsync();
return View(applications);
}
[HttpGet]
public async Task<IActionResult> Page(
string? account = null,
string? appId = null,
int page = 1,
int limit = 20
)
{
var query = allowedClientRepository.SearchFor(x => true);
if (!string.IsNullOrWhiteSpace(account))
{
query = query.Where(x => x.Account == account);
}
if (!string.IsNullOrWhiteSpace(appId))
{
query = query.Where(x => x.ApplicationId == appId);
}
var pagedList = await query
.OrderByDescending(x => x.AllowedTime)
.ToPagedListAsync(page, limit);
return Json(new LayuiPageWarp<AllowedClient>(pagedList));
}
[HttpPost]
public async Task<IActionResult> Grant(string account, string appId)
{
if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(appId))
{
return Json(ResponseResult.ToFail("参数不能为空"));
}
var user = await accountService.GetOneUserAsync(account);
if (user == null)
{
return Json(ResponseResult.ToFail("用户不存在"));
}
var app = await applicationManager.FindByClientIdAsync(appId);
if (app == null)
{
return Json(ResponseResult.ToFail("应用不存在"));
}
// 假设 GetDisplayNameAsync 是扩展方法或需要转换类型
// OpenIddictApplicationManager 没有直接的 DisplayName 属性,需要通过 descriptor 或者 properties 获取
// 这里简化处理,直接转换
var appDescriptor = (DpzApplication)app;
var exists = await allowedClientRepository
.SearchFor(x => x.Account == account && x.ApplicationId == appId)
.AnyAsync();
if (exists)
{
return Json(ResponseResult.ToFail("该用户已拥有此应用权限"));
}
await allowedClientRepository.InsertAsync(
new AllowedClient
{
Account = account,
ApplicationId = appId,
ApplicationName = appDescriptor.DisplayName ?? appId,
AllowedTime = DateTime.Now,
}
);
return Json(ResponseResult.ToSuccess("授权成功"));
}
[HttpPost]
public async Task<IActionResult> Revoke(string id)
{
if (!ObjectId.TryParse(id, out var oid))
{
return Json(ResponseResult.ToFail("无效的ID"));
}
await allowedClientRepository.DeleteAsync(x => x.Id == oid);
return Json(ResponseResult.ToSuccess("撤销成功"));
}
}
这个控制器是一个授权管理模块,用于管理系统用户对应用程序的访问权限。以下是详细功能说明:
权限控制:
[Authorize(nameof(Permissions.System))] 限制只有具有系统权限的用户才能访问依赖注入服务:
allowedClientRepository: 管理允许的客户端数据访问applicationRepository: 管理应用程序数据访问applicationManager: OpenIddict 的应用程序管理accountService: 用户账户服务applicationRepository 获取所有应用程序account: 过滤特定账户appId: 过滤特定应用page/limit: 分页参数AllowedClient:存储用户与应用之间的授权关系
DpzApplication:表示系统中的应用程序
AuthApplicationModel:用于视图显示的简化应用模型
这个控制器为系统管理员提供了一个完整的界面来管理哪些用户可以访问哪些应用程序。
