2017-11-29 16:03:47 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
import { Component, ViewChild } from '@angular/core';
|
|
|
|
import { IonicPage, NavController, NavParams, Content } from 'ionic-angular';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
|
|
|
import { CoreWSProvider } from '@providers/ws';
|
2017-11-29 16:03:47 +01:00
|
|
|
import { CoreLoginHelperProvider } from '../../providers/helper';
|
2018-05-16 14:43:25 +02:00
|
|
|
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
|
2018-03-12 15:44:41 +01:00
|
|
|
import { CoreUserProfileFieldDelegate } from '@core/user/providers/user-profile-field-delegate';
|
2017-11-29 16:03:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Page to signup using email.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
@IonicPage({ segment: 'core-login-email-signup' })
|
2017-11-29 16:03:47 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'page-core-login-email-signup',
|
|
|
|
templateUrl: 'email-signup.html',
|
|
|
|
})
|
|
|
|
export class CoreLoginEmailSignupPage {
|
|
|
|
@ViewChild(Content) content: Content;
|
2018-05-16 14:43:25 +02:00
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
signupForm: FormGroup;
|
|
|
|
siteUrl: string;
|
|
|
|
siteConfig: any;
|
|
|
|
siteName: string;
|
|
|
|
authInstructions: string;
|
|
|
|
settings: any;
|
|
|
|
countries: any;
|
|
|
|
countriesKeys: any[];
|
|
|
|
categories: any[];
|
2018-01-29 10:05:20 +01:00
|
|
|
settingsLoaded = false;
|
2018-04-06 14:37:49 +02:00
|
|
|
captcha = {
|
|
|
|
recaptcharesponse: ''
|
|
|
|
};
|
2017-11-29 16:03:47 +01:00
|
|
|
|
2018-05-16 14:43:25 +02:00
|
|
|
// Data for age verification.
|
|
|
|
ageVerificationForm: FormGroup;
|
|
|
|
countryControl: FormControl;
|
|
|
|
isMinor = false; // Whether the user is minor age.
|
|
|
|
ageDigitalConsentVerification: boolean; // Whether the age verification is enabled.
|
|
|
|
supportName: string;
|
|
|
|
supportEmail: string;
|
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
// Validation errors.
|
|
|
|
usernameErrors: any;
|
|
|
|
passwordErrors: any;
|
|
|
|
emailErrors: any;
|
|
|
|
email2Errors: any;
|
|
|
|
policyErrors: any;
|
|
|
|
namefieldsErrors: any;
|
|
|
|
|
|
|
|
constructor(private navCtrl: NavController, navParams: NavParams, private fb: FormBuilder, private wsProvider: CoreWSProvider,
|
|
|
|
private sitesProvider: CoreSitesProvider, private loginHelper: CoreLoginHelperProvider,
|
|
|
|
private domUtils: CoreDomUtilsProvider, private translate: TranslateService, private utils: CoreUtilsProvider,
|
2018-01-29 10:05:20 +01:00
|
|
|
private textUtils: CoreTextUtilsProvider, private userProfileFieldDelegate: CoreUserProfileFieldDelegate) {
|
2017-11-29 16:03:47 +01:00
|
|
|
|
|
|
|
this.siteUrl = navParams.get('siteUrl');
|
|
|
|
|
2018-05-16 14:43:25 +02:00
|
|
|
// Create the ageVerificationForm.
|
|
|
|
this.ageVerificationForm = this.fb.group({
|
|
|
|
age: ['', Validators.required]
|
|
|
|
});
|
|
|
|
this.countryControl = this.fb.control('', Validators.required);
|
|
|
|
this.ageVerificationForm.addControl('country', this.countryControl);
|
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
// Create the signupForm with the basic controls. More controls will be added later.
|
|
|
|
this.signupForm = this.fb.group({
|
2018-01-29 10:05:20 +01:00
|
|
|
username: ['', Validators.required],
|
|
|
|
password: ['', Validators.required],
|
|
|
|
email: ['', Validators.compose([Validators.required, Validators.email])],
|
|
|
|
email2: ['', Validators.compose([Validators.required, Validators.email])]
|
2017-11-29 16:03:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Setup validation errors.
|
2017-12-08 15:53:27 +01:00
|
|
|
this.usernameErrors = this.loginHelper.getErrorMessages('core.login.usernamerequired');
|
|
|
|
this.passwordErrors = this.loginHelper.getErrorMessages('core.login.passwordrequired');
|
|
|
|
this.emailErrors = this.loginHelper.getErrorMessages('core.login.missingemail');
|
2017-12-18 10:22:04 +01:00
|
|
|
this.email2Errors = this.loginHelper.getErrorMessages('core.login.missingemail', undefined, 'core.login.emailnotmatch');
|
2017-12-08 15:53:27 +01:00
|
|
|
this.policyErrors = this.loginHelper.getErrorMessages('core.login.policyagree');
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View loaded.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
ionViewDidLoad(): void {
|
2017-11-29 16:03:47 +01:00
|
|
|
// Fetch the data.
|
|
|
|
this.fetchData().finally(() => {
|
|
|
|
this.settingsLoaded = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complete the FormGroup using the settings received from server.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
protected completeFormGroup(): void {
|
2017-11-29 16:03:47 +01:00
|
|
|
this.signupForm.addControl('city', this.fb.control(this.settings.defaultcity || ''));
|
|
|
|
this.signupForm.addControl('country', this.fb.control(this.settings.country || ''));
|
|
|
|
|
|
|
|
// Add the name fields.
|
2018-01-29 10:05:20 +01:00
|
|
|
for (const i in this.settings.namefields) {
|
2017-11-29 16:03:47 +01:00
|
|
|
this.signupForm.addControl(this.settings.namefields[i], this.fb.control('', Validators.required));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.settings.sitepolicy) {
|
|
|
|
this.signupForm.addControl('policyagreed', this.fb.control(false, Validators.requiredTrue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the required data from the server-
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
protected fetchData(): Promise<any> {
|
2017-11-29 16:03:47 +01:00
|
|
|
// Get site config.
|
|
|
|
return this.sitesProvider.getSitePublicConfig(this.siteUrl).then((config) => {
|
|
|
|
this.siteConfig = config;
|
|
|
|
|
|
|
|
if (this.treatSiteConfig(config)) {
|
2018-05-16 14:43:25 +02:00
|
|
|
// Check content verification.
|
|
|
|
if (typeof this.ageDigitalConsentVerification == 'undefined') {
|
|
|
|
return this.wsProvider.callAjax('core_auth_is_age_digital_consent_verification_enabled', {},
|
|
|
|
{siteUrl: this.siteUrl }).then((result) => {
|
|
|
|
|
|
|
|
this.ageDigitalConsentVerification = result.status;
|
|
|
|
}).catch((e) => {
|
|
|
|
// Capture exceptions, fail silently.
|
|
|
|
}).then(() => {
|
|
|
|
return this.getSignupSettings();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return this.getSignupSettings();
|
|
|
|
}
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
this.completeFormGroup();
|
|
|
|
}).catch((err) => {
|
|
|
|
this.domUtils.showErrorModal(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get signup settings from server.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
protected getSignupSettings(): Promise<any> {
|
|
|
|
return this.wsProvider.callAjax('auth_email_get_signup_settings', {}, { siteUrl: this.siteUrl }).then((settings) => {
|
2017-11-29 16:03:47 +01:00
|
|
|
this.settings = settings;
|
|
|
|
this.categories = this.loginHelper.formatProfileFieldsForSignup(settings.profilefields);
|
|
|
|
|
2018-04-06 14:37:49 +02:00
|
|
|
if (this.settings.recaptchapublickey) {
|
|
|
|
this.captcha.recaptcharesponse = ''; // Reset captcha.
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:43:25 +02:00
|
|
|
if (!this.countryControl.value) {
|
|
|
|
this.countryControl.setValue(settings.country || '');
|
|
|
|
}
|
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
this.namefieldsErrors = {};
|
|
|
|
if (settings.namefields) {
|
|
|
|
settings.namefields.forEach((field) => {
|
2017-12-08 15:53:27 +01:00
|
|
|
this.namefieldsErrors[field] = this.loginHelper.getErrorMessages('core.login.missing' + field);
|
2017-11-29 16:03:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.utils.getCountryList().then((countries) => {
|
|
|
|
this.countries = countries;
|
|
|
|
this.countriesKeys = Object.keys(countries);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat the site config, checking if it's valid and extracting the data we're interested in.
|
|
|
|
*
|
|
|
|
* @param {any} siteConfig Site config to treat.
|
2018-01-29 10:05:20 +01:00
|
|
|
* @return {boolean} True if success.
|
2017-11-29 16:03:47 +01:00
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
protected treatSiteConfig(siteConfig: any): boolean {
|
2017-11-29 16:03:47 +01:00
|
|
|
if (siteConfig && siteConfig.registerauth == 'email' && !this.loginHelper.isEmailSignupDisabled(siteConfig)) {
|
|
|
|
this.siteName = siteConfig.sitename;
|
|
|
|
this.authInstructions = siteConfig.authinstructions;
|
2018-05-16 14:43:25 +02:00
|
|
|
this.ageDigitalConsentVerification = siteConfig.agedigitalconsentverification;
|
|
|
|
this.supportName = siteConfig.supportname;
|
|
|
|
this.supportEmail = siteConfig.supportemail;
|
|
|
|
this.countryControl.setValue(siteConfig.country || '');
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
this.domUtils.showErrorModal(
|
2018-01-29 10:05:20 +01:00
|
|
|
this.translate.instant('core.login.signupplugindisabled', { $a: this.translate.instant('core.login.auth_email') }));
|
2017-11-29 16:03:47 +01:00
|
|
|
this.navCtrl.pop();
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2017-11-29 16:03:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pull to refresh.
|
|
|
|
*
|
|
|
|
* @param {any} refresher Refresher.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
refreshSettings(refresher: any): void {
|
2017-11-29 16:03:47 +01:00
|
|
|
this.fetchData().finally(() => {
|
|
|
|
refresher.complete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create account.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
create(): void {
|
2018-04-06 14:37:49 +02:00
|
|
|
if (!this.signupForm.valid || (this.settings.recaptchapublickey && !this.captcha.recaptcharesponse)) {
|
2017-11-29 16:03:47 +01:00
|
|
|
// Form not valid. Scroll to the first element with errors.
|
2018-01-31 11:37:42 +01:00
|
|
|
if (!this.domUtils.scrollToInputError(this.content)) {
|
2017-11-29 16:03:47 +01:00
|
|
|
// Input not found, show an error modal.
|
2017-12-08 15:53:27 +01:00
|
|
|
this.domUtils.showErrorModal('core.errorinvalidform', true);
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-29 10:05:20 +01:00
|
|
|
const params: any = {
|
2017-11-29 16:03:47 +01:00
|
|
|
username: this.signupForm.value.username.trim().toLowerCase(),
|
|
|
|
password: this.signupForm.value.password,
|
|
|
|
firstname: this.textUtils.cleanTags(this.signupForm.value.firstname),
|
|
|
|
lastname: this.textUtils.cleanTags(this.signupForm.value.lastname),
|
|
|
|
email: this.signupForm.value.email.trim(),
|
|
|
|
city: this.textUtils.cleanTags(this.signupForm.value.city),
|
|
|
|
country: this.signupForm.value.country
|
|
|
|
},
|
2017-12-08 15:53:27 +01:00
|
|
|
modal = this.domUtils.showModalLoading('core.sending', true);
|
2017-11-29 16:03:47 +01:00
|
|
|
|
|
|
|
if (this.siteConfig.launchurl) {
|
2018-01-29 10:05:20 +01:00
|
|
|
const service = this.sitesProvider.determineService(this.siteUrl);
|
2017-11-29 16:03:47 +01:00
|
|
|
params.redirect = this.loginHelper.prepareForSSOLogin(this.siteUrl, service, this.siteConfig.launchurl);
|
|
|
|
}
|
|
|
|
|
2018-04-06 14:37:49 +02:00
|
|
|
// Get the recaptcha response (if needed).
|
|
|
|
if (this.settings.recaptchapublickey && this.captcha.recaptcharesponse) {
|
|
|
|
params.recaptcharesponse = this.captcha.recaptcharesponse;
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data for the custom profile fields.
|
2018-01-25 13:19:11 +01:00
|
|
|
this.userProfileFieldDelegate.getDataForFields(this.settings.profilefields, true, 'email', this.signupForm.value).then(
|
2018-01-29 10:05:20 +01:00
|
|
|
(fieldsData) => {
|
|
|
|
params.customprofilefields = fieldsData;
|
2017-11-29 16:03:47 +01:00
|
|
|
|
2018-01-29 10:05:20 +01:00
|
|
|
this.wsProvider.callAjax('auth_email_signup_user', params, { siteUrl: this.siteUrl }).then((result) => {
|
|
|
|
if (result.success) {
|
|
|
|
// Show alert and ho back.
|
|
|
|
const message = this.translate.instant('core.login.emailconfirmsent', { $a: params.email });
|
|
|
|
this.domUtils.showAlert(this.translate.instant('core.success'), message);
|
|
|
|
this.navCtrl.pop();
|
|
|
|
} else {
|
2018-04-06 14:37:49 +02:00
|
|
|
if (result.warnings && result.warnings.length) {
|
|
|
|
let error = result.warnings[0].message;
|
|
|
|
if (error == 'incorrect-captcha-sol') {
|
2018-05-16 14:43:25 +02:00
|
|
|
error = this.translate.instant('core.login.recaptchaincorrect');
|
2018-04-06 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.domUtils.showErrorModal(error);
|
|
|
|
} else {
|
|
|
|
this.domUtils.showErrorModal('core.login.usernotaddederror', true);
|
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
this.domUtils.showErrorModalDefault(error && error.error, 'core.login.usernotaddederror', true);
|
|
|
|
}).finally(() => {
|
|
|
|
modal.dismiss();
|
|
|
|
});
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show authentication instructions.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
protected showAuthInstructions(): void {
|
2018-01-23 13:00:00 +01:00
|
|
|
this.textUtils.expandText(this.translate.instant('core.login.instructions'), this.authInstructions);
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|
2018-05-16 14:43:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show contact information on site (we have to display again the age verification form).
|
|
|
|
*/
|
|
|
|
showContactOnSite(): void {
|
|
|
|
this.utils.openInBrowser(this.siteUrl + '/login/verify_age_location.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify Age.
|
|
|
|
*/
|
|
|
|
verifyAge(): void {
|
|
|
|
if (!this.ageVerificationForm.valid) {
|
|
|
|
this.domUtils.showErrorModal('core.errorinvalidform', true);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const modal = this.domUtils.showModalLoading('core.sending', true),
|
|
|
|
params = this.ageVerificationForm.value;
|
|
|
|
|
|
|
|
params.age = parseInt(params.age, 10); // Use just the integer part.
|
|
|
|
|
|
|
|
this.wsProvider.callAjax('core_auth_is_minor', params, {siteUrl: this.siteUrl}).then((result) => {
|
|
|
|
if (!result.status) {
|
|
|
|
// Not a minor, go ahead!
|
|
|
|
this.ageDigitalConsentVerification = false;
|
|
|
|
} else {
|
|
|
|
// Is a minor!!
|
|
|
|
this.isMinor = true;
|
|
|
|
}
|
|
|
|
}).catch(() => {
|
|
|
|
// Something wrong, redirect to the site.
|
|
|
|
this.domUtils.showErrorModal('There was an error verifying your age, please try again using the browser.');
|
|
|
|
}).finally(() => {
|
|
|
|
modal.dismiss();
|
|
|
|
});
|
|
|
|
}
|
2017-11-29 16:03:47 +01:00
|
|
|
}
|