网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using Dpz.Core.Auth.Models;
using Dpz.Core.EnumLibrary;
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;
using MongoDB.Driver.Linq;

namespace Dpz.Core.Auth.Controllers;

[Authorize(nameof(Permissions.System))]
public class AccessRequestController(
    IRepository<ApplicationAccessRequest> accessRequestRepository,
    IRepository<AllowedClient> allowedClientRepository,
    IRepository<DpzApplication> applicationRepository,
    IAccountService accountService
) : Controller
{
    [HttpGet]
    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? clientId = null,
        DateTime? startDate = null,
        DateTime? endDate = null,
        int page = 1,
        int limit = 20
    )
    {
        var query = accessRequestRepository.SearchFor(x => true);

        if (!string.IsNullOrWhiteSpace(account))
        {
            query = query.Where(x => x.UserId == account);
        }

        if (!string.IsNullOrWhiteSpace(clientId))
        {
            query = query.Where(x => x.ClientId == clientId);
        }

        if (startDate.HasValue)
        {
            query = query.Where(x => x.RequestTime >= startDate.Value);
        }

        if (endDate.HasValue)
        {
            // End of the day
            var end = endDate.Value.Date.AddDays(1).AddTicks(-1);
            query = query.Where(x => x.RequestTime <= end);
        }

        var pagedList = await query
            .OrderByDescending(x => x.RequestTime)
            .ToPagedListAsync(page, limit);

        return Json(new LayuiPageWarp<ApplicationAccessRequest>(pagedList));
    }

    [HttpPost]
    public async Task<IActionResult> Handle(string id, AccessRequestStatus status, string remark)
    {
        var request = await accessRequestRepository.TryGetAsync(id);
        if (request == null)
        {
            return NotFound("Request not found");
        }

        var handler = await accountService.GetOneUserAsync(User.NameIdentifier);
        if (handler == null)
        {
            return BadRequest("Handler not found");
        }

        if (!ObjectId.TryParse(id, out var oid))
        {
            return BadRequest("Invalid request id");
        }

        // If restoring to pending
        if (status == AccessRequestStatus.Pending)
        {
            // Revoke access if it was approved
            if (request.Status == AccessRequestStatus.Approved)
            {
                await allowedClientRepository.DeleteAsync(x =>
                    x.Account == request.UserId && x.ApplicationId == request.ClientId
                );
            }

            request.Status = AccessRequestStatus.Pending;
            request.HandleTime = null;
            request.HandlerId = null;
            request.HandlerName = null;
            request.HandleRemark = null;

            await accessRequestRepository.UpdateAsync(
                x => x.Id == oid,
                Builders<ApplicationAccessRequest>
                    .Update.Set(x => x.Status, AccessRequestStatus.Pending)
                    .Set(x => x.HandleTime, null)
                    .Set(x => x.HandlerId, null)
                    .Set(x => x.HandlerName, null)
                    .Set(x => x.HandleRemark, null)
            );

            return RedirectToAction("Index");
        }
        
        if (
            request.Status == AccessRequestStatus.Approved
            && status != AccessRequestStatus.Approved
        )
        {
            await allowedClientRepository.DeleteAsync(x =>
                x.Account == request.UserId && x.ApplicationId == request.ClientId
            );
        }

        request.Status = status;
        request.HandleTime = DateTime.Now;
        request.HandlerId = handler.Id;
        request.HandlerName = handler.Name;
        request.HandleRemark = remark;

        await accessRequestRepository.UpdateAsync(
            x => x.Id == oid,
            Builders<ApplicationAccessRequest>
                .Update.Set(x => x.Status, status)
                .Set(x => x.HandleTime, request.HandleTime)
                .Set(x => x.HandlerId, request.HandlerId)
                .Set(x => x.HandlerName, request.HandlerName)
                .Set(x => x.HandleRemark, request.HandleRemark)
        );

        if (status != AccessRequestStatus.Approved)
        {
            return RedirectToAction("Index");
        }

        var exists = await allowedClientRepository
            .SearchFor(x => x.Account == request.UserId && x.ApplicationId == request.ClientId)
            .AnyAsync();
        if (!exists)
        {
            await allowedClientRepository.InsertAsync(
                new AllowedClient
                {
                    Account = request.UserId,
                    ApplicationId = request.ClientId,
                    ApplicationName = request.ClientDisplayName,
                    AllowedTime = DateTime.Now,
                }
            );
        }

        return RedirectToAction("Index");
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

AccessRequestController 功能解析

这个控制器是一个用于管理系统访问请求的ASP.NET Core控制器,主要处理用户对应用程序的访问权限申请和审批流程。以下是其主要功能:

1. 基本信息和权限

  • 控制器使用 [Authorize] 属性,要求用户具有系统权限(Permissions.System)才能访问
  • 通过构造函数注入了4个服务:
    • accessRequestRepository: 访问请求存储库
    • allowedClientRepository: 已授权客户端存储库
    • applicationRepository: 应用程序存储库
    • accountService: 账户服务

2. 主要功能方法

2.1 Index 方法

  • GET 请求
  • 获取所有应用程序列表
  • 返回一个视图,显示应用程序的基本信息(Id, ClientId, DisplayName)

2.2 Page 方法

  • GET 请求
  • 提供分页查询访问请求的功能
  • 支持以下查询参数:
    • account: 按用户ID过滤
    • clientId: 按客户端ID过滤
    • startDate/endDate: 按时间范围过滤
    • page/limit: 分页参数
  • 返回Layui表格兼容的分页数据格式

2.3 Handle 方法

  • POST 请求
  • 处理访问请求的审批/拒绝操作
  • 参数:
    • id: 请求ID
    • status: 要设置的状态(批准/拒绝/恢复待处理)
    • remark: 处理备注
  • 主要逻辑:
    1. 查找请求记录和当前处理人
    2. 如果状态改为"Pending":
      • 如果之前是"Approved",则撤销访问权限
      • 重置处理信息
    3. 如果从"Approved"改为其他状态,撤销访问权限
    4. 更新请求状态和处理信息
    5. 如果状态是"Approved",检查并添加用户到允许访问列表

3. 业务逻辑

  • 实现了完整的访问请求审批流程
  • 当请求被批准时,会自动将用户添加到允许访问列表
  • 当请求被拒绝或恢复为待处理时,会自动撤销已授予的权限
  • 所有操作都记录处理人和处理时间

4. 数据模型关系

  • ApplicationAccessRequest: 存储访问请求
  • AllowedClient: 存储已授权的用户-应用关系
  • DpzApplication: 存储应用程序信息

这个控制器主要用于管理系统管理员对用户访问应用程序权限的审批工作流。

loading