Skip to content

Commit

Permalink
feat:user visit fix (#180)
Browse files Browse the repository at this point in the history
* feat:modify IUserService visit method

* feat: add pm get apps by app types api

* feat:update user visit

* fix:unit test error

* feat:fix test error

* feat:fix test error

Co-authored-by: Mayue <[email protected]>
Co-authored-by: yanpengju <[email protected]>
  • Loading branch information
3 people authored Aug 8, 2022
1 parent 8776eec commit d33a4d3
Show file tree
Hide file tree
Showing 30 changed files with 121 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@ package-lock.json
Thunbs.db
#忽略nuget库
packages/
.vscode/
.vscode/
coverage.opencover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface IIdentityUser

string? UserName { get; set; }

IEnumerable<IdentityRole<string>> Roles { get; set; }
string[] Roles { get; set; }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public class IdentityUser : IIdentityUser

public string? UserName { get; set; }

public IEnumerable<IdentityRole<string>> Roles { get; set; } = new List<IdentityRole<string>>();
public string[] Roles { get; set; } = Array.Empty<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface IUserContext

TIdentityUser? GetUser<TIdentityUser>() where TIdentityUser : IIdentityUser;

IEnumerable<IdentityRole<TRoleId>> GetUserRoles<TRoleId>();
IEnumerable<TRoleId> GetUserRoles<TRoleId>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ public IDisposable Change<TIdentityUser>(TIdentityUser identityUser) where TIden
return new DisposeAction(() => _currentUser.Value = user);
}

