Greenfield: Expand user data (#6649)

This commit is contained in:
d11n
2025-04-04 09:15:33 +02:00
committed by GitHub
parent 65629f40b3
commit a916ae81be
11 changed files with 135 additions and 94 deletions

View File

@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -84,7 +85,7 @@ namespace BTCPayServer.Controllers.Greenfield
var user = await _userManager.FindByIdOrEmail(idOrEmail);
if (user != null)
{
return Ok(await FromModel(user));
return Ok(await ForAPI(user));
}
return this.UserNotFound();
}
@@ -128,7 +129,13 @@ namespace BTCPayServer.Controllers.Greenfield
[HttpGet("~/api/v1/users/")]
public async Task<ActionResult<ApplicationUserData[]>> GetUsers()
{
return Ok(await _userService.GetUsersWithRoles());
var usersWithRoles = await _userService.GetUsersWithRoles();
List<ApplicationUserData> users = [];
foreach (var user in usersWithRoles)
{
users.Add(await UserService.ForAPI<ApplicationUserData>(user.User, user.Roles, _callbackGenerator, _uriResolver, Request));
}
return Ok(users);
}
[Authorize(Policy = Policies.CanViewProfile, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
@@ -136,7 +143,7 @@ namespace BTCPayServer.Controllers.Greenfield
public async Task<ActionResult<ApplicationUserData>> GetCurrentUser()
{
var user = await _userManager.GetUserAsync(User);
return await FromModel(user!);
return await ForAPI(user!);
}
[Authorize(Policy = Policies.CanModifyProfile, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
@@ -232,7 +239,7 @@ namespace BTCPayServer.Controllers.Greenfield
if (!ModelState.IsValid)
return this.CreateValidationError(ModelState);
var model = await FromModel(user);
var model = await ForAPI(user);
return Ok(model);
}
@@ -264,7 +271,7 @@ namespace BTCPayServer.Controllers.Greenfield
user.SetBlob(blob);
await _userManager.UpdateAsync(user);
_eventAggregator.Publish(new UserEvent.Updated(user));
var model = await FromModel(user);
var model = await ForAPI(user);
return Ok(model);
}
catch (Exception e)
@@ -414,7 +421,7 @@ namespace BTCPayServer.Controllers.Greenfield
};
_eventAggregator.Publish(userEvent);
var model = await FromModel(user);
var model = await ForAPI(user);
return CreatedAtAction(string.Empty, model);
}
@@ -449,14 +456,11 @@ namespace BTCPayServer.Controllers.Greenfield
return Ok();
}
private async Task<ApplicationUserData> FromModel(ApplicationUser data)
private async Task<ApplicationUserData> ForAPI(ApplicationUser data)
{
var roles = (await _userManager.GetRolesAsync(data)).ToArray();
var model = UserService.FromModel(data, roles);
model.ImageUrl = string.IsNullOrEmpty(model.ImageUrl)
? null
: await _uriResolver.Resolve(Request.GetAbsoluteRootUri(), UnresolvedUri.Create(model.ImageUrl));
return model;
var blob = data.GetBlob();
return await UserService.ForAPI<ApplicationUserData>(data, roles, _callbackGenerator, _uriResolver, Request);
}
}
}