Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth sdk):update user center and add readme #113

Merged
merged 6 commits into from
Jun 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@ The purpose of MASA.Contrib is based on [MASA.BuildingBlocks](https://github.com
* Storage: cloud storage
* [Aliyun Storage](./src/Storage/Masa.Contrib.Storage.ObjectStorage.Aliyun/README.md)
* Operational capacity
* [Auth](./src/BasicAbility/Masa.Contrib.BasicAbility.Auth/README.md): Authentication and Authorization
* [Dcc](./src/BasicAbility/Masa.Contrib.BasicAbility.Dcc/README.md): Distributed Configuration Center
* [PM](./src/BasicAbility/Masa.Contrib.BasicAbility.Pm/README.md): Project Management
* [Scheduler](./src/BasicAbility/Masa.Contrib.BasicAbility.Scheduler/README.md): Distributed Scheduler
3 changes: 2 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
中 | [EN](README.md)
中 | [EN](README.md)

[![codecov](https://codecov.io/gh/masastack/MASA.Contrib/branch/main/graph/badge.svg?token=87TPNHUHW2)](https://codecov.io/gh/masastack/MASA.Contrib)

@@ -56,6 +56,7 @@ MASA.Contrib是基于[MASA.BuildingBlocks](https://github.com/masastack/MASA.Bui
* Storage: 云存储
* [阿里云存储](./src/Storage/Masa.Contrib.Storage.ObjectStorage.Aliyun/README.zh-CN.md)
* 业务能力
* [Auth](./src/BasicAbility/Masa.Contrib.BasicAbility.Auth/README.zh-CN.md): 权限认证
* [Dcc](./src/BasicAbility/Masa.Contrib.BasicAbility.Dcc/README.zh-CN.md): 分布式配置中心
* [PM](./src/BasicAbility/Masa.Contrib.BasicAbility.Pm/README.zh-CN.md): 项目管理
* [Scheduler](./src/BasicAbility/Masa.Contrib.BasicAbility.Scheduler/README.zh-CN.md): 分布式调度中心
Original file line number Diff line number Diff line change
@@ -21,14 +21,15 @@ public static IServiceCollection AddOidcDbContext<T>(this IServiceCollection ser
public static IServiceCollection SeedClientData(this IServiceCollection services, List<Client> clients)
{
var clientRepository = services.BuildServiceProvider().GetRequiredService<IClientRepository>();
var apiScopeRepository = services.BuildServiceProvider().GetRequiredService<IApiScopeRepository>();
var identityResourceRepository = services.BuildServiceProvider().GetRequiredService<IIdentityResourceRepository>();

var scopes = clients.SelectMany(c => c.AllowedScopes);
foreach (var scope in scopes)
{
if (apiScopeRepository.FindAsync(s => s.Name == scope.Scope).Result == null)
if (identityResourceRepository.FindAsync(s => s.Name == scope.Scope).Result == null)
{
_ = apiScopeRepository.AddAsync(new ApiScope(scope.Scope)).Result;
_ = identityResourceRepository.AddAsync(new IdentityResource(scope.Scope, scope.Scope, "",
true, true, true, true, true)).Result;
}
}
foreach (var client in clients)
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ public AuthClient(ICallerProvider callerProvider, IMultiEnvironmentUserContext u
{
UserService = new UserService(callerProvider, userContext);
SubjectService = new SubjectService(callerProvider);
TeamService = new TeamService(callerProvider);
TeamService = new TeamService(callerProvider, userContext);
ProjectService = new ProjectService(callerProvider, userContext);
PermissionService = new PermissionService(callerProvider, userContext);
}
46 changes: 46 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[中](README.zh-CN.md) | EN

## Masa.Contrib.BasicAbility.Auth

Injecting IAuthClient interface,cal the service to obtain the capabilities provided by the auth SDK.
SDK need to get current user ID dependency Masa.Contrib.Identity.IdentityModel(../../Identity/Masa.Contrib.Identity.IdentityModel/README.zh,Therefore,the identitymodel service needs to be added before use.

### Service Introduction

```c#
IAuthClient
├── UserService
├── SubjectService
├── TeamService
├── PermissionService
└── ProjectService
```

### Use Introduction

#### Install dependent package

```C#
Install-Package Masa.Contrib.BasicAbility.Auth
```

#### Register auth service

```C#
builder.Services.AddAuthClient("http://authservice.com");
```

> `http://authservice.com` is auth service address

#### Dependency injection IAuthClient

```c#
var app = builder.Build();

app.MapGet("/GetTeams", ([FromServices] IAuthClient authClient) =>
{
return authClient.TeamService.GetAllAsync();
});

app.Run();
```
45 changes: 45 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
中 | [EN](README.md)

## Masa.Contrib.BasicAbility.Auth

通过注入IAuthClient接口,调用对应Service获取Auth SDK 提供的能力。
SDK获取当前用户ID依赖Masa.Contrib.Identity.IdentityModel(../../Identity/Masa.Contrib.Identity.IdentityModel/README.zh-CN.zh,所以使用前需添加IdentityModel服务。

### 服务介绍
```c#
IAuthClient
├── UserService 用户服务
├── SubjectService 全局搜索用户、角色、团队、组织架构
├── TeamService 团队服务
├── PermissionService 权限、菜单服务
└── ProjectService 全局导航服务
```

### 使用介绍

#### 安装依赖包

```C#
Install-Package Masa.Contrib.BasicAbility.Auth
```

#### 注册相关服务

```C#
builder.Services.AddAuthClient("http://authservice.com");
```

> `http://authservice.com` 为Auth后台服务地址

#### 依赖注入IAuthClient

```c#
var app = builder.Build();

app.MapGet("/GetTeams", ([FromServices] IAuthClient authClient) =>
{
return authClient.TeamService.GetAllAsync();
});

app.Run();
```
Original file line number Diff line number Diff line change
@@ -7,10 +7,12 @@ public class TeamService : ITeamService
{
readonly ICallerProvider _callerProvider;
readonly string _party = "api/team/";
readonly IUserContext _userContext;

public TeamService(ICallerProvider callerProvider)
public TeamService(ICallerProvider callerProvider, IUserContext userContext)
{
_callerProvider = callerProvider;
_userContext = userContext;
}

public async Task<TeamDetailModel?> GetDetailAsync(Guid id)
@@ -28,5 +30,18 @@ public async Task<List<TeamModel>> GetListAsync(Guid userId = default)
}
return await _callerProvider.GetAsync<List<TeamModel>>(requestUri) ?? new();
}

public async Task<List<TeamModel>> GetAllAsync()
{
var requestUri = $"{_party}list";
return await _callerProvider.GetAsync<List<TeamModel>>(requestUri) ?? new();
}

public async Task<List<TeamModel>> GetUserTeamsAsync()
{
var userId = _userContext.GetUserId<Guid>();
var requestUri = $"{_party}list?userId={userId}";
return await _callerProvider.GetAsync<List<TeamModel>>(requestUri) ?? new();
}
}

Original file line number Diff line number Diff line change
@@ -50,8 +50,9 @@ public async Task<UserModel> FindByAccountAsync(string account)
return await _callerProvider.GetAsync<object, UserModel>(requestUri, new { account = account }) ?? new();
}

public async Task<UserModel> FindByIdAsync(Guid id)
public async Task<UserModel> GetCurrentUserAsync()
{
var id = _userContext.GetUserId<Guid>();
var requestUri = $"api/user/findById";
return await _callerProvider.GetAsync<object, UserModel>(requestUri, new { id }) ?? new();
}
26 changes: 22 additions & 4 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/TeamServiceTest.cs
Original file line number Diff line number Diff line change
@@ -14,21 +14,39 @@ public async Task TestGetDetailAsync()
var requestUri = $"api/team/detail";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable();
var teamService = new Mock<TeamService>(callerProvider.Object);
var userContext = new Mock<IUserContext>();
var teamService = new Mock<TeamService>(callerProvider.Object, userContext.Object);
var result = await teamService.Object.GetDetailAsync(teamId);
callerProvider.Verify(provider => provider.GetAsync<object, TeamDetailModel>(requestUri, It.IsAny<object>(), default), Times.Once);
Assert.IsTrue(result is not null);
}

[TestMethod]
public async Task TestGetListAsync()
public async Task TestGetAllAsync()
{
var data = new List<TeamModel>();
var requestUri = $"api/team/list";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<List<TeamModel>>(requestUri, default)).ReturnsAsync(data).Verifiable();
var teamService = new Mock<TeamService>(callerProvider.Object);
var result = await teamService.Object.GetListAsync();
var userContext = new Mock<IUserContext>();
var teamService = new Mock<TeamService>(callerProvider.Object, userContext.Object);
var result = await teamService.Object.GetAllAsync();
callerProvider.Verify(provider => provider.GetAsync<List<TeamModel>>(requestUri, default), Times.Once);
Assert.IsTrue(result is not null);
}

[TestMethod]
public async Task TestGetUserTeamsAsync()
{
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
var data = new List<TeamModel>();
var requestUri = $"api/team/list?userId={userId}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<List<TeamModel>>(requestUri, default)).ReturnsAsync(data).Verifiable();
var userContext = new Mock<IUserContext>();
userContext.Setup(user => user.GetUserId<Guid>()).Returns(userId).Verifiable();
var teamService = new Mock<TeamService>(callerProvider.Object, userContext.Object);
var result = await teamService.Object.GetUserTeamsAsync();
callerProvider.Verify(provider => provider.GetAsync<List<TeamModel>>(requestUri, default), Times.Once);
Assert.IsTrue(result is not null);
}
16 changes: 16 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/UserServiceTest.cs
Original file line number Diff line number Diff line change
@@ -105,6 +105,22 @@ public async Task TestFindByAccountAsync(string account)
Assert.IsTrue(result is not null);
}

[TestMethod]
public async Task TestGetCurrentUserAsync()
{
var userId = Guid.Parse("A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1");
var data = new UserModel();
var requestUri = $"api/user/findById";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<object, UserModel>(requestUri, It.IsAny<object>(), default)).ReturnsAsync(data).Verifiable();
var userContext = new Mock<IUserContext>();
userContext.Setup(user => user.GetUserId<Guid>()).Returns(userId).Verifiable();
var userService = new UserService(callerProvider.Object, userContext.Object);
var result = await userService.GetCurrentUserAsync();
callerProvider.Verify(provider => provider.GetAsync<object, UserModel>(requestUri, It.IsAny<object>(), default), Times.Once);
Assert.IsTrue(result is not null);
}

[TestMethod]
[DataRow("https://www.baidu.com/")]
public async Task TestVisitedAsync(string url)