Skip to content

Commit d33a4d3

Browse files
MayueCifMayueyanpengju
authored
feat:user visit fix (#180)
* 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 <mayue@lonsid.cn> Co-authored-by: yanpengju <yanpengju@masastack.com>
1 parent 8776eec commit d33a4d3

File tree

30 files changed

+121
-73
lines changed

30 files changed

+121
-73
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,5 @@ package-lock.json
218218
Thunbs.db
219219
#忽略nuget库
220220
packages/
221-
.vscode/
221+
.vscode/
222+
coverage.opencover.xml

src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/Entities/IIdentityUser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public interface IIdentityUser
99

1010
string? UserName { get; set; }
1111

12-
IEnumerable<IdentityRole<string>> Roles { get; set; }
12+
string[] Roles { get; set; }
1313
}

src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/Entities/IdentityRole.cs

-11
This file was deleted.

src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/Entities/IdentityUser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public class IdentityUser : IIdentityUser
99

1010
public string? UserName { get; set; }
1111

12-
public IEnumerable<IdentityRole<string>> Roles { get; set; } = new List<IdentityRole<string>>();
12+
public string[] Roles { get; set; } = Array.Empty<string>();
1313
}

src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/IUserContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public interface IUserContext
1515

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

18-
IEnumerable<IdentityRole<TRoleId>> GetUserRoles<TRoleId>();
18+
IEnumerable<TRoleId> GetUserRoles<TRoleId>();
1919
}

src/BuildingBlocks/Authentication/Masa.BuildingBlocks.Authentication.Identity/UserContext.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ public IDisposable Change<TIdentityUser>(TIdentityUser identityUser) where TIden
4545
return new DisposeAction(() => _currentUser.Value = user);
4646
}
4747

48-
public IEnumerable<IdentityRole<TRoleId>> GetUserRoles<TRoleId>()
48+
public IEnumerable<TRoleId> GetUserRoles<TRoleId>()
4949
{
50-
return GetUserSimple()?.Roles.Select(r => new IdentityRole<TRoleId>
51-
{
52-
Id = TypeConvertProvider.ConvertTo<TRoleId>(r.Id),
53-
Name = r.Name
54-
}) ?? new List<IdentityRole<TRoleId>>();
50+
return GetUserSimple()?.Roles.Select(r => TypeConvertProvider.ConvertTo<TRoleId>(r)) ?? new List<TRoleId>();
5551
}
5652
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts;
5+
6+
public static class IsolationConsts
7+
{
8+
public const string ENVIRONMENT = "env";
9+
}

src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth.Contracts/Model/UserModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class UserModel
3131

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

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

3636
public UserModel()
3737
{

src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/IUserService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface IUserService
3333

3434
Task<StaffDetailModel?> GetCurrentStaffAsync();
3535

36-
Task VisitedAsync(string url);
36+
Task VisitedAsync(string appId, string url);
3737

3838
Task<List<UserVisitedModel>> GetVisitedListAsync();
3939

src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Pm/Model/AppModel.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Masa.BuildingBlocks.StackSdks.Pm.Enum;
5-
64
namespace Masa.BuildingBlocks.StackSdks.Pm.Model;
75

86
public class AppModel
@@ -17,15 +15,28 @@ public class AppModel
1715

1816
public AppTypes Type { get; set; }
1917

18+
public string Url { get; set; }
19+
20+
public ServiceTypes ServiceType { get; set; }
21+
22+
public string SwaggerUrl { get; set; }
23+
24+
public string Description { get; set; }
25+
2026
public AppModel()
2127
{
2228
}
2329

24-
public AppModel(int id, string name, string identity, int projectId)
30+
public AppModel(int id, string name, string identity, int projectId, AppTypes type, string url, ServiceTypes serviceType, string swaggerUrl, string description)
2531
{
2632
Id = id;
2733
Name = name;
2834
Identity = identity;
2935
ProjectId = projectId;
36+
Type = type;
37+
Url = url;
38+
ServiceType = serviceType;
39+
SwaggerUrl = swaggerUrl;
40+
Description = description;
3041
}
3142
}

src/BuildingBlocks/StackSdks/Masa.BuildingBlocks.StackSdks.Pm/Service/IAppService.cs

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

4-
using Masa.BuildingBlocks.StackSdks.Pm.Model;
5-
64
namespace Masa.BuildingBlocks.StackSdks.Pm.Service;
75

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

1816
Task<AppDetailModel> GetByIdentityAsync(string identity);
17+
18+
Task<List<AppDetailModel>> GetListByAppTypes(params AppTypes[] appTypes);
1919
}

