From a890ee66129e5573ea1bdf1057efa3058c508fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Gon=C3=A7alves?= Date: Mon, 3 Oct 2022 16:51:22 +0100 Subject: [PATCH] [SG-666][SG-667] Email is not prefilled and username isn't generated automatically (#2109) * SG-666 SG-667 - Email is now prefilled for plus addressed email username type * Username is auto generated upon navigation * SG-666 - Fixed PR comments * Added missing property initialization --- .../Pages/Generator/GeneratorPageViewModel.cs | 51 +++++++++++++------ .../Pages/Vault/CipherAddEditPageViewModel.cs | 4 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/App/Pages/Generator/GeneratorPageViewModel.cs b/src/App/Pages/Generator/GeneratorPageViewModel.cs index d23e278ed..6e51ef0d4 100644 --- a/src/App/Pages/Generator/GeneratorPageViewModel.cs +++ b/src/App/Pages/Generator/GeneratorPageViewModel.cs @@ -20,6 +20,7 @@ namespace Bit.App.Pages private readonly IPlatformUtilsService _platformUtilsService; private readonly IClipboardService _clipboardService; private readonly IUsernameGenerationService _usernameGenerationService; + private readonly ITokenService _tokenService; readonly LazyResolve _logger = new LazyResolve("logger"); private PasswordGenerationOptions _options; @@ -49,8 +50,6 @@ namespace Bit.App.Pages private bool _showFirefoxRelayApiAccessToken; private bool _showAnonAddyApiAccessToken; private bool _showSimpleLoginApiKey; - private UsernameEmailType _catchAllEmailTypeSelected; - private UsernameEmailType _plusAddressedEmailTypeSelected; private bool _editMode; public GeneratorPageViewModel() @@ -59,6 +58,7 @@ namespace Bit.App.Pages _platformUtilsService = ServiceContainer.Resolve(); _clipboardService = ServiceContainer.Resolve(); _usernameGenerationService = ServiceContainer.Resolve(); + _tokenService = ServiceContainer.Resolve(); PageTitle = AppResources.Generator; GeneratorTypeOptions = new List { @@ -157,7 +157,7 @@ namespace Bit.App.Pages public bool ShowUsernameEmailType { - get => !string.IsNullOrWhiteSpace(EmailWebsite) || EditMode; + get => !string.IsNullOrWhiteSpace(EmailWebsite); } public int Length @@ -367,7 +367,7 @@ namespace Bit.App.Pages IsUsername = value == GeneratorType.Username; TriggerPropertyChanged(nameof(GeneratorTypeSelected)); SaveOptionsAsync().FireAndForget(); - SaveUsernameOptionsAsync(false).FireAndForget(); + SaveUsernameOptionsAsync().FireAndForget(); } } } @@ -382,6 +382,7 @@ namespace Bit.App.Pages IsPassword = value == 0; TriggerPropertyChanged(nameof(PasswordTypeSelectedIndex)); SaveOptionsAsync().FireAndForget(); + SaveUsernameOptionsAsync().FireAndForget(); } } } @@ -396,7 +397,7 @@ namespace Bit.App.Pages _usernameOptions.Type = value; Username = Constants.DefaultUsernameGenerated; TriggerPropertyChanged(nameof(UsernameTypeSelected), new string[] { nameof(UsernameTypeDescriptionLabel) }); - SaveUsernameOptionsAsync(false).FireAndForget(); + SaveUsernameOptionsAsync().FireAndForget(); } } } @@ -564,26 +565,28 @@ namespace Bit.App.Pages public UsernameEmailType PlusAddressedEmailTypeSelected { - get => _plusAddressedEmailTypeSelected; + get => _usernameOptions.PlusAddressedEmailType; set { - if (SetProperty(ref _plusAddressedEmailTypeSelected, value)) + if (_usernameOptions.PlusAddressedEmailType != value) { _usernameOptions.PlusAddressedEmailType = value; - SaveUsernameOptionsAsync(false).FireAndForget(); + TriggerPropertyChanged(nameof(PlusAddressedEmailTypeSelected)); + SaveUsernameOptionsAsync().FireAndForget(); } } } public UsernameEmailType CatchAllEmailTypeSelected { - get => _catchAllEmailTypeSelected; + get => _usernameOptions.CatchAllEmailType; set { - if (SetProperty(ref _catchAllEmailTypeSelected, value)) + if (_usernameOptions.CatchAllEmailType != value) { _usernameOptions.CatchAllEmailType = value; - SaveUsernameOptionsAsync(false).FireAndForget(); + TriggerPropertyChanged(nameof(CatchAllEmailTypeSelected)); + SaveUsernameOptionsAsync().FireAndForget(); } } } @@ -601,16 +604,28 @@ namespace Bit.App.Pages { (_options, EnforcedPolicyOptions) = await _passwordGenerationService.GetOptionsAsync(); LoadFromOptions(); - await RegenerateAsync(); _usernameOptions = await _usernameGenerationService.GetOptionsAsync(); + _usernameOptions.PlusAddressedEmail = _tokenService.GetEmail(); + _usernameOptions.EmailWebsite = EmailWebsite; + _usernameOptions.CatchAllEmailType = _usernameOptions.PlusAddressedEmailType = string.IsNullOrWhiteSpace(EmailWebsite) || !EditMode ? UsernameEmailType.Random : UsernameEmailType.Website; - if (!EditMode) + if (!IsUsername) { - _usernameOptions.CatchAllEmailType = _usernameOptions.PlusAddressedEmailType = UsernameEmailType.Random; + await RegenerateAsync(); + } + else + { + if (UsernameTypeSelected != UsernameType.ForwardedEmailAlias) + { + await RegenerateUsernameAsync(); + } + else + { + Username = Constants.DefaultUsernameGenerated; + } } TriggerUsernamePropertiesChanged(); - Username = Constants.DefaultUsernameGenerated; _doneIniting = true; } @@ -668,10 +683,14 @@ namespace Bit.App.Pages await _usernameGenerationService.SaveOptionsAsync(_usernameOptions); - if (regenerate) + if (regenerate && UsernameTypeSelected != UsernameType.ForwardedEmailAlias) { await RegenerateUsernameAsync(); } + else + { + Username = Constants.DefaultUsernameGenerated; + } } public async Task SliderChangedAsync() diff --git a/src/App/Pages/Vault/CipherAddEditPageViewModel.cs b/src/App/Pages/Vault/CipherAddEditPageViewModel.cs index 78e8eb0a5..7be04f536 100644 --- a/src/App/Pages/Vault/CipherAddEditPageViewModel.cs +++ b/src/App/Pages/Vault/CipherAddEditPageViewModel.cs @@ -605,6 +605,8 @@ namespace Bit.App.Pages return; } + var website = Cipher?.Login?.Uris?.FirstOrDefault()?.Host; + var page = new GeneratorPage(false, async (username) => { try @@ -617,7 +619,7 @@ namespace Bit.App.Pages { OnGenerateUsernameException(ex); } - }, isUsernameGenerator: true, emailWebsite: Cipher?.Name, editMode: true); + }, isUsernameGenerator: true, emailWebsite: website, editMode: true); await Page.Navigation.PushModalAsync(new NavigationPage(page)); }