From 69e7c651cd2eac23b5b6a5927c09b07dad6955ba Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Thu, 26 Aug 2021 19:31:56 +0200 Subject: [PATCH] Remove policies from the portal (#1534) --- .../Portal/Controllers/PoliciesController.cs | 175 ------------ .../Portal/EnterprisePortalCurrentContext.cs | 3 - .../Portal/Models/MasterPasswordDataModel.cs | 21 -- .../Models/PasswordGeneratorDataModel.cs | 37 --- .../src/Portal/Models/PoliciesModel.cs | 41 --- .../src/Portal/Models/PolicyEditModel.cs | 135 ---------- .../src/Portal/Models/PolicyModel.cs | 69 ----- .../src/Portal/Views/Home/Index.cshtml | 10 - .../src/Portal/Views/Policies/Edit.cshtml | 250 ------------------ .../src/Portal/Views/Policies/Index.cshtml | 29 -- .../src/Portal/Views/Shared/_Layout.cshtml | 13 +- 11 files changed, 1 insertion(+), 782 deletions(-) delete mode 100644 bitwarden_license/src/Portal/Controllers/PoliciesController.cs delete mode 100644 bitwarden_license/src/Portal/Models/MasterPasswordDataModel.cs delete mode 100644 bitwarden_license/src/Portal/Models/PasswordGeneratorDataModel.cs delete mode 100644 bitwarden_license/src/Portal/Models/PoliciesModel.cs delete mode 100644 bitwarden_license/src/Portal/Models/PolicyEditModel.cs delete mode 100644 bitwarden_license/src/Portal/Models/PolicyModel.cs delete mode 100644 bitwarden_license/src/Portal/Views/Policies/Edit.cshtml delete mode 100644 bitwarden_license/src/Portal/Views/Policies/Index.cshtml diff --git a/bitwarden_license/src/Portal/Controllers/PoliciesController.cs b/bitwarden_license/src/Portal/Controllers/PoliciesController.cs deleted file mode 100644 index 7d2632c78..000000000 --- a/bitwarden_license/src/Portal/Controllers/PoliciesController.cs +++ /dev/null @@ -1,175 +0,0 @@ -using System; -using System.Threading.Tasks; -using Bit.Core.Enums; -using Bit.Core.Models.Table; -using Bit.Core.Repositories; -using Bit.Core.Services; -using Bit.Portal.Models; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace Bit.Portal.Controllers -{ - [Authorize] - public class PoliciesController : Controller - { - private readonly IUserService _userService; - private readonly IOrganizationService _organizationService; - private readonly IPolicyService _policyService; - private readonly IPolicyRepository _policyRepository; - private readonly EnterprisePortalCurrentContext _enterprisePortalCurrentContext; - private readonly II18nService _i18nService; - - public PoliciesController( - IUserService userService, - IOrganizationService organizationService, - IPolicyService policyService, - IPolicyRepository policyRepository, - EnterprisePortalCurrentContext enterprisePortalCurrentContext, - II18nService i18nService) - { - _userService = userService; - _organizationService = organizationService; - _policyService = policyService; - _policyRepository = policyRepository; - _enterprisePortalCurrentContext = enterprisePortalCurrentContext; - _i18nService = i18nService; - } - - public async Task Index() - { - var orgId = _enterprisePortalCurrentContext.SelectedOrganizationId; - if (orgId == null) - { - return Redirect("~/"); - } - - if (!_enterprisePortalCurrentContext.SelectedOrganizationDetails.UsePolicies || - !_enterprisePortalCurrentContext.CanManagePoliciesForSelectedOrganization) - { - return Redirect("~/"); - } - - var policies = await _policyRepository.GetManyByOrganizationIdAsync(orgId.Value); - return View(new PoliciesModel(policies, _enterprisePortalCurrentContext.SelectedOrganizationDetails)); - } - - [HttpGet("/edit/{type}")] - public async Task Edit(PolicyType type) - { - var orgId = _enterprisePortalCurrentContext.SelectedOrganizationId; - if (orgId == null) - { - return Redirect("~"); - } - - if (!_enterprisePortalCurrentContext.SelectedOrganizationDetails.UsePolicies || - !_enterprisePortalCurrentContext.CanManagePoliciesForSelectedOrganization) - { - return Redirect("~/"); - } - - var policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId.Value, type); - return BuildPolicyView(policy, type); - } - - [HttpPost("/edit/{type}")] - [ValidateAntiForgeryToken] - public async Task Edit(PolicyType type, PolicyEditModel model) - { - var orgId = _enterprisePortalCurrentContext.SelectedOrganizationId; - if (orgId == null) - { - return Redirect("~"); - } - - if (!_enterprisePortalCurrentContext.SelectedOrganizationDetails.UsePolicies || - !_enterprisePortalCurrentContext.CanManagePoliciesForSelectedOrganization) - { - return Redirect("~/"); - } - - await ValidateDependentPolicies(type, orgId, model.Enabled); - var policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId.Value, type); - if (!ModelState.IsValid) - { - return BuildPolicyView(policy, type); - } - - if (policy == null) - { - policy = model.ToPolicy(type, orgId.Value); - } - else - { - policy = model.ToPolicy(policy); - } - - var userId = _userService.GetProperUserId(User); - await _policyService.SaveAsync(policy, _userService, _organizationService, userId); - return RedirectToAction("Edit", new { type }); - } - - private IActionResult BuildPolicyView(Policy policy, PolicyType type) - { - if (policy == null) - { - return View(new PolicyEditModel(type, _i18nService)); - } - else - { - return View(new PolicyEditModel(policy, _i18nService)); - } - } - - private async Task ValidateDependentPolicies(PolicyType type, Guid? orgId, bool enabled) - { - if (orgId == null) - { - throw new ArgumentNullException(nameof(orgId), "OrgId cannot be null"); - } - - switch(type) - { - case PolicyType.MasterPassword: - case PolicyType.PasswordGenerator: - case PolicyType.TwoFactorAuthentication: - case PolicyType.PersonalOwnership: - case PolicyType.DisableSend: - case PolicyType.SendOptions: - case PolicyType.ResetPassword: - break; - - case PolicyType.SingleOrg: - if (enabled) - { - break; - } - - var requireSso = - await _policyRepository.GetByOrganizationIdTypeAsync(orgId.Value, PolicyType.RequireSso); - if (requireSso?.Enabled == true) - { - ModelState.AddModelError(string.Empty, _i18nService.T("DisableRequireSsoError")); - } - break; - - case PolicyType.RequireSso: - if (!enabled) - { - break; - } - - var singleOrg = await _policyRepository.GetByOrganizationIdTypeAsync(orgId.Value, PolicyType.SingleOrg); - if (singleOrg?.Enabled != true) - { - ModelState.AddModelError(string.Empty, _i18nService.T("RequireSsoPolicyReqError")); - } - break; - - default: - throw new ArgumentOutOfRangeException(); - } - } - } -} diff --git a/bitwarden_license/src/Portal/EnterprisePortalCurrentContext.cs b/bitwarden_license/src/Portal/EnterprisePortalCurrentContext.cs index 892471eea..76d1bd51e 100644 --- a/bitwarden_license/src/Portal/EnterprisePortalCurrentContext.cs +++ b/bitwarden_license/src/Portal/EnterprisePortalCurrentContext.cs @@ -39,9 +39,6 @@ namespace Bit.Portal public bool OwnerForSelectedOrganization => SelectedOrganizationDetails?.Type == Core.Enums.OrganizationUserType.Owner; - public bool CanManagePoliciesForSelectedOrganization => - AdminForSelectedOrganization || SelectedOrganizationDetailsPermissions.ManagePolicies == true; - public bool CanManageSsoForSelectedOrganization => AdminForSelectedOrganization || SelectedOrganizationDetailsPermissions.ManageSso == true; diff --git a/bitwarden_license/src/Portal/Models/MasterPasswordDataModel.cs b/bitwarden_license/src/Portal/Models/MasterPasswordDataModel.cs deleted file mode 100644 index cea45af42..000000000 --- a/bitwarden_license/src/Portal/Models/MasterPasswordDataModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Bit.Portal.Models -{ - public class MasterPasswordDataModel - { - [Display(Name = "MinimumLength")] - [Range(8, int.MaxValue, ErrorMessage = "MasterPasswordMinLengthError")] - public int? MinLength { get; set; } - [Display(Name = "MinimumComplexityScore")] - public int? MinComplexity { get; set; } - [Display(Name = "UppercaseAZ")] - public bool RequireUpper { get; set; } - [Display(Name = "LowercaseAZ")] - public bool RequireLower { get; set; } - [Display(Name = "Numbers09")] - public bool RequireNumbers { get; set; } - [Display(Name = "SpecialCharacters")] - public bool RequireSpecial { get; set; } - } -} diff --git a/bitwarden_license/src/Portal/Models/PasswordGeneratorDataModel.cs b/bitwarden_license/src/Portal/Models/PasswordGeneratorDataModel.cs deleted file mode 100644 index d007a7b08..000000000 --- a/bitwarden_license/src/Portal/Models/PasswordGeneratorDataModel.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Bit.Portal.Models -{ - public class PasswordGeneratorDataModel - { - // Shared - [Display(Name = "MinimumLength")] - [Range(5, 128)] - public int? MinLength { get; set; } - [Display(Name = "DefaultType")] - public string DefaultType { get; set; } - // PG - Password - [Display(Name = "UppercaseAZ")] - public bool UseUpper { get; set; } - [Display(Name = "LowercaseAZ")] - public bool UseLower { get; set; } - [Display(Name = "Numbers09")] - public bool UseNumbers { get; set; } - [Display(Name = "SpecialCharacters")] - public bool UseSpecial { get; set; } - [Display(Name = "MinimumNumbers")] - [Range(0, 9)] - public int? MinNumbers { get; set; } - [Display(Name = "MinimumSpecial")] - [Range(0, 9)] - public int? MinSpecial { get; set; } - // PG - Passphrase - [Display(Name = "MinimumNumberOfWords")] - [Range(3, 20)] - public int? MinNumberWords { get; set; } - [Display(Name = "Capitalize")] - public bool Capitalize { get; set; } - [Display(Name = "IncludeNumber")] - public bool IncludeNumber { get; set; } - } -} diff --git a/bitwarden_license/src/Portal/Models/PoliciesModel.cs b/bitwarden_license/src/Portal/Models/PoliciesModel.cs deleted file mode 100644 index 96f174851..000000000 --- a/bitwarden_license/src/Portal/Models/PoliciesModel.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Bit.Core.Enums; -using Bit.Core.Models.Data; -using Bit.Core.Models.Table; - -namespace Bit.Portal.Models -{ - public class PoliciesModel - { - public PoliciesModel(ICollection policies, OrganizationUserOrganizationDetails selectedOrgDetails) - { - if (policies == null) - { - return; - } - - var policyDict = policies?.ToDictionary(p => p.Type); - Policies = new List(); - - foreach (var type in Enum.GetValues(typeof(PolicyType)).Cast()) - { - if (type == PolicyType.RequireSso && !selectedOrgDetails.UseSso) - { - continue; - } - - if (type == PolicyType.ResetPassword && !selectedOrgDetails.UseResetPassword) - { - continue; - } - - var enabled = policyDict.ContainsKey(type) ? policyDict[type].Enabled : false; - Policies.Add(new PolicyModel(type, enabled)); - } - } - - public List Policies { get; set; } - } -} diff --git a/bitwarden_license/src/Portal/Models/PolicyEditModel.cs b/bitwarden_license/src/Portal/Models/PolicyEditModel.cs deleted file mode 100644 index 0d7ae81ec..000000000 --- a/bitwarden_license/src/Portal/Models/PolicyEditModel.cs +++ /dev/null @@ -1,135 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using Bit.Core.Enums; -using Bit.Core.Models.Table; -using Bit.Core.Models.Data; -using Bit.Core.Services; -using Microsoft.AspNetCore.Mvc.Rendering; - -namespace Bit.Portal.Models -{ - public class PolicyEditModel : PolicyModel - { - public PolicyEditModel() { } - - public PolicyEditModel(PolicyType type, II18nService i18nService) - : base(type, false) - { - // Inject service and create static lists - TranslateStrings(i18nService); - } - - public PolicyEditModel(Policy model, II18nService i18nService) - : base(model) - { - if (model == null) - { - return; - } - - // Inject service and create static lists - TranslateStrings(i18nService); - - if (model.Data != null) - { - var options = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true, - }; - - switch (model.Type) - { - case PolicyType.MasterPassword: - MasterPasswordDataModel = JsonSerializer.Deserialize(model.Data, options); - break; - case PolicyType.PasswordGenerator: - PasswordGeneratorDataModel = JsonSerializer.Deserialize(model.Data, options); - break; - case PolicyType.SendOptions: - SendOptionsDataModel = JsonSerializer.Deserialize(model.Data, options); - break; - case PolicyType.ResetPassword: - ResetPasswordDataModel = - JsonSerializer.Deserialize(model.Data, options); - break; - default: - throw new ArgumentOutOfRangeException(); - } - } - } - - public MasterPasswordDataModel MasterPasswordDataModel { get; set; } - public PasswordGeneratorDataModel PasswordGeneratorDataModel { get; set; } - public SendOptionsPolicyData SendOptionsDataModel { get; set; } - public ResetPasswordDataModel ResetPasswordDataModel { get; set; } - public List Complexities { get; set; } - public List DefaultTypes { get; set; } - public string EnableCheckboxText { get; set; } - - public Policy ToPolicy(PolicyType type, Guid organizationId) - { - return ToPolicy(new Policy - { - Type = type, - OrganizationId = organizationId - }); - } - - public Policy ToPolicy(Policy existingPolicy) - { - existingPolicy.Enabled = Enabled; - - var options = new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - }; - switch (existingPolicy.Type) - { - case PolicyType.MasterPassword: - existingPolicy.Data = JsonSerializer.Serialize(MasterPasswordDataModel, options); - break; - case PolicyType.PasswordGenerator: - existingPolicy.Data = JsonSerializer.Serialize(PasswordGeneratorDataModel, options); - break; - case PolicyType.SendOptions: - existingPolicy.Data = JsonSerializer.Serialize(SendOptionsDataModel, options); - break; - case PolicyType.ResetPassword: - existingPolicy.Data = JsonSerializer.Serialize(ResetPasswordDataModel, options); - break; - case PolicyType.SingleOrg: - case PolicyType.TwoFactorAuthentication: - case PolicyType.RequireSso: - case PolicyType.PersonalOwnership: - case PolicyType.DisableSend: - break; - default: - throw new ArgumentOutOfRangeException(); - } - - return existingPolicy; - } - - public void TranslateStrings(II18nService i18nService) - { - Complexities = new List - { - new SelectListItem { Value = null, Text = "--" + i18nService.T("Select") + "--"}, - new SelectListItem { Value = "0", Text = i18nService.T("Weak") + " (0)" }, - new SelectListItem { Value = "1", Text = i18nService.T("Weak") + " (1)" }, - new SelectListItem { Value = "2", Text = i18nService.T("Weak") + " (2)" }, - new SelectListItem { Value = "3", Text = i18nService.T("Good") + " (3)" }, - new SelectListItem { Value = "4", Text = i18nService.T("Strong") + " (4)" }, - }; - DefaultTypes = new List - { - new SelectListItem { Value = null, Text = i18nService.T("UserPreference") }, - new SelectListItem { Value = "password", Text = i18nService.T("Password") }, - new SelectListItem { Value = "passphrase", Text = i18nService.T("Passphrase") }, - }; - EnableCheckboxText = PolicyType == PolicyType.PersonalOwnership - ? i18nService.T("PersonalOwnershipCheckboxDesc") : i18nService.T("Enabled"); - } - } -} diff --git a/bitwarden_license/src/Portal/Models/PolicyModel.cs b/bitwarden_license/src/Portal/Models/PolicyModel.cs deleted file mode 100644 index 49c0bea45..000000000 --- a/bitwarden_license/src/Portal/Models/PolicyModel.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using Bit.Core.Enums; -using Bit.Core.Models.Table; - -namespace Bit.Portal.Models -{ - public class PolicyModel - { - public PolicyModel() { } - - public PolicyModel(Policy policy) - : this(policy.Type, policy.Enabled) - { } - - public PolicyModel(PolicyType policyType, bool enabled) - { - switch (policyType) - { - case PolicyType.TwoFactorAuthentication: - NameKey = "TwoStepLogin"; - DescriptionKey = "TwoStepLoginDescription"; - break; - case PolicyType.MasterPassword: - NameKey = "MasterPassword"; - DescriptionKey = "MasterPasswordDescription"; - break; - case PolicyType.PasswordGenerator: - NameKey = "PasswordGenerator"; - DescriptionKey = "PasswordGeneratorDescription"; - break; - case PolicyType.SingleOrg: - NameKey = "SingleOrganization"; - DescriptionKey = "SingleOrganizationDescription"; - break; - case PolicyType.RequireSso: - NameKey = "RequireSso"; - DescriptionKey = "RequireSsoDescription"; - break; - case PolicyType.PersonalOwnership: - NameKey = "PersonalOwnership"; - DescriptionKey = "PersonalOwnershipDescription"; - break; - case PolicyType.DisableSend: - NameKey = "DisableSend"; - DescriptionKey = "DisableSendDescription"; - break; - case PolicyType.SendOptions: - NameKey = "SendOptions"; - DescriptionKey = "SendOptionsDescription"; - break; - case PolicyType.ResetPassword: - NameKey = "ResetPassword"; - DescriptionKey = "ResetPasswordDescription"; - break; - default: - throw new ArgumentOutOfRangeException(); - } - - PolicyType = policyType; - Enabled = enabled; - } - - public string NameKey { get; set; } - public string DescriptionKey { get; set; } - public PolicyType PolicyType { get; set; } - public bool Enabled { get; set; } - } -} diff --git a/bitwarden_license/src/Portal/Views/Home/Index.cshtml b/bitwarden_license/src/Portal/Views/Home/Index.cshtml index 2d021f355..cc1d4bf63 100644 --- a/bitwarden_license/src/Portal/Views/Home/Index.cshtml +++ b/bitwarden_license/src/Portal/Views/Home/Index.cshtml @@ -18,14 +18,4 @@ } - - @if (EnterprisePortalCurrentContext.SelectedOrganizationDetails.UsePolicies && - EnterprisePortalCurrentContext.CanManagePoliciesForSelectedOrganization) - { - - } diff --git a/bitwarden_license/src/Portal/Views/Policies/Edit.cshtml b/bitwarden_license/src/Portal/Views/Policies/Edit.cshtml deleted file mode 100644 index d894d7165..000000000 --- a/bitwarden_license/src/Portal/Views/Policies/Edit.cshtml +++ /dev/null @@ -1,250 +0,0 @@ -@using Bit.Core.Enums -@model PolicyEditModel -@inject Bit.Core.Services.II18nService i18nService -@{ - ViewData["Title"] = i18nService.T("EditPolicy", i18nService.T(Model.NameKey)); -} - - -

@i18nService.T(Model.DescriptionKey)

- -
- @if (Model.PolicyType == PolicyType.TwoFactorAuthentication) - { - - } - - @if (Model.PolicyType == PolicyType.SingleOrg) - { - - } - - @if (Model.PolicyType == PolicyType.RequireSso) - { - - - } - - @if (Model.PolicyType == PolicyType.PersonalOwnership) - { - - } - - @if (Model.PolicyType == PolicyType.DisableSend) - { - - } - - @if (Model.PolicyType == PolicyType.SendOptions) - { - - } - - @if (Model.PolicyType == PolicyType.ResetPassword) - { - - } - -
- -
-
- - -
-
- - @if (Model.PolicyType == PolicyType.MasterPassword) - { -
-
- - -
-
- - - -
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- } - - @if (Model.PolicyType == PolicyType.PasswordGenerator) - { -
-
- - -
-
-
-

@i18nService.T("Password")

-
-
- - - -
-
-
-
- - - -
-
- - - -
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-
-

@i18nService.T("Passphrase")

-
-
- - - -
-
-
-
- - -
-
- - -
-
-
- } - - @if (Model.PolicyType == PolicyType.SendOptions) - { -
-

@i18nService.T("Options")

-
-
- - -
-
-
- } - - @if (Model.PolicyType == PolicyType.ResetPassword) - { -
-

@i18nService.T("ResetPasswordAutoEnroll")

-

@i18nService.T("ResetPasswordAutoEnrollDescription")

- -
-
- - -
-
-
- } - -
- - @i18nService.T("Cancel") -
-
diff --git a/bitwarden_license/src/Portal/Views/Policies/Index.cshtml b/bitwarden_license/src/Portal/Views/Policies/Index.cshtml deleted file mode 100644 index 90249d791..000000000 --- a/bitwarden_license/src/Portal/Views/Policies/Index.cshtml +++ /dev/null @@ -1,29 +0,0 @@ -@model PoliciesModel -@inject Bit.Core.Services.II18nService i18nService -@{ - ViewData["Title"] = i18nService.T("Policies"); -} - - - -
- - - @foreach (var policyModel in Model.Policies) - { - - - - } - -
- @i18nService.T(policyModel.NameKey) - @if (policyModel.Enabled) - { - @i18nService.T("Enabled") - } - @i18nService.T(policyModel.DescriptionKey) -
-
diff --git a/bitwarden_license/src/Portal/Views/Shared/_Layout.cshtml b/bitwarden_license/src/Portal/Views/Shared/_Layout.cshtml index 5d38511d8..a9833cc57 100644 --- a/bitwarden_license/src/Portal/Views/Shared/_Layout.cshtml +++ b/bitwarden_license/src/Portal/Views/Shared/_Layout.cshtml @@ -31,8 +31,7 @@ { }