MOBILE-4245 Dev: Create new setting to control staging sites
This commit is contained in:
		
							parent
							
								
									101294349e
								
							
						
					
					
						commit
						3e0e29ba2e
					
				| @ -86,6 +86,7 @@ | ||||
|     }, | ||||
|     "customurlscheme": "moodlemobile", | ||||
|     "sites": [], | ||||
|     "stagingsites": [], | ||||
|     "multisitesdisplay": "", | ||||
|     "sitefindersettings": {}, | ||||
|     "onlyallowlistedsites": false, | ||||
|  | ||||
| @ -12,7 +12,7 @@ | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; | ||||
| import { Component, OnInit, ViewChild, ElementRef, OnDestroy } from '@angular/core'; | ||||
| import { FormBuilder, FormGroup, ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms'; | ||||
| 
 | ||||
| import { CoreApp } from '@services/app'; | ||||
| @ -46,6 +46,7 @@ import { CoreUserSupportConfig } from '@features/user/classes/support/support-co | ||||
| import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config'; | ||||
| import { CoreLoginError } from '@classes/errors/loginerror'; | ||||
| import { CorePlatform } from '@services/platform'; | ||||
| import { CoreEventObserver, CoreEvents } from '@singletons/events'; | ||||
| 
 | ||||
| /** | ||||
|  * Site (url) chooser when adding a new site. | ||||
| @ -55,11 +56,11 @@ import { CorePlatform } from '@services/platform'; | ||||
|     templateUrl: 'site.html', | ||||
|     styleUrls: ['site.scss', '../../login.scss'], | ||||
| }) | ||||
| export class CoreLoginSitePage implements OnInit { | ||||
| export class CoreLoginSitePage implements OnInit, OnDestroy { | ||||
| 
 | ||||
|     @ViewChild('siteFormEl') formElement?: ElementRef; | ||||
| 
 | ||||
|     siteForm: FormGroup; | ||||
|     siteForm!: FormGroup; | ||||
|     fixedSites?: CoreLoginSiteInfoExtended[]; | ||||
|     filteredSites?: CoreLoginSiteInfoExtended[]; | ||||
|     siteSelector: CoreLoginSiteSelectorListMethod = 'sitefinder'; | ||||
| @ -68,14 +69,13 @@ export class CoreLoginSitePage implements OnInit { | ||||
|     sites: CoreLoginSiteInfoExtended[] = []; | ||||
|     hasSites = false; | ||||
|     loadingSites = false; | ||||
|     searchFunction: (search: string) => void; | ||||
|     showScanQR: boolean; | ||||
|     searchFunction!: (search: string) => void; | ||||
|     showScanQR!: boolean; | ||||
|     enteredSiteUrl?: CoreLoginSiteInfoExtended; | ||||
|     siteFinderSettings: CoreLoginSiteFinderSettings; | ||||
|     siteFinderSettings!: CoreLoginSiteFinderSettings; | ||||
|     stagingSitesChangeListener: CoreEventObserver; | ||||
| 
 | ||||
|     constructor( | ||||
|         protected formBuilder: FormBuilder, | ||||
|     ) { | ||||
|     constructor(protected formBuilder: FormBuilder) { | ||||
| 
 | ||||
|         let url = ''; | ||||
|         this.siteSelector = CoreConstants.CONFIG.multisitesdisplay; | ||||
| @ -122,13 +122,36 @@ export class CoreLoginSitePage implements OnInit { | ||||
| 
 | ||||
|             this.loadingSites = false; | ||||
|         }, 1000); | ||||
| 
 | ||||
|         this.stagingSitesChangeListener = CoreEvents.on( | ||||
|             CoreEvents.STAGING_SITES_CHANGE, | ||||
|             async ({ enabled }: { enabled: boolean }) => { | ||||
|                 if (enabled) { | ||||
|                     await this.loadStagingSites(); | ||||
| 
 | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 if (CoreLoginHelper.hasSeveralFixedSites()) { | ||||
|                     this.siteForm = this.formBuilder.group({ | ||||
|                         siteUrl: [this.initSiteSelector(), this.moodleUrlValidator()], | ||||
|                     }); | ||||
| 
 | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 this.filteredSites = undefined; | ||||
|                 this.fixedSites = undefined; | ||||
|             }, | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Initialize the component. | ||||
|      */ | ||||
|     ngOnInit(): void { | ||||
|     async ngOnInit(): Promise<void> { | ||||
|         this.showKeyboard = !!CoreNavigator.getRouteBooleanParam('showKeyboard'); | ||||
|         await this.loadStagingSites(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -150,6 +173,35 @@ export class CoreLoginSitePage implements OnInit { | ||||
|         return this.fixedSites[0].url; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Load staging sites list if they are enabled. | ||||
|      */ | ||||
|     protected async loadStagingSites(): Promise<void> { | ||||
|         const stagingSites = await CoreLoginHelper.getStagingSites(); | ||||
| 
 | ||||
|         if (!stagingSites.length) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         const sites = this.extendCoreLoginSiteInfo(<CoreLoginSiteInfoExtended[]> stagingSites); | ||||
|         this.siteSelector = 'list'; | ||||
| 
 | ||||
|         if (!this.fixedSites) { | ||||
|             this.fixedSites = []; | ||||
|         } | ||||
| 
 | ||||
|         for (const site of sites) { | ||||
|             if (this.fixedSites.some(item => item.url === site.url)) { | ||||
|                 continue; | ||||
|             } | ||||
| 
 | ||||
|             this.fixedSites.push(site); | ||||
|         } | ||||
| 
 | ||||
|         this.siteFinderSettings.displayimage = false; | ||||
|         this.filteredSites = this.fixedSites; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Initialize and show onboarding if needed. | ||||
|      * | ||||
| @ -612,6 +664,13 @@ export class CoreLoginSitePage implements OnInit { | ||||
|         CoreNavigator.navigate('/settings'); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @inheritdoc | ||||
|      */ | ||||
|     ngOnDestroy(): void { | ||||
|         this.stagingSitesChangeListener.off(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  | ||||
| @ -40,6 +40,7 @@ import { CorePath } from '@singletons/path'; | ||||
| import { CorePromisedValue } from '@classes/promised-value'; | ||||
| import { SafeHtml } from '@angular/platform-browser'; | ||||
| import { CoreLoginError } from '@classes/errors/loginerror'; | ||||
| import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; | ||||
| 
 | ||||
| const PASSWORD_RESETS_CONFIG_KEY = 'password-resets'; | ||||
| 
 | ||||
| @ -385,6 +386,21 @@ export class CoreLoginHelperProvider { | ||||
|         return CoreLoginHelper.isUniqueFixedSite() ? CoreConstants.CONFIG.sites[0].url : CoreConstants.CONFIG.sites; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get staging sites. | ||||
|      * | ||||
|      * @returns Staging sites. | ||||
|      */ | ||||
|     async getStagingSites(): Promise<CoreLoginSiteInfo[]> { | ||||
|         const hasEnabledStagingSites = await CoreSettingsHelper.hasEnabledStagingSites(); | ||||
| 
 | ||||
|         if (!hasEnabledStagingSites) { | ||||
|             return []; | ||||
|         } | ||||
| 
 | ||||
|         return CoreConstants.CONFIG.stagingsites; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the valid identity providers from a site config. | ||||
|      * | ||||
|  | ||||
| @ -30,6 +30,12 @@ | ||||
|             </ion-label> | ||||
|             <ion-toggle [(ngModel)]="forceSafeAreaMargins" (ionChange)="safeAreaChanged()"></ion-toggle> | ||||
|         </ion-item> | ||||
|         <ion-item class="ion-text-wrap" *ngIf="stagingSites.length"> | ||||
|             <ion-label> | ||||
|                 <h2>Enable staging sites</h2> | ||||
|             </ion-label> | ||||
|             <ion-toggle [(ngModel)]="enableStagingSites" (ionChange)="setEnabledStagingSites()"></ion-toggle> | ||||
|         </ion-item> | ||||
|         <ng-container *ngIf="siteId"> | ||||
|             <ion-item class="ion-text-wrap"> | ||||
|                 <ion-label> | ||||
|  | ||||
| @ -12,13 +12,15 @@ | ||||
| // See the License for the specific language governing permissions and
 | ||||
| // limitations under the License.
 | ||||
| 
 | ||||
| import { CoreConstants } from '@/core/constants'; | ||||
| import { Component, OnInit } from '@angular/core'; | ||||
| import { CoreLoginHelperProvider } from '@features/login/services/login-helper'; | ||||
| import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; | ||||
| import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; | ||||
| import { CoreUserTours } from '@features/usertours/services/user-tours'; | ||||
| import { CoreConfig } from '@services/config'; | ||||
| import { CorePlatform } from '@services/platform'; | ||||
| import { CoreSites } from '@services/sites'; | ||||
| import { CoreLoginSiteInfo, CoreSites } from '@services/sites'; | ||||
| import { CoreDomUtils } from '@services/utils/dom'; | ||||
| import { CoreUtils } from '@services/utils/utils'; | ||||
| 
 | ||||
| @ -41,6 +43,8 @@ export class CoreSettingsDevPage implements OnInit { | ||||
|     pluginStylesCount = 0; | ||||
|     sitePlugins: CoreSitePluginsBasicInfo[] = []; | ||||
|     userToursEnabled = true; | ||||
|     stagingSites: CoreLoginSiteInfo[] = []; | ||||
|     enableStagingSites = false; | ||||
| 
 | ||||
|     disabledFeatures: string[] = []; | ||||
| 
 | ||||
| @ -55,6 +59,12 @@ export class CoreSettingsDevPage implements OnInit { | ||||
| 
 | ||||
|         this.siteId = CoreSites.getCurrentSite()?.getId(); | ||||
| 
 | ||||
|         this.stagingSites = CoreConstants.CONFIG.stagingsites; | ||||
| 
 | ||||
|         if (this.stagingSites.length) { | ||||
|             this.enableStagingSites = await CoreSettingsHelper.hasEnabledStagingSites(); | ||||
|         } | ||||
| 
 | ||||
|         if (!this.siteId) { | ||||
|             return; | ||||
|         } | ||||
| @ -157,6 +167,10 @@ export class CoreSettingsDevPage implements OnInit { | ||||
|         CoreDomUtils.showToast('User tours have been reseted'); | ||||
|     } | ||||
| 
 | ||||
|     async setEnabledStagingSites(): Promise<void> { | ||||
|         await CoreSettingsHelper.setEnabledStagingSites(this.enableStagingSites); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // Basic site plugin info.
 | ||||
|  | ||||
| @ -477,6 +477,17 @@ export class CoreSettingsHelperProvider { | ||||
|         return this.darkModeObservable; | ||||
|     } | ||||
| 
 | ||||
|     async hasEnabledStagingSites(): Promise<boolean> { | ||||
|         const staging = await CoreConfig.get<string>('staging_sites', 'false'); | ||||
| 
 | ||||
|         return CoreUtils.isTrueOrOne(staging); | ||||
|     } | ||||
| 
 | ||||
|     async setEnabledStagingSites(enabled: boolean): Promise<void> { | ||||
|         await CoreConfig.set('staging_sites', `${enabled}`); | ||||
|         CoreEvents.trigger(CoreEvents.STAGING_SITES_CHANGE, { enabled }); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| export const CoreSettingsHelper = makeSingleton(CoreSettingsHelperProvider); | ||||
|  | ||||
							
								
								
									
										1
									
								
								src/types/config.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/types/config.d.ts
									
									
									
									
										vendored
									
									
								
							| @ -43,6 +43,7 @@ export interface EnvironmentConfig { | ||||
|     defaultZoomLevel?: CoreZoomLevel; // Set the default zoom level of the app.
 | ||||
|     customurlscheme: string; | ||||
|     sites: CoreLoginSiteInfo[]; | ||||
|     stagingsites: CoreLoginSiteInfo[]; | ||||
|     multisitesdisplay: CoreLoginSiteSelectorListMethod; | ||||
|     sitefindersettings: Partial<CoreLoginSiteFinderSettings>; | ||||
|     onlyallowlistedsites: boolean; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user