Skip to content

Commit

Permalink
PT-8715: Get organization maintainer role name from app settings (#26)
Browse files Browse the repository at this point in the history
* Get organization maintainer role name from app settings

* Getting role method

* Use IOptions for getting role name

* Update index.md
  • Loading branch information
getmansky authored Aug 31, 2022
1 parent 63ab977 commit b133373
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
14 changes: 13 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,20 @@ mutation requestRegistration (command: InputRequestRegistrationType!) {
}
```

> The mutation registers a company when all argumets have been provided, and registers a customer only when the company value is null. If a company is created, the customer becomes its member and owner.
> The mutation registers a company when all argumets have been provided, and registers a customer only when the company value is null. If a company is created, the customer becomes its member and owner. In this case customer gets the **Organization maintainer** role whose name or ID must be provided in **appsettings.json**.
>
> The user that creates a company and/or customer is always displayed as *frontend*.
>
> The company status is determined by the *Company default status* store setting, while contact and account statuses come from the *Contact default status* setting. Both settings must be provided in advance.
>
> Example of the role name settings provided below.
```
{
...
"FrontendSecurity": {
"OrganizationMaintainerRole": "Organization maintainer"
},
...
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
using VirtoCommerce.NotificationsModule.Core.Model;
using VirtoCommerce.Platform.Security;
using VirtoCommerce.ProfileExperienceApiModule.Data.Models.RegisterOrganization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using VirtoCommerce.ProfileExperienceApiModule.Data.Configuration;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands
{
Expand All @@ -41,10 +44,10 @@ public class RegisterRequestCommandHandler : IRequestHandler<RegisterRequestComm
private readonly NewContactValidator _contactValidator;
private readonly AccountValidator _accountValidator;
private readonly OrganizationValidator _organizationValidator;
private readonly IOptions<FrontendSecurityOptions> _securityOptions;

private const string Creator = "frontend";
private const string UserType = "Manager";
private const string MaintainerRoleId = "org-maintainer";
#pragma warning disable S107
public RegisterRequestCommandHandler(IMapper mapper,
IDynamicPropertyUpdaterService dynamicPropertyUpdater,
Expand All @@ -55,7 +58,8 @@ public RegisterRequestCommandHandler(IMapper mapper,
IAccountService accountService,
NewContactValidator contactValidator,
AccountValidator accountValidator,
OrganizationValidator organizationValidator)
OrganizationValidator organizationValidator,
IOptions<FrontendSecurityOptions> securityOptions)
#pragma warning restore S107
{
_mapper = mapper;
Expand All @@ -68,6 +72,7 @@ public RegisterRequestCommandHandler(IMapper mapper,
_contactValidator = contactValidator;
_accountValidator = accountValidator;
_organizationValidator = organizationValidator;
_securityOptions = securityOptions;
}

public virtual async Task<RegisterOrganizationResult> Handle(RegisterRequestCommand request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -128,10 +133,9 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque

if (organization != null)
{
var maintainerRole = await _accountService.FindRoleById(MaintainerRoleId);
var maintainerRole = await GetMaintainerRole(result, tokenSource);
if (maintainerRole == null)
{
SetErrorResult(result, "Role not found",$"Organization maintainer role with id {MaintainerRoleId} not found", tokenSource);
return result;
}

Expand Down Expand Up @@ -181,6 +185,24 @@ private async Task<RegisterOrganizationResult> ProcessRequestAsync(RegisterReque
return result;
}

private async Task<Role> GetMaintainerRole(RegisterOrganizationResult result, CancellationTokenSource tokenSource)
{
var maintainerRoleId = _securityOptions.Value.OrganizationMaintainerRole;
if (maintainerRoleId == null)
{
SetErrorResult(result, "Role not configured", "Organization maintainer role configuration is not found in the app settings", tokenSource);
return null;
}

var role = await _accountService.FindRoleByName(maintainerRoleId) ?? await _accountService.FindRoleById(maintainerRoleId);
if (role == null)
{
SetErrorResult(result, "Role not found", $"Organization maintainer role {maintainerRoleId} not found", tokenSource);
}

return role;
}

private static AccountCreationResult GetAccountCreationResult(IdentityResult identityResult, ApplicationUser account)
{
return new AccountCreationResult
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Configuration
{
public class FrontendSecurityOptions
{
public string OrganizationMaintainerRole { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@ public async Task<Role> FindRoleById(string roleId)
using var roleManager = _roleManagerFactory();
return await roleManager.FindByIdAsync(roleId);
}

public async Task<Role> FindRoleByName(string roleName)
{
using var roleManager = _roleManagerFactory();
return await roleManager.FindByNameAsync(roleName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IAccountService
public Task<IdentityResult> CreateAccountAsync(ApplicationUser account);
public Task<ApplicationUser> GetAccountAsync(string userName);
public Task<Role> FindRoleById(string roleId);
public Task<Role> FindRoleByName(string roleName);
}
}
6 changes: 5 additions & 1 deletion src/VirtoCommerce.ProfileExperienceApiModule.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VirtoCommerce.ExperienceApiModule.Core.Extensions;
using VirtoCommerce.ExperienceApiModule.Core.Infrastructure;
Expand All @@ -15,6 +16,7 @@
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Organization;
using VirtoCommerce.ProfileExperienceApiModule.Data.Authorization;
using VirtoCommerce.ProfileExperienceApiModule.Data.Configuration;
using VirtoCommerce.ProfileExperienceApiModule.Data.Middlewares;
using VirtoCommerce.ProfileExperienceApiModule.Data.Schemas;
using VirtoCommerce.ProfileExperienceApiModule.Data.Services;
Expand All @@ -23,9 +25,10 @@

namespace VirtoCommerce.CusomersExperienceApi.Web
{
public class Module : IModule
public class Module : IModule, IHasConfiguration
{
public ManifestModuleInfo ModuleInfo { get; set; }
public IConfiguration Configuration { get; set; }

public void Initialize(IServiceCollection serviceCollection)
{
Expand All @@ -44,6 +47,7 @@ public void Initialize(IServiceCollection serviceCollection)
serviceCollection.AddTransient<IContactAggregateRepository, ContactAggregateRepository>();
serviceCollection.AddTransient<IAccountService, AccountsService>();
serviceCollection.AddSingleton<IAuthorizationHandler, ProfileAuthorizationHandler>();
serviceCollection.AddOptions<FrontendSecurityOptions>().Bind(Configuration.GetSection("FrontendSecurity")).ValidateDataAnnotations();

serviceCollection.AddAutoMapper(typeof(XProfileAnchor));

Expand Down

0 comments on commit b133373

Please sign in to comment.