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

[PM-15128] Add Promote Provider Service User functionality to Bitwarden Portal #5118

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
45 changes: 45 additions & 0 deletions src/Admin/Controllers/ToolsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Bit.Admin.Enums;
using Bit.Admin.Models;
using Bit.Admin.Utilities;
using Bit.Core;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Entities;
using Bit.Core.Models.BitStripe;
using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces;
Expand All @@ -28,6 +30,7 @@
private readonly ITransactionRepository _transactionRepository;
private readonly IInstallationRepository _installationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IProviderUserRepository _providerUserRepository;
private readonly IPaymentService _paymentService;
private readonly ITaxRateRepository _taxRateRepository;
private readonly IStripeAdapter _stripeAdapter;
Expand All @@ -41,6 +44,7 @@
ITransactionRepository transactionRepository,
IInstallationRepository installationRepository,
IOrganizationUserRepository organizationUserRepository,
IProviderUserRepository providerUserRepository,

Check warning on line 47 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L47

Added line #L47 was not covered by tests
ITaxRateRepository taxRateRepository,
IPaymentService paymentService,
IStripeAdapter stripeAdapter,
Expand All @@ -53,6 +57,7 @@
_transactionRepository = transactionRepository;
_installationRepository = installationRepository;
_organizationUserRepository = organizationUserRepository;
_providerUserRepository = providerUserRepository;

Check warning on line 60 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L60

Added line #L60 was not covered by tests
_taxRateRepository = taxRateRepository;
_paymentService = paymentService;
_stripeAdapter = stripeAdapter;
Expand Down Expand Up @@ -220,6 +225,46 @@
return RedirectToAction("Edit", "Organizations", new { id = model.OrganizationId.Value });
}

[RequireFeature(FeatureFlagKeys.PromoteProviderServiceUserTool)]
[RequirePermission(Permission.Tools_PromoteProviderServiceUser)]
public IActionResult PromoteProviderServiceUser()
{
return View();
}

Check warning on line 233 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L231-L233

Added lines #L231 - L233 were not covered by tests

[HttpPost]
[ValidateAntiForgeryToken]
[RequireFeature(FeatureFlagKeys.PromoteProviderServiceUserTool)]
[RequirePermission(Permission.Tools_PromoteProviderServiceUser)]
public async Task<IActionResult> PromoteProviderServiceUser(PromoteProviderServiceUserModel model)
{

Check warning on line 240 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L240

Added line #L240 was not covered by tests
if (!ModelState.IsValid)
{
return View(model);

Check warning on line 243 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L242-L243

Added lines #L242 - L243 were not covered by tests
}

var providerUsers = await _providerUserRepository.GetManyByProviderAsync(
model.ProviderId.Value, null);

Check warning on line 247 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L246-L247

Added lines #L246 - L247 were not covered by tests
var serviceUser = providerUsers.FirstOrDefault(u => u.UserId == model.UserId.Value);
if (serviceUser == null)
{
ModelState.AddModelError(nameof(model.UserId), "Service User Id not found in this provider.");
}

Check warning on line 252 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L250-L252

Added lines #L250 - L252 were not covered by tests
else if (serviceUser.Type != Core.AdminConsole.Enums.Provider.ProviderUserType.ServiceUser)
{
ModelState.AddModelError(nameof(model.UserId), "User is not a service user of this provider.");
}

Check warning on line 256 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L254-L256

Added lines #L254 - L256 were not covered by tests

if (!ModelState.IsValid)
{
return View(model);

Check warning on line 260 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L259-L260

Added lines #L259 - L260 were not covered by tests
}

serviceUser.Type = Core.AdminConsole.Enums.Provider.ProviderUserType.ProviderAdmin;
await _providerUserRepository.ReplaceAsync(serviceUser);
return RedirectToAction("Edit", "Providers", new { id = model.ProviderId.Value });
}

Check warning on line 266 in src/Admin/Controllers/ToolsController.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Controllers/ToolsController.cs#L263-L266

Added lines #L263 - L266 were not covered by tests

