From 006f9018a9a3b4ec591ec390788a666fd8021668 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 22 Nov 2023 08:59:17 +0100 Subject: [PATCH] MOBILE-4469 login: Move login constants to new file --- .../login/components/site-help/site-help.ts | 6 +-- .../site-onboarding/site-onboarding.ts | 4 +- src/core/features/login/constants.ts | 23 +++++++++ .../login/pages/credentials/credentials.ts | 8 ++-- .../login/pages/email-signup/email-signup.ts | 7 +-- .../login/pages/reconnect/reconnect.ts | 5 +- src/core/features/login/pages/site/site.ts | 6 +-- .../features/login/services/login-helper.ts | 47 +++++++++---------- src/core/features/settings/pages/dev/dev.ts | 6 +-- src/testing/services/behat-runtime.ts | 4 +- 10 files changed, 67 insertions(+), 49 deletions(-) create mode 100644 src/core/features/login/constants.ts diff --git a/src/core/features/login/components/site-help/site-help.ts b/src/core/features/login/components/site-help/site-help.ts index 51629ee3b..ba1b6101d 100644 --- a/src/core/features/login/components/site-help/site-help.ts +++ b/src/core/features/login/components/site-help/site-help.ts @@ -16,7 +16,7 @@ import { AfterViewInit, Component, ElementRef, HostBinding, OnDestroy } from '@a import { CoreUtils } from '@services/utils/utils'; import { ModalController, Translate } from '@singletons'; -import { CoreLoginHelperProvider, GET_STARTED_URL } from '@features/login/services/login-helper'; +import { FAQ_QRCODE_IMAGE_HTML, FAQ_URL_IMAGE_HTML, GET_STARTED_URL } from '@features/login/constants'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreCancellablePromise } from '@classes/cancellable-promise'; @@ -39,8 +39,8 @@ export class CoreLoginSiteHelpComponent implements AfterViewInit, OnDestroy { constructor(protected el: ElementRef) { const getStartedTitle = Translate.instant('core.login.faqsetupsitelinktitle'); const canScanQR = CoreUtils.canScanQR(); - const urlImageHtml = CoreLoginHelperProvider.FAQ_URL_IMAGE_HTML; - const qrCodeImageHtml = CoreLoginHelperProvider.FAQ_QRCODE_IMAGE_HTML; + const urlImageHtml = FAQ_URL_IMAGE_HTML; + const qrCodeImageHtml = FAQ_QRCODE_IMAGE_HTML; const setupLinkHtml = `${GET_STARTED_URL}`; const questions: Array = [ { diff --git a/src/core/features/login/components/site-onboarding/site-onboarding.ts b/src/core/features/login/components/site-onboarding/site-onboarding.ts index b80736975..2b5896b60 100644 --- a/src/core/features/login/components/site-onboarding/site-onboarding.ts +++ b/src/core/features/login/components/site-onboarding/site-onboarding.ts @@ -16,7 +16,7 @@ import { Component } from '@angular/core'; import { CoreConfig } from '@services/config'; import { CoreUtils } from '@services/utils/utils'; -import { CoreLoginHelperProvider, GET_STARTED_URL } from '@features/login/services/login-helper'; +import { GET_STARTED_URL, ONBOARDING_DONE } from '@features/login/constants'; import { ModalController } from '@singletons'; /** @@ -88,7 +88,7 @@ export class CoreLoginSiteOnboardingComponent { * Saves the onboarding has finished. */ protected saveOnboardingDone(): void { - CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1); + CoreConfig.set(ONBOARDING_DONE, 1); } } diff --git a/src/core/features/login/constants.ts b/src/core/features/login/constants.ts new file mode 100644 index 000000000..0da8f8fe3 --- /dev/null +++ b/src/core/features/login/constants.ts @@ -0,0 +1,23 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +export const GET_STARTED_URL = 'https://moodle.com'; +export const ONBOARDING_DONE = 'onboarding_done'; +export const FAQ_QRCODE_INFO_DONE = 'qrcode_info_done'; +export const FAQ_URL_IMAGE_HTML = ''; +export const FAQ_QRCODE_IMAGE_HTML = ''; +export const EMAIL_SIGNUP_FEATURE_NAME = 'CoreLoginEmailSignup'; +export const FORGOTTEN_PASSWORD_FEATURE_NAME = 'NoDelegate_ForgottenPassword'; +export const IDENTITY_PROVIDERS_FEATURE_NAME = 'NoDelegate_IdentityProviders'; +export const IDENTITY_PROVIDER_FEATURE_NAME_PREFIX = 'NoDelegate_IdentityProvider_'; diff --git a/src/core/features/login/pages/credentials/credentials.ts b/src/core/features/login/pages/credentials/credentials.ts index 2461f4f23..35221ad9b 100644 --- a/src/core/features/login/pages/credentials/credentials.ts +++ b/src/core/features/login/pages/credentials/credentials.ts @@ -21,7 +21,7 @@ import { CoreApp } from '@services/app'; import { CoreNetwork } from '@services/network'; import { CoreSiteCheckResponse, CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; -import { CoreLoginHelper, CoreLoginHelperProvider } from '@features/login/services/login-helper'; +import { CoreLoginHelper } from '@features/login/services/login-helper'; import { Translate } from '@singletons'; import { CoreSitePublicConfigResponse, CoreUnauthenticatedSite } from '@classes/sites/unauthenticated-site'; import { CoreEvents } from '@singletons/events'; @@ -33,6 +33,7 @@ import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest import { SafeHtml } from '@angular/platform-browser'; import { CorePlatform } from '@services/platform'; import { CoreSitesFactory } from '@services/sites-factory'; +import { EMAIL_SIGNUP_FEATURE_NAME, FORGOTTEN_PASSWORD_FEATURE_NAME } from '@features/login/constants'; /** * Page to enter the user credentials. @@ -199,9 +200,8 @@ export class CoreLoginCredentialsPage implements OnInit, OnDestroy { this.showScanQR = await CoreLoginHelper.displayQRInCredentialsScreen(this.siteConfig.tool_mobile_qrcodetype); } - this.canSignup = this.siteConfig.registerauth == 'email' && - !this.site.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME); - this.showForgottenPassword = !this.site.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME); + this.canSignup = this.siteConfig.registerauth == 'email' && !this.site.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME); + this.showForgottenPassword = !this.site.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME); this.exceededAttemptsHTML = CoreLoginHelper.buildExceededAttemptsHTML( !!this.supportConfig?.canContactSupport(), this.showForgottenPassword, diff --git a/src/core/features/login/pages/email-signup/email-signup.ts b/src/core/features/login/pages/email-signup/email-signup.ts index 9b83a7988..8938b13eb 100644 --- a/src/core/features/login/pages/email-signup/email-signup.ts +++ b/src/core/features/login/pages/email-signup/email-signup.ts @@ -27,7 +27,6 @@ import { AuthEmailSignupProfileFieldsCategory, AuthEmailSignupSettings, CoreLoginHelper, - CoreLoginHelperProvider, } from '@features/login/services/login-helper'; import { CoreNavigator } from '@services/navigator'; import { CoreForms } from '@singletons/form'; @@ -35,6 +34,7 @@ import { CoreRecaptchaComponent } from '@components/recaptcha/recaptcha'; import { CorePath } from '@singletons/path'; import { CoreDom } from '@singletons/dom'; import { CoreSitesFactory } from '@services/sites-factory'; +import { EMAIL_SIGNUP_FEATURE_NAME } from '@features/login/constants'; /** * Page to signup using email. @@ -238,10 +238,7 @@ export class CoreLoginEmailSignupPage implements OnInit { * @returns True if success. */ protected async treatSiteConfig(): Promise { - if ( - this.siteConfig?.registerauth == 'email' && - !this.site.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME) - ) { + if (this.siteConfig?.registerauth == 'email' && !this.site.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME)) { this.siteName = await this.site.getSiteName(); this.authInstructions = this.siteConfig.authinstructions; diff --git a/src/core/features/login/pages/reconnect/reconnect.ts b/src/core/features/login/pages/reconnect/reconnect.ts index 966605f57..999e63786 100644 --- a/src/core/features/login/pages/reconnect/reconnect.ts +++ b/src/core/features/login/pages/reconnect/reconnect.ts @@ -20,7 +20,7 @@ import { CoreNetwork } from '@services/network'; import { CoreSiteBasicInfo, CoreSites, CoreSitesReadingStrategy } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreUtils } from '@services/utils/utils'; -import { CoreLoginHelper, CoreLoginHelperProvider } from '@features/login/services/login-helper'; +import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreSite } from '@classes/sites/site'; import { CoreEvents } from '@singletons/events'; import { CoreError } from '@classes/errors/error'; @@ -32,6 +32,7 @@ import { CoreUserAuthenticatedSupportConfig } from '@features/user/classes/suppo import { Translate } from '@singletons'; import { SafeHtml } from '@angular/platform-browser'; import { CoreSitePublicConfigResponse } from '@classes/sites/unauthenticated-site'; +import { FORGOTTEN_PASSWORD_FEATURE_NAME } from '@features/login/constants'; /** * Page to enter the user password to reconnect to a site. @@ -171,7 +172,7 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { return; } - this.showForgottenPassword = !this.site.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME); + this.showForgottenPassword = !this.site.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME); this.exceededAttemptsHTML = CoreLoginHelper.buildExceededAttemptsHTML( !!this.supportConfig?.canContactSupport(), this.showForgottenPassword, diff --git a/src/core/features/login/pages/site/site.ts b/src/core/features/login/pages/site/site.ts index d0dc4ee29..b85b1c865 100644 --- a/src/core/features/login/pages/site/site.ts +++ b/src/core/features/login/pages/site/site.ts @@ -23,7 +23,6 @@ import { CoreUtils } from '@services/utils/utils'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreLoginHelper, - CoreLoginHelperProvider, CoreLoginSiteFinderSettings, CoreLoginSiteSelectorListMethod, } from '@features/login/services/login-helper'; @@ -48,6 +47,7 @@ import { CoreLoginError } from '@classes/errors/loginerror'; import { CorePlatform } from '@services/platform'; import { CoreReferrer } from '@services/referrer'; import { CoreSitesFactory } from '@services/sites-factory'; +import { ONBOARDING_DONE } from '@features/login/constants'; /** * Site (url) chooser when adding a new site. @@ -110,7 +110,7 @@ export class CoreLoginSitePage implements OnInit { if (showOnboarding) { // Don't display onboarding in this case, and don't display it again later. - CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1); + CoreConfig.set(ONBOARDING_DONE, 1); } } else if (showOnboarding) { this.initOnboarding(); @@ -191,7 +191,7 @@ export class CoreLoginSitePage implements OnInit { * @returns Promise resolved when done. */ protected async initOnboarding(): Promise { - const onboardingDone = await CoreConfig.get(CoreLoginHelperProvider.ONBOARDING_DONE, false); + const onboardingDone = await CoreConfig.get(ONBOARDING_DONE, false); if (!onboardingDone) { // Check onboarding. diff --git a/src/core/features/login/services/login-helper.ts b/src/core/features/login/services/login-helper.ts index ac514840c..6918b9fdb 100644 --- a/src/core/features/login/services/login-helper.ts +++ b/src/core/features/login/services/login-helper.ts @@ -48,10 +48,14 @@ import { CoreUnauthenticatedSite, TypeOfLogin, } from '@classes/sites/unauthenticated-site'; - -const PASSWORD_RESETS_CONFIG_KEY = 'password-resets'; - -export const GET_STARTED_URL = 'https://moodle.com'; +import { + EMAIL_SIGNUP_FEATURE_NAME, + FAQ_QRCODE_IMAGE_HTML, + FAQ_QRCODE_INFO_DONE, + FORGOTTEN_PASSWORD_FEATURE_NAME, + IDENTITY_PROVIDERS_FEATURE_NAME, + IDENTITY_PROVIDER_FEATURE_NAME_PREFIX, +} from '../constants'; /** * Helper provider that provides some common features regarding authentication. @@ -59,14 +63,7 @@ export const GET_STARTED_URL = 'https://moodle.com'; @Injectable({ providedIn: 'root' }) export class CoreLoginHelperProvider { - static readonly ONBOARDING_DONE = 'onboarding_done'; - static readonly FAQ_QRCODE_INFO_DONE = 'qrcode_info_done'; - static readonly FAQ_URL_IMAGE_HTML = ''; - static readonly FAQ_QRCODE_IMAGE_HTML = ''; - static readonly EMAIL_SIGNUP_FEATURE_NAME = 'CoreLoginEmailSignup'; - static readonly FORGOTTEN_PASSWORD_FEATURE_NAME = 'NoDelegate_ForgottenPassword'; - static readonly IDENTITY_PROVIDERS_FEATURE_NAME = 'NoDelegate_IdentityProviders'; - static readonly IDENTITY_PROVIDER_FEATURE_NAME_PREFIX = 'NoDelegate_IdentityProvider_'; + protected static readonly PASSWORD_RESETS_CONFIG_KEY = 'password-resets'; protected logger: CoreLogger; protected sessionExpiredCheckingSite: Record = {}; @@ -418,7 +415,7 @@ export class CoreLoginHelperProvider { return []; } // eslint-disable-next-line deprecation/deprecation - if (this.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDERS_FEATURE_NAME, siteConfig)) { + if (this.isFeatureDisabled(IDENTITY_PROVIDERS_FEATURE_NAME, siteConfig)) { // Identity providers are disabled, return an empty list. return []; } @@ -435,7 +432,7 @@ export class CoreLoginHelperProvider { provider.url && (provider.url.indexOf(httpsUrl) != -1 || provider.url.indexOf(httpUrl) != -1) && !this.isFeatureDisabled( // eslint-disable-line deprecation/deprecation - CoreLoginHelperProvider.IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id, + IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id, siteConfig, ) ) { @@ -459,7 +456,7 @@ export class CoreLoginHelperProvider { return []; } - if (site.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDERS_FEATURE_NAME)) { + if (site.isFeatureDisabled(IDENTITY_PROVIDERS_FEATURE_NAME)) { // Identity providers are disabled, return an empty list. return []; } @@ -473,7 +470,7 @@ export class CoreLoginHelperProvider { const urlParams = CoreUrlUtils.extractUrlParams(provider.url); if (provider.url && (provider.url.indexOf(httpsUrl) != -1 || provider.url.indexOf(httpUrl) != -1) && - !site.isFeatureDisabled(CoreLoginHelperProvider.IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id)) { + !site.isFeatureDisabled(IDENTITY_PROVIDER_FEATURE_NAME_PREFIX + urlParams.id)) { validProviders.push(provider); } }); @@ -571,7 +568,7 @@ export class CoreLoginHelperProvider { */ isEmailSignupDisabled(config?: CoreSitePublicConfigResponse): boolean { // eslint-disable-next-line deprecation/deprecation - return this.isFeatureDisabled(CoreLoginHelperProvider.EMAIL_SIGNUP_FEATURE_NAME, config); + return this.isFeatureDisabled(EMAIL_SIGNUP_FEATURE_NAME, config); } /** @@ -617,7 +614,7 @@ export class CoreLoginHelperProvider { */ isForgottenPasswordDisabled(config?: CoreSitePublicConfigResponse): boolean { // eslint-disable-next-line deprecation/deprecation - return this.isFeatureDisabled(CoreLoginHelperProvider.FORGOTTEN_PASSWORD_FEATURE_NAME, config); + return this.isFeatureDisabled(FORGOTTEN_PASSWORD_FEATURE_NAME, config); } /** @@ -1294,14 +1291,14 @@ export class CoreLoginHelperProvider { * @returns Promise resolved if the user accepts to scan QR. */ async showScanQRInstructions(): Promise { - const dontShowWarning = await CoreConfig.get(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE, 0); + const dontShowWarning = await CoreConfig.get(FAQ_QRCODE_INFO_DONE, 0); if (dontShowWarning) { return; } const message = Translate.instant( 'core.login.faqwhereisqrcodeanswer', - { $image: '
'+ CoreLoginHelperProvider.FAQ_QRCODE_IMAGE_HTML + '
' }, + { $image: '
'+ FAQ_QRCODE_IMAGE_HTML + '
' }, ); const header = Translate.instant('core.login.faqwhereisqrcode'); @@ -1315,7 +1312,7 @@ export class CoreLoginHelperProvider { ); if (dontShowAgain) { - CoreConfig.set(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE, 1); + CoreConfig.set(FAQ_QRCODE_INFO_DONE, 1); } } catch { // User canceled. @@ -1484,7 +1481,7 @@ export class CoreLoginHelperProvider { passwordResets[siteUrl] = Date.now(); - await CoreConfig.set(PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets)); + await CoreConfig.set(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets)); } /** @@ -1516,9 +1513,9 @@ export class CoreLoginHelperProvider { } if (Object.values(passwordResets).length === 0) { - await CoreConfig.delete(PASSWORD_RESETS_CONFIG_KEY); + await CoreConfig.delete(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY); } else { - await CoreConfig.set(PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets)); + await CoreConfig.set(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, JSON.stringify(passwordResets)); } } @@ -1562,7 +1559,7 @@ export class CoreLoginHelperProvider { * @returns Password resets. */ protected async getPasswordResets(): Promise> { - const passwordResetsJson = await CoreConfig.get(PASSWORD_RESETS_CONFIG_KEY, '{}'); + const passwordResetsJson = await CoreConfig.get(CoreLoginHelperProvider.PASSWORD_RESETS_CONFIG_KEY, '{}'); return CoreTextUtils.parseJSON>(passwordResetsJson, {}); } diff --git a/src/core/features/settings/pages/dev/dev.ts b/src/core/features/settings/pages/dev/dev.ts index 3a7072d9a..0877d74f0 100644 --- a/src/core/features/settings/pages/dev/dev.ts +++ b/src/core/features/settings/pages/dev/dev.ts @@ -14,7 +14,7 @@ import { CoreConstants } from '@/core/constants'; import { Component, OnInit } from '@angular/core'; -import { CoreLoginHelperProvider } from '@features/login/services/login-helper'; +import { FAQ_QRCODE_INFO_DONE, ONBOARDING_DONE } from '@features/login/constants'; import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { CoreUserTours } from '@features/usertours/services/user-tours'; @@ -174,8 +174,8 @@ export class CoreSettingsDevPage implements OnInit { async resetUserTours(): Promise { await CoreUserTours.resetTours(); - await CoreConfig.delete(CoreLoginHelperProvider.ONBOARDING_DONE); - await CoreConfig.delete(CoreLoginHelperProvider.FAQ_QRCODE_INFO_DONE); + await CoreConfig.delete(ONBOARDING_DONE); + await CoreConfig.delete(FAQ_QRCODE_INFO_DONE); CoreDomUtils.showToast('User tours have been reseted'); } diff --git a/src/testing/services/behat-runtime.ts b/src/testing/services/behat-runtime.ts index b5d73ce60..ffc67398e 100644 --- a/src/testing/services/behat-runtime.ts +++ b/src/testing/services/behat-runtime.ts @@ -15,7 +15,7 @@ import { TestingBehatDomUtils } from './behat-dom'; import { TestingBehatBlocking } from './behat-blocking'; import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes'; -import { CoreLoginHelperProvider } from '@features/login/services/login-helper'; +import { ONBOARDING_DONE } from '@features/login/constants'; import { CoreConfig } from '@services/config'; import { EnvironmentConfig } from '@/types/config'; import { LocalNotifications, makeSingleton, NgZone } from '@singletons'; @@ -77,7 +77,7 @@ export class TestingBehatRuntimeService { TestingBehatBlocking.init(); if (options.skipOnBoarding) { - CoreConfig.set(CoreLoginHelperProvider.ONBOARDING_DONE, 1); + CoreConfig.set(ONBOARDING_DONE, 1); } if (options.configOverrides) {