网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
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("撤销成功"));
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

GrantController 功能解析

这个控制器是一个授权管理模块,用于管理系统用户对应用程序的访问权限。以下是详细功能说明:

主要功能

  1. 权限控制

    • 使用 [Authorize(nameof(Permissions.System))] 限制只有具有系统权限的用户才能访问
  2. 依赖注入服务

    • allowedClientRepository: 管理允许的客户端数据访问
    • applicationRepository: 管理应用程序数据访问
    • applicationManager: OpenIddict 的应用程序管理
    • accountService: 用户账户服务

具体方法

1. Index 方法

  • 功能:显示所有应用程序列表
  • 返回:包含应用程序ID、客户端ID和显示名称的视图
  • 数据:从 applicationRepository 获取所有应用程序

2. Page 方法

  • 功能:分页查询已授权记录
  • 参数
    • account: 过滤特定账户
    • appId: 过滤特定应用
    • page/limit: 分页参数
  • 返回:Layui表格兼容的分页JSON数据

3. Grant 方法

  • 功能:授予用户访问特定应用的权限
  • 验证
    • 检查参数是否为空
    • 检查用户是否存在
    • 检查应用是否存在
    • 检查是否已授权
  • 操作:在验证通过后创建新的授权记录

4. Revoke 方法

  • 功能:撤销用户的应用程序访问权限
  • 验证:检查ID是否有效
  • 操作:删除对应的授权记录

数据模型

  1. AllowedClient:存储用户与应用之间的授权关系

    • 包含账户名、应用ID、应用名称和授权时间
  2. DpzApplication:表示系统中的应用程序

    • 包含客户端ID和显示名称等
  3. AuthApplicationModel:用于视图显示的简化应用模型

技术特点

  • 使用MongoDB作为数据存储
  • 集成OpenIddict进行OAuth2/OpenID Connect管理
  • 采用Layui前端框架兼容的数据格式
  • 实现标准的分页查询功能
  • 包含完整的授权/撤销业务流程

这个控制器为系统管理员提供了一个完整的界面来管理哪些用户可以访问哪些应用程序。

loading