Skip to content

feat(team/permission):add sdk method for team and permission #77

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

Merged
merged 3 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/AuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ public AuthClient(ICallerProvider callerProvider)
UserService = new UserService(callerProvider);
SubjectService = new SubjectService(callerProvider);
TeamService = new TeamService(callerProvider);
PermissionService = new PermissionService(callerProvider);
}

public IUserService UserService { get; }

public ISubjectService SubjectService { get; }

public ITeamService TeamService { get; }

public IPermissionService PermissionService { get; }
}

5 changes: 5 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.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
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth;

public class EnvironmentProvider : IEnvironmentProvider
{
public string GetEnvironment()
{
return "development";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth;

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

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

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var envClaim = _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == "env");
if (envClaim != null)
{
_httpContextAccessor.HttpContext?.Items.TryAdd(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
request.Headers.Add(ENVIRONMENT_KEY, _environmentProvider.GetEnvironment());
}
return await base.SendAsync(request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Infrastructure;

public class BaseSingleton
{
static BaseSingleton()
{
AllSingletons = new Dictionary<Type, object>();
}

public static IDictionary<Type, object> AllSingletons { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Infrastructure;

public class Singleton<T> : BaseSingleton
{
static T instance;

public static T Instance
{
get => instance;
set
{
instance = value;
AllSingletons[typeof(T)] = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="$(MasaUtilsPackageVersion)" />
<PackageReference Include="Masa.Utils.Configuration.Json" Version="$(MasaUtilsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Masa.Utils.Extensions.DependencyInjection" Version="$(MasaUtilsPackageVersion)" />
</ItemGroup>

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

namespace Masa.Contrib.BasicAbility.Auth.Service;

public class PermissionService : IPermissionService
{
readonly ICallerProvider _callerProvider;

const string PARTY = "api/permission/";

public PermissionService(ICallerProvider callerProvider)
{
_callerProvider = callerProvider;
}

//todo remove userId param
public async Task<bool> AuthorizedAsync(string appId, string code, Guid userId)
{
var requestUri = $"{PARTY}authorized?appId={appId}&code={code}&userId={userId}";
return await _callerProvider.GetAsync<bool>(requestUri);
}

public async Task<List<MenuModel>> GetMenusAsync(string appId, Guid userId)
{
var requestUri = $"{PARTY}menus?appId={appId}&userId={userId}";
return await _callerProvider.GetAsync<List<MenuModel>>(requestUri, default) ?? new();
}

public async Task<List<string>> GetElementPermissionsAsync(string appId, Guid userId)
{
var requestUri = $"{PARTY}element-permissions?appId={appId}&userId={userId}";
return await _callerProvider.GetAsync<List<string>>(requestUri, default) ?? new();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Service;

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

namespace Masa.Contrib.BasicAbility.Auth.Service;

public class TeamService : ITeamService
{
readonly ICallerProvider _callerProvider;
readonly string _party = "api/team/";

public TeamService(ICallerProvider callerProvider)
{
Expand All @@ -11,8 +15,18 @@ public TeamService(ICallerProvider callerProvider)

public async Task<TeamDetailModel?> GetDetailAsync(Guid id)
{
var requestUri = $"api/team/deatil";
var requestUri = $"{_party}detail";
return await _callerProvider.GetAsync<object, TeamDetailModel>(requestUri, new { id });
}

public async Task<List<TeamModel>> GetListAsync(Guid userId = default)
{
var requestUri = $"{_party}list";
if (Guid.Empty != userId)
{
requestUri = $"{requestUri}?userId={userId}";
}
return await _callerProvider.GetAsync<List<TeamModel>>(requestUri) ?? new();
}
}

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

namespace Masa.Contrib.BasicAbility.Auth.Service;

public class UserService : IUserService
Expand Down Expand Up @@ -32,5 +35,17 @@ public async Task<List<StaffModel>> GetListByTeamAsync(Guid teamId)
var requestUri = $"api/staff/getListByTeam";
return await _callerProvider.GetAsync<object, List<StaffModel>>(requestUri, new { id = teamId }) ?? new();
}

public async Task<bool> ValidateCredentialsByAccountAsync(string account, string password)
{
var requestUri = $"api/user/validateByAccount";
return await _callerProvider.PostAsync<object, bool>(requestUri, new { account = account, password = password });
}

public async Task<UserModel> FindByAccountAsync(string account)
{
var requestUri = $"api/user/findByAccount";
return await _callerProvider.GetAsync<object, UserModel>(requestUri, new { account = account }) ?? new();
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth;

Expand All @@ -14,18 +15,22 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services,
{
builder.Name = DEFAULT_CLIENT_NAME;
builder.Configure = opt => opt.BaseAddress = new Uri(authServiceBaseAddress);
});
}).AddHttpMessageHandler<HttpEnvironmentDelegatingHandler>();
});
}

public static IServiceCollection AddAuthClient(this IServiceCollection services, Action<CallerOptions> callerOptions)
{
ArgumentNullException.ThrowIfNull(callerOptions, nameof(callerOptions));

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

services.AddScoped<IAuthClient>(serviceProvider =>
{
Singleton<IServiceProvider>.Instance = serviceProvider;
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
var authClient = new AuthClient(callProvider);
return authClient;
Expand Down
3 changes: 3 additions & 0 deletions src/BasicAbility/Masa.Contrib.BasicAbility.Auth/_import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
global using Masa.BuildingBlocks.BasicAbility.Auth;
global using Masa.BuildingBlocks.BasicAbility.Auth.Model;
global using Masa.BuildingBlocks.BasicAbility.Auth.Service;
global using Masa.Contrib.BasicAbility.Auth.Infrastructure;
global using Masa.Contrib.BasicAbility.Auth.Service;
global using Masa.Utils.Caller.Core;
global using Masa.Utils.Caller.HttpClient;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.Caching.Memory;
global using Microsoft.Extensions.DependencyInjection;
global using static Masa.Contrib.BasicAbility.Auth.Constants;
16 changes: 16 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/BaseAuthTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Tests
{
public class BaseAuthTest
{
[TestInitialize]
public void Initialize()
{
IServiceCollection service = new ServiceCollection();
service.AddAuthClient("https://localhost:18102");
var authClient = service.BuildServiceProvider().GetRequiredService<IAuthClient>();
}
}
}
49 changes: 49 additions & 0 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/PermissionServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class PermissionServiceTest : BaseAuthTest
{
[TestMethod]
[DataRow("app1", "A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1")]
public async Task TestGetMenusAsync(string appId, string userId)
{
var data = new List<MenuModel>();
var requestUri = $"api/permission/menus?appId={appId}&userId={userId}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<List<MenuModel>>(requestUri, default)).ReturnsAsync(data).Verifiable();
var authClient = new AuthClient(callerProvider.Object);
var result = await authClient.PermissionService.GetMenusAsync(appId, Guid.Parse(userId));
callerProvider.Verify(provider => provider.GetAsync<List<MenuModel>>(It.IsAny<string>(), default), Times.Once);
Assert.IsTrue(result is not null);
}

[TestMethod]
[DataRow("app1", "code", "A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1")]
public async Task TestAuthorizedAsync(string appId, string code, string userId)
{
var data = false;
var requestUri = $"api/permission/authorized?code={code}&userId={userId}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<bool>(requestUri, default)).ReturnsAsync(data).Verifiable();
var authClient = new AuthClient(callerProvider.Object);
var result = await authClient.PermissionService.AuthorizedAsync(appId, code, Guid.Parse(userId));
callerProvider.Verify(provider => provider.GetAsync<bool>(It.IsAny<string>(), default), Times.Once);
}

[TestMethod]
[DataRow("app1", "A9C8E0DD-1E9C-474D-8FE7-8BA9672D53D1")]
public async Task TestGetElementPermissionsAsync(string appId, string userId)
{
var data = new List<string>();
var requestUri = $"api/permission/element-permissions?appId={appId}&userId={userId}";
var callerProvider = new Mock<ICallerProvider>();
callerProvider.Setup(provider => provider.GetAsync<List<string>>(requestUri, default)).ReturnsAsync(data).Verifiable();
var authClient = new AuthClient(callerProvider.Object);
var result = await authClient.PermissionService.GetElementPermissionsAsync(appId, Guid.Parse(userId));
callerProvider.Verify(provider => provider.GetAsync<List<string>>(It.IsAny<string>(), default), Times.Once);
Assert.IsTrue(result is not null);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class SubjectServiceTest
public class SubjectServiceTest : BaseAuthTest
{
[TestMethod]
public async Task TestGetListAsync()
Expand Down
22 changes: 19 additions & 3 deletions test/Masa.Contrib.BasicAbility.Auth.Tests/TeamServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.BasicAbility.Auth.Tests;

[TestClass]
public class TeamServiceTest
public class TeamServiceTest : BaseAuthTest
{
[TestMethod]
public async Task TestGetListAsync()
public async Task TestGetDetailAsync()
{
var data = new TeamDetailModel();
Guid teamId = Guid.NewGuid();
var requestUri = $"api/team/deatil";
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 authClient = new AuthClient(callerProvider.Object);
var result = await authClient.TeamService.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()
{
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 authClient = new AuthClient(callerProvider.Object);
var result = await authClient.TeamService.GetListAsync();
callerProvider.Verify(provider => provider.GetAsync<List<TeamModel>>(requestUri, default), Times.Once);
Assert.IsTrue(result is not null);
}
}

Loading