2016-07-01 01:55:44 +08:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2015-08-11 05:11:27 +08:00
|
|
|
import debounce from 'discourse/lib/debounce';
|
2014-08-13 07:04:36 +08:00
|
|
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
2015-08-08 03:08:27 +08:00
|
|
|
import { setting } from 'discourse/lib/computed';
|
2015-10-30 01:44:08 +08:00
|
|
|
import { on } from 'ember-addons/ember-computed-decorators';
|
2016-06-15 02:31:51 +08:00
|
|
|
import { emailValid } from 'discourse/lib/utilities';
|
2016-07-01 00:26:49 +08:00
|
|
|
import InputValidation from 'discourse/models/input-validation';
|
2014-08-13 07:04:36 +08:00
|
|
|
|
2015-08-12 00:27:07 +08:00
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
2014-08-15 00:51:16 +08:00
|
|
|
needs: ['login'],
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
uniqueUsernameValidation: null,
|
|
|
|
globalNicknameExists: false,
|
|
|
|
complete: false,
|
|
|
|
accountPasswordConfirm: 0,
|
|
|
|
accountChallenge: 0,
|
|
|
|
formSubmitted: false,
|
2013-07-26 01:01:27 +08:00
|
|
|
rejectedEmails: Em.A([]),
|
2013-12-21 05:34:34 +08:00
|
|
|
rejectedPasswords: Em.A([]),
|
2013-11-20 03:15:05 +08:00
|
|
|
prefilledUsername: null,
|
2014-09-27 02:48:34 +08:00
|
|
|
userFields: null,
|
2016-03-04 02:01:31 +08:00
|
|
|
isDeveloper: false,
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2014-08-15 10:09:12 +08:00
|
|
|
hasAuthOptions: Em.computed.notEmpty('authOptions'),
|
2015-08-08 03:08:27 +08:00
|
|
|
canCreateLocal: setting('enable_local_logins'),
|
2014-08-15 10:09:12 +08:00
|
|
|
showCreateForm: Em.computed.or('hasAuthOptions', 'canCreateLocal'),
|
2015-08-08 03:08:27 +08:00
|
|
|
maxUsernameLength: setting('max_username_length'),
|
|
|
|
minUsernameLength: setting('min_username_length'),
|
2014-08-15 01:14:56 +08:00
|
|
|
|
2015-06-27 03:52:05 +08:00
|
|
|
resetForm() {
|
2014-09-27 02:48:34 +08:00
|
|
|
// We wrap the fields in a structure so we can assign a value
|
2014-01-04 07:05:46 +08:00
|
|
|
this.setProperties({
|
|
|
|
accountName: '',
|
|
|
|
accountEmail: '',
|
|
|
|
accountUsername: '',
|
|
|
|
accountPassword: '',
|
|
|
|
authOptions: null,
|
|
|
|
globalNicknameExists: false,
|
|
|
|
complete: false,
|
|
|
|
formSubmitted: false,
|
2014-09-27 02:48:34 +08:00
|
|
|
rejectedEmails: [],
|
|
|
|
rejectedPasswords: [],
|
|
|
|
prefilledUsername: null,
|
2016-03-04 02:01:31 +08:00
|
|
|
isDeveloper: false
|
2014-01-04 07:05:46 +08:00
|
|
|
});
|
2014-09-27 02:48:34 +08:00
|
|
|
this._createUserFields();
|
2014-01-04 07:05:46 +08:00
|
|
|
},
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
submitDisabled: function() {
|
2015-11-27 04:44:30 +08:00
|
|
|
if (!this.get('emailValidation.failed') && !this.get('passwordRequired')) return false; // 3rd party auth
|
2013-05-31 02:12:33 +08:00
|
|
|
if (this.get('formSubmitted')) return true;
|
|
|
|
if (this.get('nameValidation.failed')) return true;
|
|
|
|
if (this.get('emailValidation.failed')) return true;
|
|
|
|
if (this.get('usernameValidation.failed')) return true;
|
|
|
|
if (this.get('passwordValidation.failed')) return true;
|
2014-09-27 02:48:34 +08:00
|
|
|
|
|
|
|
// Validate required fields
|
2015-06-27 03:52:05 +08:00
|
|
|
let userFields = this.get('userFields');
|
2014-10-09 02:38:18 +08:00
|
|
|
if (userFields) { userFields = userFields.filterProperty('field.required'); }
|
2014-12-11 02:15:30 +08:00
|
|
|
if (!Ember.isEmpty(userFields)) {
|
2015-06-27 03:52:05 +08:00
|
|
|
const anyEmpty = userFields.any(function(uf) {
|
|
|
|
const val = uf.get('value');
|
2014-12-11 02:15:30 +08:00
|
|
|
return !val || Ember.isEmpty(val);
|
2014-09-27 02:48:34 +08:00
|
|
|
});
|
|
|
|
if (anyEmpty) { return true; }
|
|
|
|
}
|
2013-05-31 02:12:33 +08:00
|
|
|
return false;
|
2014-09-30 04:59:03 +08:00
|
|
|
}.property('passwordRequired', 'nameValidation.failed', 'emailValidation.failed', 'usernameValidation.failed', 'passwordValidation.failed', 'formSubmitted', 'userFields.@each.value'),
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2015-06-27 03:52:05 +08:00
|
|
|
|
|
|
|
usernameRequired: Ember.computed.not('authOptions.omit_username'),
|
|
|
|
|
2016-06-29 08:43:40 +08:00
|
|
|
fullnameRequired: function() {
|
|
|
|
return this.get('siteSettings.full_name_required') || this.get('siteSettings.enable_names');
|
|
|
|
}.property(),
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
passwordRequired: function() {
|
2015-08-12 00:27:07 +08:00
|
|
|
return Ember.isEmpty(this.get('authOptions.auth_provider'));
|
2013-05-31 02:12:33 +08:00
|
|
|
}.property('authOptions.auth_provider'),
|
|
|
|
|
2013-12-17 06:31:05 +08:00
|
|
|
passwordInstructions: function() {
|
2016-03-04 17:52:23 +08:00
|
|
|
return this.get('isDeveloper') ? I18n.t('user.password.instructions', {count: Discourse.SiteSettings.min_admin_password_length}) : I18n.t('user.password.instructions', {count: Discourse.SiteSettings.min_password_length});
|
2016-03-04 02:01:31 +08:00
|
|
|
}.property('isDeveloper'),
|
2013-12-17 06:31:05 +08:00
|
|
|
|
2015-04-03 05:07:56 +08:00
|
|
|
nameInstructions: function() {
|
|
|
|
return I18n.t(Discourse.SiteSettings.full_name_required ? 'user.name.instructions_required' : 'user.name.instructions');
|
|
|
|
}.property(),
|
|
|
|
|
|
|
|
// Validate the name.
|
2013-05-31 02:12:33 +08:00
|
|
|
nameValidation: function() {
|
2015-08-12 00:27:07 +08:00
|
|
|
if (Discourse.SiteSettings.full_name_required && Ember.isEmpty(this.get('accountName'))) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({ failed: true });
|
2015-04-03 05:07:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({ok: true});
|
2013-05-31 02:12:33 +08:00
|
|
|
}.property('accountName'),
|
|
|
|
|
|
|
|
// Check the email address
|
|
|
|
emailValidation: function() {
|
|
|
|
// If blank, fail without a reason
|
2015-06-27 03:52:05 +08:00
|
|
|
let email;
|
2015-08-12 00:27:07 +08:00
|
|
|
if (Ember.isEmpty(this.get('accountEmail'))) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
email = this.get("accountEmail");
|
2013-07-26 01:01:27 +08:00
|
|
|
|
|
|
|
if (this.get('rejectedEmails').contains(email)) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-07-26 01:01:27 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.email.invalid')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
if ((this.get('authOptions.email') === email) && this.get('authOptions.email_valid')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.authenticated', {
|
2016-03-30 06:08:55 +08:00
|
|
|
provider: this.authProviderDisplayName(this.get('authOptions.auth_provider'))
|
2013-05-31 02:12:33 +08:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
if (emailValid(email)) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.ok')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.invalid')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
2016-04-29 04:49:24 +08:00
|
|
|
}.property('accountEmail', 'rejectedEmails.[]'),
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2014-01-04 07:05:46 +08:00
|
|
|
emailValidated: function() {
|
|
|
|
return this.get('authOptions.email') === this.get("accountEmail") && this.get('authOptions.email_valid');
|
|
|
|
}.property('accountEmail', 'authOptions.email', 'authOptions.email_valid'),
|
|
|
|
|
2016-03-30 06:08:55 +08:00
|
|
|
authProviderDisplayName(provider) {
|
|
|
|
switch(provider) {
|
|
|
|
case "Google_oauth2": return "Google";
|
|
|
|
default: return provider;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-20 03:15:05 +08:00
|
|
|
prefillUsername: function() {
|
|
|
|
if (this.get('prefilledUsername')) {
|
2013-11-21 03:22:51 +08:00
|
|
|
// If username field has been filled automatically, and email field just changed,
|
|
|
|
// then remove the username.
|
2013-11-20 03:15:05 +08:00
|
|
|
if (this.get('accountUsername') === this.get('prefilledUsername')) {
|
|
|
|
this.set('accountUsername', '');
|
|
|
|
}
|
|
|
|
this.set('prefilledUsername', null);
|
|
|
|
}
|
2015-08-12 00:27:07 +08:00
|
|
|
if (this.get('emailValidation.ok') && (Ember.isEmpty(this.get('accountUsername')) || this.get('authOptions.email'))) {
|
2013-11-21 03:22:51 +08:00
|
|
|
// If email is valid and username has not been entered yet,
|
|
|
|
// or email and username were filled automatically by 3rd parth auth,
|
|
|
|
// then look for a registered username that matches the email.
|
2013-11-20 03:15:05 +08:00
|
|
|
this.fetchExistingUsername();
|
|
|
|
}
|
|
|
|
}.observes('emailValidation', 'accountEmail'),
|
|
|
|
|
2015-08-11 05:11:27 +08:00
|
|
|
fetchExistingUsername: debounce(function() {
|
2015-06-27 03:52:05 +08:00
|
|
|
const self = this;
|
2013-11-20 03:15:05 +08:00
|
|
|
Discourse.User.checkUsername(null, this.get('accountEmail')).then(function(result) {
|
2015-08-12 00:27:07 +08:00
|
|
|
if (result.suggestion && (Ember.isEmpty(self.get('accountUsername')) || self.get('accountUsername') === self.get('authOptions.username'))) {
|
2013-11-20 03:15:05 +08:00
|
|
|
self.set('accountUsername', result.suggestion);
|
|
|
|
self.set('prefilledUsername', result.suggestion);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 500),
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
usernameMatch: function() {
|
|
|
|
if (this.usernameNeedsToBeValidatedWithEmail()) {
|
|
|
|
if (this.get('emailValidation.failed')) {
|
|
|
|
if (this.shouldCheckUsernameMatch()) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return this.set('uniqueUsernameValidation', InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.enter_email')
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
} else {
|
2016-07-01 00:26:49 +08:00
|
|
|
return this.set('uniqueUsernameValidation', InputValidation.create({ failed: true }));
|
2013-05-31 02:12:33 +08:00
|
|
|
}
|
|
|
|
} else if (this.shouldCheckUsernameMatch()) {
|
2016-07-01 00:26:49 +08:00
|
|
|
this.set('uniqueUsernameValidation', InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.checking')
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
return this.checkUsernameAvailability();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.observes('accountEmail'),
|
|
|
|
|
|
|
|
basicUsernameValidation: function() {
|
|
|
|
this.set('uniqueUsernameValidation', null);
|
|
|
|
|
2013-11-20 03:15:05 +08:00
|
|
|
if (this.get('accountUsername') === this.get('prefilledUsername')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-11-20 03:15:05 +08:00
|
|
|
ok: true,
|
|
|
|
reason: I18n.t('user.username.prefilled')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
// If blank, fail without a reason
|
2015-08-12 00:27:07 +08:00
|
|
|
if (Ember.isEmpty(this.get('accountUsername'))) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If too short
|
2014-06-10 06:26:42 +08:00
|
|
|
if (this.get('accountUsername').length < Discourse.SiteSettings.min_username_length) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.too_short')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If too long
|
2014-09-12 04:46:34 +08:00
|
|
|
if (this.get('accountUsername').length > this.get('maxUsernameLength')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.too_long')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checkUsernameAvailability();
|
|
|
|
// Let's check it out asynchronously
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.checking')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}.property('accountUsername'),
|
|
|
|
|
|
|
|
shouldCheckUsernameMatch: function() {
|
2015-08-12 00:27:07 +08:00
|
|
|
return !Ember.isEmpty(this.get('accountUsername')) && this.get('accountUsername').length >= this.get('minUsernameLength');
|
2013-05-31 02:12:33 +08:00
|
|
|
},
|
|
|
|
|
2015-08-11 05:11:27 +08:00
|
|
|
checkUsernameAvailability: debounce(function() {
|
2015-06-27 03:52:05 +08:00
|
|
|
const _this = this;
|
2013-05-31 02:12:33 +08:00
|
|
|
if (this.shouldCheckUsernameMatch()) {
|
|
|
|
return Discourse.User.checkUsername(this.get('accountUsername'), this.get('accountEmail')).then(function(result) {
|
2016-03-04 02:01:31 +08:00
|
|
|
_this.set('isDeveloper', false);
|
2013-05-31 02:12:33 +08:00
|
|
|
if (result.available) {
|
2016-03-04 02:01:31 +08:00
|
|
|
if (result.is_developer) {
|
|
|
|
_this.set('isDeveloper', true);
|
2013-05-31 02:12:33 +08:00
|
|
|
}
|
2016-07-01 00:26:49 +08:00
|
|
|
return _this.set('uniqueUsernameValidation', InputValidation.create({
|
2016-03-04 02:01:31 +08:00
|
|
|
ok: true,
|
|
|
|
reason: I18n.t('user.username.available')
|
|
|
|
}));
|
2013-05-31 02:12:33 +08:00
|
|
|
} else {
|
|
|
|
if (result.suggestion) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return _this.set('uniqueUsernameValidation', InputValidation.create({
|
2016-03-04 02:01:31 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.username.not_available', result)
|
|
|
|
}));
|
2013-05-31 02:12:33 +08:00
|
|
|
} else if (result.errors) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return _this.set('uniqueUsernameValidation', InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
|
|
|
reason: result.errors.join(' ')
|
|
|
|
}));
|
|
|
|
} else {
|
2016-07-01 00:26:49 +08:00
|
|
|
return _this.set('uniqueUsernameValidation', InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.enter_email')
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 500),
|
|
|
|
|
|
|
|
// Actually wait for the async name check before we're 100% sure we're good to go
|
|
|
|
usernameValidation: function() {
|
2015-06-27 03:52:05 +08:00
|
|
|
const basicValidation = this.get('basicUsernameValidation');
|
|
|
|
const uniqueUsername = this.get('uniqueUsernameValidation');
|
|
|
|
return uniqueUsername ? uniqueUsername : basicValidation;
|
2013-05-31 02:12:33 +08:00
|
|
|
}.property('uniqueUsernameValidation', 'basicUsernameValidation'),
|
|
|
|
|
2015-06-27 03:52:05 +08:00
|
|
|
usernameNeedsToBeValidatedWithEmail() {
|
2013-05-31 02:12:33 +08:00
|
|
|
return( this.get('globalNicknameExists') || false );
|
|
|
|
},
|
|
|
|
|
|
|
|
// Validate the password
|
|
|
|
passwordValidation: function() {
|
|
|
|
if (!this.get('passwordRequired')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({ ok: true });
|
2013-05-31 02:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If blank, fail without a reason
|
2015-06-27 03:52:05 +08:00
|
|
|
const password = this.get("accountPassword");
|
2015-08-12 00:27:07 +08:00
|
|
|
if (Ember.isEmpty(this.get('accountPassword'))) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({ failed: true });
|
2013-05-31 02:12:33 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 03:42:41 +08:00
|
|
|
// If too short
|
2016-03-04 17:52:23 +08:00
|
|
|
const passwordLength = this.get('isDeveloper') ? Discourse.SiteSettings.min_admin_password_length : Discourse.SiteSettings.min_password_length;
|
2016-03-04 03:42:41 +08:00
|
|
|
if (password.length < passwordLength) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.password.too_short')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-21 05:34:34 +08:00
|
|
|
if (this.get('rejectedPasswords').contains(password)) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-12-21 05:34:34 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.password.common')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:27:07 +08:00
|
|
|
if (!Ember.isEmpty(this.get('accountUsername')) && this.get('accountPassword') === this.get('accountUsername')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2015-02-26 00:59:57 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.password.same_as_username')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:27:07 +08:00
|
|
|
if (!Ember.isEmpty(this.get('accountEmail')) && this.get('accountPassword') === this.get('accountEmail')) {
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2015-02-28 02:47:43 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.password.same_as_email')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
// Looks good!
|
2016-07-01 00:26:49 +08:00
|
|
|
return InputValidation.create({
|
2013-05-31 02:12:33 +08:00
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.password.ok')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
2016-04-29 04:49:24 +08:00
|
|
|
}.property('accountPassword', 'rejectedPasswords.[]', 'accountUsername', 'accountEmail', 'isDeveloper'),
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2015-10-30 01:44:08 +08:00
|
|
|
@on('init')
|
2015-06-27 03:52:05 +08:00
|
|
|
fetchConfirmationValue() {
|
2016-07-01 01:55:44 +08:00
|
|
|
return ajax('/users/hp.json').then(json => {
|
2015-10-30 01:44:08 +08:00
|
|
|
this.set('accountPasswordConfirm', json.value);
|
|
|
|
this.set('accountChallenge', json.challenge.split("").reverse().join(""));
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-18 01:37:49 +08:00
|
|
|
actions: {
|
2015-06-27 03:52:05 +08:00
|
|
|
externalLogin(provider) {
|
2014-08-15 00:51:16 +08:00
|
|
|
this.get('controllers.login').send('externalLogin', provider);
|
|
|
|
},
|
|
|
|
|
2015-06-27 03:52:05 +08:00
|
|
|
createAccount() {
|
|
|
|
const self = this,
|
2014-09-27 02:48:34 +08:00
|
|
|
attrs = this.getProperties('accountName', 'accountEmail', 'accountPassword', 'accountUsername', 'accountPasswordConfirm', 'accountChallenge'),
|
|
|
|
userFields = this.get('userFields');
|
|
|
|
|
|
|
|
// Add the userfields to the data
|
2014-12-11 02:15:30 +08:00
|
|
|
if (!Ember.isEmpty(userFields)) {
|
2014-09-27 02:48:34 +08:00
|
|
|
attrs.userFields = {};
|
|
|
|
userFields.forEach(function(f) {
|
|
|
|
attrs.userFields[f.get('field.id')] = f.get('value');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-18 01:37:49 +08:00
|
|
|
this.set('formSubmitted', true);
|
2014-09-27 02:48:34 +08:00
|
|
|
return Discourse.User.createAccount(attrs).then(function(result) {
|
2016-03-04 02:01:31 +08:00
|
|
|
self.set('isDeveloper', false);
|
2013-12-18 01:37:49 +08:00
|
|
|
if (result.success) {
|
2014-09-23 21:50:57 +08:00
|
|
|
// Trigger the browser's password manager using the hidden static login form:
|
2015-06-27 03:52:05 +08:00
|
|
|
const $hidden_login_form = $('#hidden-login-form');
|
2014-10-20 03:46:07 +08:00
|
|
|
$hidden_login_form.find('input[name=username]').val(attrs.accountUsername);
|
2014-09-27 02:48:34 +08:00
|
|
|
$hidden_login_form.find('input[name=password]').val(attrs.accountPassword);
|
2014-10-01 22:30:26 +08:00
|
|
|
$hidden_login_form.find('input[name=redirect]').val(Discourse.getURL('/users/account-created'));
|
2014-09-23 21:50:57 +08:00
|
|
|
$hidden_login_form.submit();
|
2013-12-18 01:37:49 +08:00
|
|
|
} else {
|
2013-12-21 05:34:34 +08:00
|
|
|
self.flash(result.message || I18n.t('create_account.failed'), 'error');
|
2016-03-04 02:01:31 +08:00
|
|
|
if (result.is_developer) {
|
|
|
|
self.set('isDeveloper', true);
|
|
|
|
}
|
2013-12-21 05:34:34 +08:00
|
|
|
if (result.errors && result.errors.email && result.errors.email.length > 0 && result.values) {
|
|
|
|
self.get('rejectedEmails').pushObject(result.values.email);
|
|
|
|
}
|
|
|
|
if (result.errors && result.errors.password && result.errors.password.length > 0) {
|
2014-09-27 02:48:34 +08:00
|
|
|
self.get('rejectedPasswords').pushObject(attrs.accountPassword);
|
2013-12-18 01:37:49 +08:00
|
|
|
}
|
2013-12-21 05:34:34 +08:00
|
|
|
self.set('formSubmitted', false);
|
2013-07-26 01:01:27 +08:00
|
|
|
}
|
2014-04-15 22:13:47 +08:00
|
|
|
if (result.active && !Discourse.SiteSettings.must_approve_users) {
|
2013-12-18 01:37:49 +08:00
|
|
|
return window.location.reload();
|
|
|
|
}
|
|
|
|
}, function() {
|
2013-12-21 05:34:34 +08:00
|
|
|
self.set('formSubmitted', false);
|
|
|
|
return self.flash(I18n.t('create_account.failed'), 'error');
|
2013-12-18 01:37:49 +08:00
|
|
|
});
|
|
|
|
}
|
2014-09-27 02:48:34 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_createUserFields: function() {
|
|
|
|
if (!this.site) { return; }
|
|
|
|
|
2015-06-27 03:52:05 +08:00
|
|
|
let userFields = this.site.get('user_fields');
|
2014-09-27 02:48:34 +08:00
|
|
|
if (userFields) {
|
2015-07-31 02:52:53 +08:00
|
|
|
userFields = _.sortBy(userFields, 'position').map(function(f) {
|
|
|
|
return Ember.Object.create({ value: null, field: f });
|
2014-09-27 02:48:34 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.set('userFields', userFields);
|
|
|
|
}.on('init')
|
|
|
|
|
2013-07-09 07:32:16 +08:00
|
|
|
});
|