src/Contrib/Authentication/Masa.Contrib.Authentication.Identity/DefaultUserContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public DefaultUserContext(
3434
return null;
3535

3636
var roleStr = ClaimsPrincipal?.FindClaimValue(_optionsMonitor.CurrentValue.Role);
37-
var roles = new List<IdentityRole<string>>();
37+
var roles = Array.Empty<string>();
3838
if (!string.IsNullOrWhiteSpace(roleStr))
3939
{
4040
try
4141
{
42-
roles = JsonSerializer.Deserialize<List<IdentityRole<string>>>(roleStr) ?? roles;
42+
roles = JsonSerializer.Deserialize<string[]>(roleStr) ?? roles;
4343
}
4444
catch (Exception e)
4545
{

src/Contrib/Authentication/Tests/Masa.Contrib.Authentication.Identity.Tests/TestIdentity.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void TestAddSimpleIdentityReturnUserIdEqual1()
139139
new(ClaimType.DEFAULT_USER_ID, "1"),
140140
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
141141
new(ClaimType.DEFAULT_TENANT_ID, "1"),
142-
new(ClaimType.DEFAULT_USER_ROLE, "[{ \"Name\": \"admin\",\"Id\": \"1\" }]")
142+
new(ClaimType.DEFAULT_USER_ROLE, "[\"roleId\"]")
143143
})
144144
})
145145
};
@@ -176,7 +176,7 @@ public void TestAddMultiTenantIdentityReturnTenantIdIs1()
176176
new(ClaimType.DEFAULT_USER_ID, "1"),
177177
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
178178
new(ClaimType.DEFAULT_TENANT_ID, "1"),
179-
new(ClaimType.DEFAULT_USER_ROLE, "[{ \"Name\": \"admin\",\"Id\": \"1\" }]")
179+
new(ClaimType.DEFAULT_USER_ROLE, "[\"1\"]")
180180
})
181181
})
182182
};

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Constants.cs

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ namespace Masa.Contrib.StackSdks.Auth;
66
internal class Constants
77
{
88
public const string DEFAULT_CLIENT_NAME = "masa.contrib.basicability.auth";
9-
10-
public const string ENVIRONMENT_KEY = "env_key";
119
}
1210

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/HttpEnvironmentDelegatingHandler.cs

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ namespace Masa.Contrib.StackSdks.Auth;
55