[RequirePermission(Permission.Tools_GenerateLicenseFile)]
public IActionResult GenerateLicense()
{
Expand Down
1 change: 1 addition & 0 deletions src/Admin/Enums/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum Permission

Tools_ChargeBrainTreeCustomer,
Tools_PromoteAdmin,
Tools_PromoteProviderServiceUser,
Tools_GenerateLicenseFile,
Tools_ManageTaxRates,
Tools_ManageStripeSubscriptions,
Expand Down
13 changes: 13 additions & 0 deletions src/Admin/Models/PromoteProviderServiceUserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace Bit.Admin.Models;

public class PromoteProviderServiceUserModel
{
[Required]
[Display(Name = "Provider Service User Id")]
public Guid? UserId { get; set; }

Check warning on line 9 in src/Admin/Models/PromoteProviderServiceUserModel.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Models/PromoteProviderServiceUserModel.cs#L9

Added line #L9 was not covered by tests
[Required]
[Display(Name = "Provider Id")]
public Guid? ProviderId { get; set; }

Check warning on line 12 in src/Admin/Models/PromoteProviderServiceUserModel.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Models/PromoteProviderServiceUserModel.cs#L12

Added line #L12 was not covered by tests
}
2 changes: 2 additions & 0 deletions src/Admin/Utilities/RolePermissionMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
Permission.Provider_ResendEmailInvite,
Permission.Tools_ChargeBrainTreeCustomer,
Permission.Tools_PromoteAdmin,
Permission.Tools_PromoteProviderServiceUser,

Check warning on line 48 in src/Admin/Utilities/RolePermissionMapping.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Utilities/RolePermissionMapping.cs#L48

Added line #L48 was not covered by tests
Permission.Tools_GenerateLicenseFile,
Permission.Tools_ManageTaxRates,
Permission.Tools_ManageStripeSubscriptions
Expand Down Expand Up @@ -91,6 +92,7 @@
Permission.Provider_ResendEmailInvite,
Permission.Tools_ChargeBrainTreeCustomer,
Permission.Tools_PromoteAdmin,
Permission.Tools_PromoteProviderServiceUser,

Check warning on line 95 in src/Admin/Utilities/RolePermissionMapping.cs

View check run for this annotation

Codecov / codecov/patch

src/Admin/Utilities/RolePermissionMapping.cs#L95

Added line #L95 was not covered by tests
Permission.Tools_GenerateLicenseFile,
Permission.Tools_ManageTaxRates,
Permission.Tools_ManageStripeSubscriptions,
Expand Down
12 changes: 11 additions & 1 deletion src/Admin/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@using Bit.Admin.Enums;
@using Bit.Core

@inject SignInManager<IdentityUser> SignInManager
@inject Bit.Core.Settings.GlobalSettings GlobalSettings
@inject Bit.Admin.Services.IAccessControlService AccessControlService
@inject Bit.Core.Services.IFeatureService FeatureService

@{
var canViewUsers = AccessControlService.UserHasPermission(Permission.User_List_View);
Expand All @@ -11,13 +13,15 @@
var canChargeBraintree = AccessControlService.UserHasPermission(Permission.Tools_ChargeBrainTreeCustomer);
var canCreateTransaction = AccessControlService.UserHasPermission(Permission.Tools_CreateEditTransaction);
var canPromoteAdmin = AccessControlService.UserHasPermission(Permission.Tools_PromoteAdmin);
var canPromoteProviderServiceUser = FeatureService.IsEnabled(FeatureFlagKeys.PromoteProviderServiceUserTool) &&
AccessControlService.UserHasPermission(Permission.Tools_PromoteProviderServiceUser);

Check warning on line 17 in src/Admin/Views/Shared/_Layout.cshtml

View check run for this annotation

Codecov / codecov/patch

src/Admin/Views/Shared/_Layout.cshtml#L17

Added line #L17 was not covered by tests
var canGenerateLicense = AccessControlService.UserHasPermission(Permission.Tools_GenerateLicenseFile);
var canManageTaxRates = AccessControlService.UserHasPermission(Permission.Tools_ManageTaxRates);
var canManageStripeSubscriptions = AccessControlService.UserHasPermission(Permission.Tools_ManageStripeSubscriptions);
var canProcessStripeEvents = AccessControlService.UserHasPermission(Permission.Tools_ProcessStripeEvents);
var canMigrateProviders = AccessControlService.UserHasPermission(Permission.Tools_MigrateProviders);

var canViewTools = canChargeBraintree || canCreateTransaction || canPromoteAdmin ||
var canViewTools = canChargeBraintree || canCreateTransaction || canPromoteAdmin || canPromoteProviderServiceUser ||
canGenerateLicense || canManageTaxRates || canManageStripeSubscriptions;
}

Expand Down Expand Up @@ -91,6 +95,12 @@
Promote Admin
</a>
}
@if (canPromoteProviderServiceUser)
{
<a class="dropdown-item" asp-controller="Tools" asp-action="PromoteProviderServiceUser">
Promote Provider Service User
</a>
}

Check warning on line 103 in src/Admin/Views/Shared/_Layout.cshtml

View check run for this annotation

Codecov / codecov/patch

src/Admin/Views/Shared/_Layout.cshtml#L103

Added line #L103 was not covered by tests
@if (canGenerateLicense)
{
<a class="dropdown-item" asp-controller="Tools" asp-action="GenerateLicense">
Expand Down
25 changes: 25 additions & 0 deletions src/Admin/Views/Tools/PromoteProviderServiceUser.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@model PromoteProviderServiceUserModel
@{
ViewData["Title"] = "Promote Provider Service User";
}

<h1>Promote Provider Service User</h1>

<form method="post">
<div asp-validation-summary="All" class="alert alert-danger"></div>
<div class="row">
<div class="col-md">
<div class="mb-3">
<label asp-for="UserId" class="form-label"></label>
<input type="text" class="form-control" asp-for="UserId">
</div>
</div>
<div class="col-md">
<div class="mb-3">
<label asp-for="ProviderId" class="form-label"></label>
<input type="text" class="form-control" asp-for="ProviderId">
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Promote Service User</button>
</form>
1 change: 1 addition & 0 deletions src/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public static class FeatureFlagKeys
public const string InlineMenuTotp = "inline-menu-totp";
public const string PM12443RemovePagingLogic = "pm-12443-remove-paging-logic";
public const string SelfHostLicenseRefactor = "pm-11516-self-host-license-refactor";
public const string PromoteProviderServiceUserTool = "pm-15128-promote-provider-service-user-tool";

public static List<string> GetAllKeys()
{
Expand Down
Loading