|
|
|
@ -5,18 +5,18 @@ |
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Reflection; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using NSwag.Annotations; |
|
|
|
using Squidex.Areas.Api.Controllers.Users.Models; |
|
|
|
using Squidex.Domain.Users; |
|
|
|
using Squidex.Infrastructure.Commands; |
|
|
|
using Squidex.Infrastructure.Reflection; |
|
|
|
using Squidex.Infrastructure.Log; |
|
|
|
using Squidex.Pipeline; |
|
|
|
using Squidex.Shared.Users; |
|
|
|
|
|
|
|
@ -30,8 +30,9 @@ namespace Squidex.Areas.Api.Controllers.Users |
|
|
|
public sealed class UsersController : ApiController |
|
|
|
{ |
|
|
|
private static readonly byte[] AvatarBytes; |
|
|
|
private readonly UserManager<IUser> userManager; |
|
|
|
private readonly IUserPictureStore userPictureStore; |
|
|
|
private readonly IUserResolver userResolver; |
|
|
|
private readonly ISemanticLog log; |
|
|
|
|
|
|
|
static UsersController() |
|
|
|
{ |
|
|
|
@ -45,11 +46,17 @@ namespace Squidex.Areas.Api.Controllers.Users |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public UsersController(ICommandBus commandBus, UserManager<IUser> userManager, IUserPictureStore userPictureStore) |
|
|
|
public UsersController( |
|
|
|
ICommandBus commandBus, |
|
|
|
IUserPictureStore userPictureStore, |
|
|
|
IUserResolver userResolver, |
|
|
|
ISemanticLog log) |
|
|
|
: base(commandBus) |
|
|
|
{ |
|
|
|
this.userManager = userManager; |
|
|
|
this.userPictureStore = userPictureStore; |
|
|
|
this.userResolver = userResolver; |
|
|
|
|
|
|
|
this.log = log; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -68,11 +75,22 @@ namespace Squidex.Areas.Api.Controllers.Users |
|
|
|
[ProducesResponseType(typeof(PublicUserDto[]), 200)] |
|
|
|
public async Task<IActionResult> GetUsers(string query) |
|
|
|
{ |
|
|
|
var entities = await userManager.QueryByEmailAsync(query ?? string.Empty); |
|
|
|
try |
|
|
|
{ |
|
|
|
var entities = await userResolver.QueryByEmailAsync(query); |
|
|
|
|
|
|
|
var models = entities.Where(x => !x.IsHidden()).Select(UserDto.FromUser).ToArray(); |
|
|
|
|
|
|
|
var models = entities.Where(x => !x.IsHidden()).Select(x => SimpleMapper.Map(x, new UserDto { DisplayName = x.DisplayName() })).ToArray(); |
|
|
|
return Ok(models); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
log.LogError(ex, w => w |
|
|
|
.WriteProperty("action", nameof(GetUsers)) |
|
|
|
.WriteProperty("status", "Failed")); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(models); |
|
|
|
return Ok(new UserDto[0]); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -89,16 +107,25 @@ namespace Squidex.Areas.Api.Controllers.Users |
|
|
|
[ProducesResponseType(typeof(PublicUserDto), 200)] |
|
|
|
public async Task<IActionResult> GetUser(string id) |
|
|
|
{ |
|
|
|
var entity = await userManager.FindByIdAsync(id); |
|
|
|
try |
|
|
|
{ |
|
|
|
var entity = await userResolver.FindByIdOrEmailAsync(id); |
|
|
|
|
|
|
|
if (entity != null) |
|
|
|
{ |
|
|
|
var response = UserDto.FromUser(entity); |
|
|
|
|
|
|
|
if (entity == null) |
|
|
|
return Ok(response); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
return NotFound(); |
|
|
|
log.LogError(ex, w => w |
|
|
|
.WriteProperty("action", nameof(GetUser)) |
|
|
|
.WriteProperty("status", "Failed")); |
|
|
|
} |
|
|
|
|
|
|
|
var response = SimpleMapper.Map(entity, new UserDto { DisplayName = entity.DisplayName() }); |
|
|
|
|
|
|
|
return Ok(response); |
|
|
|
return NotFound(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -114,41 +141,41 @@ namespace Squidex.Areas.Api.Controllers.Users |
|
|
|
[ProducesResponseType(200)] |
|
|
|
public async Task<IActionResult> GetUserPicture(string id) |
|
|
|
{ |
|
|
|
var entity = await userManager.FindByIdAsync(id); |
|
|
|
|
|
|
|
if (entity == null) |
|
|
|
{ |
|
|
|
return NotFound(); |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
if (entity.IsPictureUrlStored()) |
|
|
|
{ |
|
|
|
return new FileStreamResult(await userPictureStore.DownloadAsync(entity.Id), "image/png"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
return new FileStreamResult(new MemoryStream(AvatarBytes), "image/png"); |
|
|
|
} |
|
|
|
var entity = await userResolver.FindByIdOrEmailAsync(id); |
|
|
|
|
|
|
|
using (var client = new HttpClient()) |
|
|
|
{ |
|
|
|
var url = entity.PictureNormalizedUrl(); |
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url)) |
|
|
|
if (entity != null) |
|
|
|
{ |
|
|
|
var response = await client.GetAsync(url); |
|
|
|
if (entity.IsPictureUrlStored()) |
|
|
|
{ |
|
|
|
return new FileStreamResult(await userPictureStore.DownloadAsync(entity.Id), "image/png"); |
|
|
|
} |
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
using (var client = new HttpClient()) |
|
|
|
{ |
|
|
|
var contentType = response.Content.Headers.ContentType.ToString(); |
|
|
|
var url = entity.PictureNormalizedUrl(); |
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url)) |
|
|
|
{ |
|
|
|
var response = await client.GetAsync(url); |
|
|
|
|
|
|
|
return new FileStreamResult(await response.Content.ReadAsStreamAsync(), contentType); |
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
var contentType = response.Content.Headers.ContentType.ToString(); |
|
|
|
|
|
|
|
return new FileStreamResult(await response.Content.ReadAsStreamAsync(), contentType); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
log.LogError(ex, w => w |
|
|
|
.WriteProperty("action", nameof(GetUser)) |
|
|
|
.WriteProperty("status", "Failed")); |
|
|
|
} |
|
|
|
|
|
|
|
return new FileStreamResult(new MemoryStream(AvatarBytes), "image/png"); |
|
|
|
} |
|
|
|
|