MOBILE-4245 Dev: Create new setting to control staging sites

main
Alfonso Salces 2023-02-03 09:45:50 +01:00
parent 101294349e
commit 3e0e29ba2e
7 changed files with 119 additions and 11 deletions

View File

@ -86,6 +86,7 @@
}, },
"customurlscheme": "moodlemobile", "customurlscheme": "moodlemobile",
"sites": [], "sites": [],
"stagingsites": [],
"multisitesdisplay": "", "multisitesdisplay": "",
"sitefindersettings": {}, "sitefindersettings": {},
"onlyallowlistedsites": false, "onlyallowlistedsites": false,

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // 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 { FormBuilder, FormGroup, ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms';
import { CoreApp } from '@services/app'; 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 { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config';
import { CoreLoginError } from '@classes/errors/loginerror'; import { CoreLoginError } from '@classes/errors/loginerror';
import { CorePlatform } from '@services/platform'; import { CorePlatform } from '@services/platform';
import { CoreEventObserver, CoreEvents } from '@singletons/events';
/** /**
* Site (url) chooser when adding a new site. * Site (url) chooser when adding a new site.
@ -55,11 +56,11 @@ import { CorePlatform } from '@services/platform';
templateUrl: 'site.html', templateUrl: 'site.html',
styleUrls: ['site.scss', '../../login.scss'], styleUrls: ['site.scss', '../../login.scss'],
}) })
export class CoreLoginSitePage implements OnInit { export class CoreLoginSitePage implements OnInit, OnDestroy {
@ViewChild('siteFormEl') formElement?: ElementRef; @ViewChild('siteFormEl') formElement?: ElementRef;
siteForm: FormGroup; siteForm!: FormGroup;
fixedSites?: CoreLoginSiteInfoExtended[]; fixedSites?: CoreLoginSiteInfoExtended[];
filteredSites?: CoreLoginSiteInfoExtended[]; filteredSites?: CoreLoginSiteInfoExtended[];
siteSelector: CoreLoginSiteSelectorListMethod = 'sitefinder'; siteSelector: CoreLoginSiteSelectorListMethod = 'sitefinder';
@ -68,14 +69,13 @@ export class CoreLoginSitePage implements OnInit {
sites: CoreLoginSiteInfoExtended[] = []; sites: CoreLoginSiteInfoExtended[] = [];
hasSites = false; hasSites = false;
loadingSites = false; loadingSites = false;
searchFunction: (search: string) => void; searchFunction!: (search: string) => void;
showScanQR: boolean; showScanQR!: boolean;
enteredSiteUrl?: CoreLoginSiteInfoExtended; enteredSiteUrl?: CoreLoginSiteInfoExtended;
siteFinderSettings: CoreLoginSiteFinderSettings; siteFinderSettings!: CoreLoginSiteFinderSettings;
stagingSitesChangeListener: CoreEventObserver;
constructor( constructor(protected formBuilder: FormBuilder) {
protected formBuilder: FormBuilder,
) {
let url = ''; let url = '';
this.siteSelector = CoreConstants.CONFIG.multisitesdisplay; this.siteSelector = CoreConstants.CONFIG.multisitesdisplay;
@ -122,13 +122,36 @@ export class CoreLoginSitePage implements OnInit {
this.loadingSites = false; this.loadingSites = false;
}, 1000); }, 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. * Initialize the component.
*/ */
ngOnInit(): void { async ngOnInit(): Promise<void> {
this.showKeyboard = !!CoreNavigator.getRouteBooleanParam('showKeyboard'); this.showKeyboard = !!CoreNavigator.getRouteBooleanParam('showKeyboard');
await this.loadStagingSites();
} }
/** /**
@ -150,6 +173,35 @@ export class CoreLoginSitePage implements OnInit {
return this.fixedSites[0].url; 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. * Initialize and show onboarding if needed.
* *
@ -612,6 +664,13 @@ export class CoreLoginSitePage implements OnInit {
CoreNavigator.navigate('/settings'); CoreNavigator.navigate('/settings');
} }
/**
* @inheritdoc
*/
ngOnDestroy(): void {
this.stagingSitesChangeListener.off();
}
} }
/** /**

View File

@ -40,6 +40,7 @@ import { CorePath } from '@singletons/path';
import { CorePromisedValue } from '@classes/promised-value'; import { CorePromisedValue } from '@classes/promised-value';
import { SafeHtml } from '@angular/platform-browser'; import { SafeHtml } from '@angular/platform-browser';
import { CoreLoginError } from '@classes/errors/loginerror'; import { CoreLoginError } from '@classes/errors/loginerror';
import { CoreSettingsHelper } from '@features/settings/services/settings-helper';
const PASSWORD_RESETS_CONFIG_KEY = 'password-resets'; 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; 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. * Get the valid identity providers from a site config.
* *

View File

@ -30,6 +30,12 @@
</ion-label> </ion-label>
<ion-toggle [(ngModel)]="forceSafeAreaMargins" (ionChange)="safeAreaChanged()"></ion-toggle> <ion-toggle [(ngModel)]="forceSafeAreaMargins" (ionChange)="safeAreaChanged()"></ion-toggle>
</ion-item> </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"> <ng-container *ngIf="siteId">
<ion-item class="ion-text-wrap"> <ion-item class="ion-text-wrap">
<ion-label> <ion-label>

View File

@ -12,13 +12,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { CoreConstants } from '@/core/constants';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { CoreLoginHelperProvider } from '@features/login/services/login-helper'; import { CoreLoginHelperProvider } from '@features/login/services/login-helper';
import { CoreSettingsHelper } from '@features/settings/services/settings-helper';
import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins';
import { CoreUserTours } from '@features/usertours/services/user-tours'; import { CoreUserTours } from '@features/usertours/services/user-tours';
import { CoreConfig } from '@services/config'; import { CoreConfig } from '@services/config';
import { CorePlatform } from '@services/platform'; import { CorePlatform } from '@services/platform';
import { CoreSites } from '@services/sites'; import { CoreLoginSiteInfo, CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
@ -41,6 +43,8 @@ export class CoreSettingsDevPage implements OnInit {
pluginStylesCount = 0; pluginStylesCount = 0;
sitePlugins: CoreSitePluginsBasicInfo[] = []; sitePlugins: CoreSitePluginsBasicInfo[] = [];
userToursEnabled = true; userToursEnabled = true;
stagingSites: CoreLoginSiteInfo[] = [];
enableStagingSites = false;
disabledFeatures: string[] = []; disabledFeatures: string[] = [];
@ -55,6 +59,12 @@ export class CoreSettingsDevPage implements OnInit {
this.siteId = CoreSites.getCurrentSite()?.getId(); this.siteId = CoreSites.getCurrentSite()?.getId();
this.stagingSites = CoreConstants.CONFIG.stagingsites;
if (this.stagingSites.length) {
this.enableStagingSites = await CoreSettingsHelper.hasEnabledStagingSites();
}
if (!this.siteId) { if (!this.siteId) {
return; return;
} }
@ -157,6 +167,10 @@ export class CoreSettingsDevPage implements OnInit {
CoreDomUtils.showToast('User tours have been reseted'); CoreDomUtils.showToast('User tours have been reseted');
} }
async setEnabledStagingSites(): Promise<void> {
await CoreSettingsHelper.setEnabledStagingSites(this.enableStagingSites);
}
} }
// Basic site plugin info. // Basic site plugin info.

View File

@ -477,6 +477,17 @@ export class CoreSettingsHelperProvider {
return this.darkModeObservable; 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); export const CoreSettingsHelper = makeSingleton(CoreSettingsHelperProvider);

View File

@ -43,6 +43,7 @@ export interface EnvironmentConfig {
defaultZoomLevel?: CoreZoomLevel; // Set the default zoom level of the app. defaultZoomLevel?: CoreZoomLevel; // Set the default zoom level of the app.
customurlscheme: string; customurlscheme: string;
sites: CoreLoginSiteInfo[]; sites: CoreLoginSiteInfo[];
stagingsites: CoreLoginSiteInfo[];
multisitesdisplay: CoreLoginSiteSelectorListMethod; multisitesdisplay: CoreLoginSiteSelectorListMethod;
sitefindersettings: Partial<CoreLoginSiteFinderSettings>; sitefindersettings: Partial<CoreLoginSiteFinderSettings>;
onlyallowlistedsites: boolean; onlyallowlistedsites: boolean;