using AmtScanner.Api.Data; using AmtScanner.Api.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace AmtScanner.Api.Controllers; [ApiController] [Route("api/[controller]")] public class WindowsCredentialsController : ControllerBase { private readonly AppDbContext _context; private readonly ILogger _logger; public WindowsCredentialsController(AppDbContext context, ILogger logger) { _context = context; _logger = logger; } /// /// 获取所有 Windows 凭据 /// [HttpGet] public async Task>> GetAll() { var credentials = await _context.WindowsCredentials .OrderByDescending(c => c.IsDefault) .ThenBy(c => c.Name) .Select(c => new WindowsCredentialDto { Id = c.Id, Name = c.Name, Username = c.Username, Domain = c.Domain, IsDefault = c.IsDefault, Note = c.Note, CreatedAt = c.CreatedAt }) .ToListAsync(); return Ok(credentials); } /// /// 创建 Windows 凭据 /// [HttpPost] public async Task> Create([FromBody] CreateWindowsCredentialRequest request) { // 如果设为默认,取消其他默认 if (request.IsDefault) { await _context.WindowsCredentials .Where(c => c.IsDefault) .ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false)); } var credential = new WindowsCredential { Name = request.Name, Username = request.Username, Password = request.Password, // 实际生产环境应加密 Domain = request.Domain, IsDefault = request.IsDefault, Note = request.Note }; _context.WindowsCredentials.Add(credential); await _context.SaveChangesAsync(); _logger.LogInformation("Created Windows credential: {Name}", credential.Name); return Ok(new WindowsCredentialDto { Id = credential.Id, Name = credential.Name, Username = credential.Username, Domain = credential.Domain, IsDefault = credential.IsDefault, Note = credential.Note, CreatedAt = credential.CreatedAt }); } /// /// 更新 Windows 凭据 /// [HttpPut("{id}")] public async Task Update(long id, [FromBody] UpdateWindowsCredentialRequest request) { var credential = await _context.WindowsCredentials.FindAsync(id); if (credential == null) { return NotFound(new { error = "凭据不存在" }); } // 如果设为默认,取消其他默认 if (request.IsDefault && !credential.IsDefault) { await _context.WindowsCredentials .Where(c => c.IsDefault && c.Id != id) .ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false)); } credential.Name = request.Name; credential.Username = request.Username; if (!string.IsNullOrEmpty(request.Password)) { credential.Password = request.Password; } credential.Domain = request.Domain; credential.IsDefault = request.IsDefault; credential.Note = request.Note; credential.UpdatedAt = DateTime.UtcNow; await _context.SaveChangesAsync(); return Ok(new { success = true }); } /// /// 删除 Windows 凭据 /// [HttpDelete("{id}")] public async Task Delete(long id) { var credential = await _context.WindowsCredentials.FindAsync(id); if (credential == null) { return NotFound(new { error = "凭据不存在" }); } _context.WindowsCredentials.Remove(credential); await _context.SaveChangesAsync(); _logger.LogInformation("Deleted Windows credential: {Name}", credential.Name); return Ok(new { success = true }); } /// /// 设置默认凭据 /// [HttpPost("{id}/set-default")] public async Task SetDefault(long id) { var credential = await _context.WindowsCredentials.FindAsync(id); if (credential == null) { return NotFound(new { error = "凭据不存在" }); } // 取消其他默认 await _context.WindowsCredentials .Where(c => c.IsDefault) .ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false)); credential.IsDefault = true; await _context.SaveChangesAsync(); return Ok(new { success = true }); } } public class WindowsCredentialDto { public long Id { get; set; } public string Name { get; set; } = string.Empty; public string Username { get; set; } = string.Empty; public string? Domain { get; set; } public bool IsDefault { get; set; } public string? Note { get; set; } public DateTime CreatedAt { get; set; } } public class CreateWindowsCredentialRequest { public string Name { get; set; } = string.Empty; public string Username { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; public string? Domain { get; set; } public bool IsDefault { get; set; } public string? Note { get; set; } } public class UpdateWindowsCredentialRequest { public string Name { get; set; } = string.Empty; public string Username { get; set; } = string.Empty; public string? Password { get; set; } public string? Domain { get; set; } public bool IsDefault { get; set; } public string? Note { get; set; } }