|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using Confluent.Kafka;
- using Confluent.Kafka.Admin;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.OpenApi.Extensions;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using TelpoKafkaConsole.Model;
- using TelpoKafkaConsole.Service;
- using TelpoKafkaConsole.WebApi.Controllers.Api;
- using TelpoKafkaConsole.WebApi.Model.Request;
-
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
-
- namespace TelpoKafkaConsole.WebApi.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ScramCredentialsUserController : ControllerBase
- {
- private readonly KafkaAdminService _servicekafkaAdmin;
- public ScramCredentialsUserController(KafkaAdminService kafkaAdminService)
- {
- _servicekafkaAdmin = kafkaAdminService;
- }
- // GET: api/<ScramCredentialsUserController>
- [HttpGet]
- public async Task<ApiResponse<object>> Get()
- {
- var usersScram = await _servicekafkaAdmin.DescribeUserScramCredentialsAsync(new List<string>());
- var users = usersScram
- .Select(i => new {i.User, ScramCredentialInfos=i.ScramCredentialInfos
- .Select(s=>new { Mechanism=s.Mechanism.GetDisplayName(),s.Iterations }) })
- .Where(i=>!i.User.Equals("superuser"));
- return ApiResponse<object>.Success(users);
- }
-
- // GET api/<ScramCredentialsUserController>/5
- [HttpGet("{username}")]
- public async Task<ApiResponse<object>> GetAsync(string username)
- {
- var users = new List<string>
- {
- username
- };
- var usersScram = await _servicekafkaAdmin.DescribeUserScramCredentialsAsync(users);
-
-
-
- var firstUserScram = usersScram.FirstOrDefault();
-
- var user = new
- {
- firstUserScram?.User,
- ScramCredentialInfos = firstUserScram?.ScramCredentialInfos
- .Select(i => new
- {
- Mechanism = i.Mechanism.GetDisplayName(),
- i.Iterations
- })
- };
- return ApiResponse<object>.Success(user);
- }
-
- // POST api/<ScramCredentialsUserController>
- [HttpPost]
- public async Task<ApiResponse<string>> PostAsync([FromBody] UserReq user)
- {
- ScramCredentialsUser scramUser = new()
- {
- Name = user.Name,
- Password = user.Password,
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser);
-
- return ApiResponse<string>.Success($"创建 Scram 用户{user.Name}成功");
- }
-
- [HttpDelete("{username}")]
- public async Task<ApiResponse<string>> Delete(string username)
- {
- ScramCredentialsUser scramUser = new()
- {
- Name = username
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser,"DELETE");
- return ApiResponse<string>.Success($"删除 Scram 用户{username} 成功");
- }
- }
- }
|