66
public class HttpEnvironmentDelegatingHandler : DelegatingHandler
77
{
8-
readonly IHttpContextAccessor _httpContextAccessor;
98
readonly IEnvironmentProvider _environmentProvider;
9+
readonly IHttpContextAccessor _httpContextAccessor;
1010

11-
public HttpEnvironmentDelegatingHandler(IHttpContextAccessor httpContextAccessor, IEnvironmentProvider environmentProvider)
11+
public HttpEnvironmentDelegatingHandler(IEnvironmentProvider environmentProvider,
12+
IHttpContextAccessor httpContextAccessor)
1213
{
13-
_httpContextAccessor = httpContextAccessor;
1414
_environmentProvider = environmentProvider;
15+
_httpContextAccessor = httpContextAccessor;
1516
}
1617

1718
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
1819
{
19-
var envClaim = _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == "env");
20-
if (envClaim != null)
20+
var requestProvider = _httpContextAccessor.HttpContext?.RequestServices.GetService<IEnvironmentProvider>();
21+
if (requestProvider != null)
22+
{
23+
request.Headers.Add(IsolationConsts.ENVIRONMENT, requestProvider.GetEnvironment());
24+
}
25+
else
2126
{
22-
_httpContextAccessor.HttpContext?.Items.TryAdd(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
23-
request.Headers.Add(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
27+
request.Headers.Add(IsolationConsts.ENVIRONMENT, _environmentProvider.GetEnvironment());
2428
}
2529
return await base.SendAsync(request, cancellationToken);
2630
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/ProjectService.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ namespace Masa.Contrib.StackSdks.Auth.Service;
66
public class ProjectService : IProjectService
77
{
88
readonly ICaller _caller;
9-
readonly IMultiEnvironmentUserContext _multiEnvironmentUserContext;
9+
readonly IUserContext _userContext;
1010

1111
const string PARTY = "api/project/";
1212

13-
public ProjectService(ICaller caller, IMultiEnvironmentUserContext multiEnvironmentUserContext)
13+
public ProjectService(ICaller caller, IUserContext userContext)
1414
{
1515
_caller = caller;
16-
_multiEnvironmentUserContext = multiEnvironmentUserContext;
16+
_userContext = userContext;
1717
}
1818

1919
public async Task<List<ProjectModel>> GetGlobalNavigations()
2020
{
21-
var userId = _multiEnvironmentUserContext.GetUserId<Guid>();
22-
var environment = _multiEnvironmentUserContext.Environment ?? "";
23-
var requestUri = $"{PARTY}navigations?userId={userId}&environment={environment}";
21+
var userId = _userContext.GetUserId<Guid>();
22+
var requestUri = $"{PARTY}navigations?userId={userId}";
2423
return await _caller.GetAsync<List<ProjectModel>>(requestUri) ?? new();
2524
}
2625
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/UserService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public async Task<UserModel> GetCurrentUserAsync()
100100
return await _caller.GetAsync<object, StaffDetailModel>(requestUri, new { userId });
101101
}
102102

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

110110
public async Task<List<UserVisitedModel>> GetVisitedListAsync()

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
2121
public static IServiceCollection AddAuthClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
2222
{
2323
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions));
24-
2524
if (services.All(service => service.ServiceType != typeof(IMultiEnvironmentUserContext)))
2625
throw new Exception("Please add IMultiEnvironmentUserContext first.");
2726

2827
services.AddHttpContextAccessor();
28+
services.TryAddScoped<IEnvironmentProvider, EnvironmentProvider>();
2929
services.AddScoped<HttpEnvironmentDelegatingHandler>();
30-
services.AddScoped<IEnvironmentProvider, EnvironmentProvider>();
3130
services.AddCaller(callerOptions);
3231

3332
services.AddScoped<IAuthClient>(serviceProvider =>

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs

+3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
global using Masa.BuildingBlocks.Service.Caller;
66
global using Masa.BuildingBlocks.Service.Caller.Options;
77
global using Masa.BuildingBlocks.StackSdks.Auth;
8+
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts;
89
global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model;
910
global using Masa.BuildingBlocks.StackSdks.Auth.Service;
1011
global using Masa.Contrib.Service.Caller;
1112
global using Masa.Contrib.Service.Caller.HttpClient;
1213
global using Masa.Contrib.StackSdks.Auth;
1314
global using Microsoft.AspNetCore.Http;
15+
global using Microsoft.Extensions.DependencyInjection;
16+
global using Microsoft.Extensions.DependencyInjection.Extensions;
1417
global using System.Text.Json;
1518
global using static Masa.Contrib.StackSdks.Auth.Constants;

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/Service/AppService.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Masa.BuildingBlocks.StackSdks.Pm.Model;
5-
using Masa.BuildingBlocks.StackSdks.Pm.Service;
6-
74
namespace Masa.Contrib.StackSdks.Pm.Service;
85

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

5552
return result ?? new();
5653
}
54+
55+
public async Task<List<AppDetailModel>> GetListByAppTypes(params AppTypes[] appTypes)
56+
{
57+
var requestUri = $"open-api/app/by-types";
58+
var result = await _caller.PostAsync<AppTypes[], List<AppDetailModel>>(requestUri, appTypes);
59+
60+
return result ?? new();
61+
}
5762
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/Service/ClusterService.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Masa.BuildingBlocks.StackSdks.Pm.Model;
5-
using Masa.BuildingBlocks.StackSdks.Pm.Service;
6-
74
namespace Masa.Contrib.StackSdks.Pm.Service;
85

96
public class ClusterService : IClusterService

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/Service/EnvironmentService.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Masa.BuildingBlocks.StackSdks.Pm.Model;
5-
using Masa.BuildingBlocks.StackSdks.Pm.Service;
6-
74
namespace Masa.Contrib.StackSdks.Pm.Service;
85

96
public class EnvironmentService : IEnvironmentService

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/Service/ProjectService.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4-
using Masa.BuildingBlocks.StackSdks.Pm.Model;
5-
using Masa.BuildingBlocks.StackSdks.Pm.Service;
6-
74
namespace Masa.Contrib.StackSdks.Pm.Service;
85

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

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

2320
return result ?? new();

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Pm/_Imports.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
global using Masa.BuildingBlocks.Service.Caller;
55
global using Masa.BuildingBlocks.Service.Caller.Options;
6+
global using Masa.BuildingBlocks.StackSdks.Pm.Enum;
7+
global using Masa.BuildingBlocks.StackSdks.Pm.Model;
8+
global using Masa.BuildingBlocks.StackSdks.Pm.Service;
69
global using Masa.Contrib.Service.Caller;
710
global using Masa.Contrib.Service.Caller.HttpClient;
811
global using Masa.Contrib.StackSdks.Pm;

src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/ProjectServiceTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public async Task TestGetGlobalNavigationsAsync()
1616
new ProjectModel()
1717
};
1818
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
19-
var requestUri = $"api/project/navigations?userId={userId}&environment=development";
19+
var requestUri = $"api/project/navigations?userId={userId}";
2020
var caller = new Mock<ICaller>();
2121
caller.Setup(provider => provider.GetAsync<List<ProjectModel>>(requestUri, default)).ReturnsAsync(data).Verifiable();
2222
var userContext = new Mock<IMultiEnvironmentUserContext>();

src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/UserServiceTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ public async Task TestGetCurrentStaffAsync()
233233
}
234234

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

0 commit comments

Comments
 (0)