public IEnumerable<IdentityRole<TRoleId>> GetUserRoles<TRoleId>()
public IEnumerable<TRoleId> GetUserRoles<TRoleId>()
{
return GetUserSimple()?.Roles.Select(r => new IdentityRole<TRoleId>
{
Id = TypeConvertProvider.ConvertTo<TRoleId>(r.Id),
Name = r.Name
}) ?? new List<IdentityRole<TRoleId>>();
return GetUserSimple()?.Roles.Select(r => TypeConvertProvider.ConvertTo<TRoleId>(r)) ?? new List<TRoleId>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts;

public static class IsolationConsts
{
public const string ENVIRONMENT = "env";
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UserModel

public AddressValueModel Address { get; set; } = new();

public List<RoleModel> Roles { get; set; } = new();
public List<Guid> RoleIds { get; set; } = new();

public UserModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IUserService

Task<StaffDetailModel?> GetCurrentStaffAsync();

Task VisitedAsync(string url);
Task VisitedAsync(string appId, string url);

Task<List<UserVisitedModel>> GetVisitedListAsync();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Enum;

namespace Masa.BuildingBlocks.StackSdks.Pm.Model;

public class AppModel
Expand All @@ -17,15 +15,28 @@ public class AppModel

public AppTypes Type { get; set; }

public string Url { get; set; }

public ServiceTypes ServiceType { get; set; }

public string SwaggerUrl { get; set; }

public string Description { get; set; }

public AppModel()
{
}

public AppModel(int id, string name, string identity, int projectId)
public AppModel(int id, string name, string identity, int projectId, AppTypes type, string url, ServiceTypes serviceType, string swaggerUrl, string description)
{
Id = id;
Name = name;
Identity = identity;
ProjectId = projectId;
Type = type;
Url = url;
ServiceType = serviceType;
SwaggerUrl = swaggerUrl;
Description = description;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Model;

namespace Masa.BuildingBlocks.StackSdks.Pm.Service;

public interface IAppService
Expand All @@ -16,4 +14,6 @@ public interface IAppService
Task<AppDetailModel> GetAsync(int Id);

Task<AppDetailModel> GetByIdentityAsync(string identity);

Task<List<AppDetailModel>> GetListByAppTypes(params AppTypes[] appTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public DefaultUserContext(
return null;

var roleStr = ClaimsPrincipal?.FindClaimValue(_optionsMonitor.CurrentValue.Role);
var roles = new List<IdentityRole<string>>();
var roles = Array.Empty<string>();
if (!string.IsNullOrWhiteSpace(roleStr))
{
try
{
roles = JsonSerializer.Deserialize<List<IdentityRole<string>>>(roleStr) ?? roles;
roles = JsonSerializer.Deserialize<string[]>(roleStr) ?? roles;
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void TestAddSimpleIdentityReturnUserIdEqual1()
new(ClaimType.DEFAULT_USER_ID, "1"),
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
new(ClaimType.DEFAULT_TENANT_ID, "1"),
new(ClaimType.DEFAULT_USER_ROLE, "[{ \"Name\": \"admin\",\"Id\": \"1\" }]")
new(ClaimType.DEFAULT_USER_ROLE, "[\"roleId\"]")
})
})
};
Expand Down Expand Up @@ -176,7 +176,7 @@ public void TestAddMultiTenantIdentityReturnTenantIdIs1()
new(ClaimType.DEFAULT_USER_ID, "1"),
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
new(ClaimType.DEFAULT_TENANT_ID, "1"),
new(ClaimType.DEFAULT_USER_ROLE, "[{ \"Name\": \"admin\",\"Id\": \"1\" }]")
new(ClaimType.DEFAULT_USER_ROLE, "[\"1\"]")
})
})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ namespace Masa.Contrib.StackSdks.Auth;
internal class Constants
{
public const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.auth";

public const string ENVIRONMENT_KEY = "env_key";
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ namespace Masa.Contrib.StackSdks.Auth;

public class HttpEnvironmentDelegatingHandler : DelegatingHandler
{
readonly IHttpContextAccessor _httpContextAccessor;
readonly IEnvironmentProvider _environmentProvider;
readonly IHttpContextAccessor _httpContextAccessor;

public HttpEnvironmentDelegatingHandler(IHttpContextAccessor httpContextAccessor, IEnvironmentProvider environmentProvider)
public HttpEnvironmentDelegatingHandler(IEnvironmentProvider environmentProvider,
IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_environmentProvider = environmentProvider;
_httpContextAccessor = httpContextAccessor;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var envClaim = _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == "env");
if (envClaim != null)
var requestProvider = _httpContextAccessor.HttpContext?.RequestServices.GetService<IEnvironmentProvider>();
if (requestProvider != null)
{
request.Headers.Add(IsolationConsts.ENVIRONMENT, requestProvider.GetEnvironment());
}
else
{
_httpContextAccessor.HttpContext?.Items.TryAdd(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
request.Headers.Add(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
request.Headers.Add(IsolationConsts.ENVIRONMENT, _environmentProvider.GetEnvironment());
}
return await base.SendAsync(request, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ namespace Masa.Contrib.StackSdks.Auth.Service;
public class ProjectService : IProjectService
{
readonly ICaller _caller;
readonly IMultiEnvironmentUserContext _multiEnvironmentUserContext;
readonly IUserContext _userContext;

const string PARTY = "api/project/";

public ProjectService(ICaller caller, IMultiEnvironmentUserContext multiEnvironmentUserContext)
public ProjectService(ICaller caller, IUserContext userContext)
{
_caller = caller;
_multiEnvironmentUserContext = multiEnvironmentUserContext;
_userContext = userContext;
}

public async Task<List<ProjectModel>> GetGlobalNavigations()
{
var userId = _multiEnvironmentUserContext.GetUserId<Guid>();
var environment = _multiEnvironmentUserContext.Environment ?? "";
var requestUri = $"{PARTY}navigations?userId={userId}&environment={environment}";
var userId = _userContext.GetUserId<Guid>();
var requestUri = $"{PARTY}navigations?userId={userId}";
return await _caller.GetAsync<List<ProjectModel>>(requestUri) ?? new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public async Task<UserModel> GetCurrentUserAsync()
return await _caller.GetAsync<object, StaffDetailModel>(requestUri, new { userId });
}

public async Task VisitedAsync(string url)
public async Task VisitedAsync(string appId, string url)
{
var userId = _userContext.GetUserId<Guid>();
var requestUri = $"api/user/visit";
await _caller.PostAsync<object>(requestUri, new { UserId = userId, Url = url }, true);
await _caller.PostAsync<object>(requestUri, new { UserId = userId, appId = appId, Url = url }, true);
}

public async Task<List<UserVisitedModel>> GetVisitedListAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
public static IServiceCollection AddAuthClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
{
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions));

if (services.All(service => service.ServiceType != typeof(IMultiEnvironmentUserContext)))
throw new Exception("Please add IMultiEnvironmentUserContext first.");

services.AddHttpContextAccessor();
services.TryAddScoped<IEnvironmentProvider, EnvironmentProvider>();
services.AddScoped<HttpEnvironmentDelegatingHandler>();
services.AddScoped<IEnvironmentProvider, EnvironmentProvider>();
services.AddCaller(callerOptions);

services.AddScoped<IAuthClient>(serviceProvider =>
Expand Down
3 changes: 3 additions & 0 deletions src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
global using Masa.BuildingBlocks.Service.Caller;
global using Masa.BuildingBlocks.Service.Caller.Options;
global using Masa.BuildingBlocks.StackSdks.Auth;
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts;
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model;
global using Masa.BuildingBlocks.StackSdks.Auth.Service;
global using Masa.Contrib.Service.Caller;
global using Masa.Contrib.Service.Caller.HttpClient;
global using Masa.Contrib.StackSdks.Auth;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using System.Text.Json;
global using static Masa.Contrib.StackSdks.Auth.Constants;
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Model;
using Masa.BuildingBlocks.StackSdks.Pm.Service;

namespace Masa.Contrib.StackSdks.Pm.Service;

public class AppService : IAppService
Expand Down Expand Up @@ -54,4 +51,12 @@ public async Task<AppDetailModel> GetWithEnvironmentClusterAsync(int id)

return result ?? new();
}

public async Task<List<AppDetailModel>> GetListByAppTypes(params AppTypes[] appTypes)
{
var requestUri = $"open-api/app/by-types";
var result = await _caller.PostAsync<AppTypes[], List<AppDetailModel>>(requestUri, appTypes);

return result ?? new();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Model;
using Masa.BuildingBlocks.StackSdks.Pm.Service;

namespace Masa.Contrib.StackSdks.Pm.Service;

public class ClusterService : IClusterService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Model;
using Masa.BuildingBlocks.StackSdks.Pm.Service;

namespace Masa.Contrib.StackSdks.Pm.Service;

public class EnvironmentService : IEnvironmentService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.StackSdks.Pm.Model;
using Masa.BuildingBlocks.StackSdks.Pm.Service;

namespace Masa.Contrib.StackSdks.Pm.Service;

public class ProjectService : IProjectService
Expand All @@ -17,7 +14,7 @@ public ProjectService(ICaller caller)

public async Task<List<ProjectAppsModel>> GetProjectAppsAsync(string envName)
{
var requestUri = $"api/v1/projectwithapps/{envName}";
var requestUri = $"open-api/projectwithapps/{envName}";
var result = await _caller.GetAsync<List<ProjectAppsModel>>(requestUri);

return result ?? new();
Expand Down
3 changes: 3 additions & 0 deletions src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

global using Masa.BuildingBlocks.Service.Caller;
global using Masa.BuildingBlocks.Service.Caller.Options;
global using Masa.BuildingBlocks.StackSdks.Pm.Enum;
global using Masa.BuildingBlocks.StackSdks.Pm.Model;
global using Masa.BuildingBlocks.StackSdks.Pm.Service;
global using Masa.Contrib.Service.Caller;
global using Masa.Contrib.Service.Caller.HttpClient;
global using Masa.Contrib.StackSdks.Pm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task TestGetGlobalNavigationsAsync()
new ProjectModel()
};
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
var requestUri = $"api/project/navigations?userId={userId}&environment=development";
var requestUri = $"api/project/navigations?userId={userId}";
var caller = new Mock<ICaller>();
caller.Setup(provider => provider.GetAsync<List<ProjectModel>>(requestUri, default)).ReturnsAsync(data).Verifiable();
var userContext = new Mock<IMultiEnvironmentUserContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ public async Task TestGetCurrentStaffAsync()
}

[TestMethod]
[DataRow("https://www.baidu.com/")]
public async Task TestVisitedAsync(string url)
[DataRow("masa-auth-web-admin", "https://www.baidu.com/")]
public async Task TestVisitedAsync(string appId, string url)
{
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
var requestUri = $"api/user/visit";
Expand All @@ -243,7 +243,7 @@ public async Task TestVisitedAsync(string url)
var userContext = new Mock<IUserContext>();
userContext.Setup(user => user.GetUserId<Guid>()).Returns(userId).Verifiable();
var userService = new UserService(caller.Object, userContext.Object);
await userService.VisitedAsync(url);
await userService.VisitedAsync(appId, url);
caller.Verify(provider => provider.PostAsync<object>(requestUri, It.IsAny<object>(), true, default), Times.Once);
}

Expand Down
Loading

0 comments on commit d33a4d3

Please sign in to comment.