2014-08-13 07:04:36 +08:00
|
|
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2014-08-13 07:04:36 +08:00
|
|
|
import DiscourseController from 'discourse/controllers/controller';
|
|
|
|
|
|
|
|
export default DiscourseController.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,
|
2013-05-31 02:12:33 +08:00
|
|
|
|
2014-08-15 10:09:12 +08:00
|
|
|
hasAuthOptions: Em.computed.notEmpty('authOptions'),
|
2014-08-15 01:14:56 +08:00
|
|
|
canCreateLocal: Discourse.computed.setting('enable_local_logins'),
|
2014-08-15 10:09:12 +08:00
|
|
|
showCreateForm: Em.computed.or('hasAuthOptions', 'canCreateLocal'),
|
2014-09-12 04:46:34 +08:00
|
|
|
maxUsernameLength: Discourse.computed.setting('max_username_length'),
|
2014-08-15 01:14:56 +08:00
|
|
|
|
2014-01-04 07:05:46 +08:00
|
|
|
resetForm: function() {
|
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,
|
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() {
|
2014-05-17 04:01:45 +08:00
|
|
|
if (!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
|
|
|
|
var userFields = this.get('userFields');
|
|
|
|
if (!Ember.empty(userFields)) {
|
|
|
|
var anyEmpty = userFields.any(function(uf) {
|
|
|
|
var val = uf.get('value');
|
|
|
|
return !val || Ember.empty(val);
|
|
|
|
});
|
|
|
|
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
|
|
|
|
|
|
|
passwordRequired: function() {
|
2014-03-20 11:49:25 +08:00
|
|
|
return this.blank('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() {
|
2013-12-20 05:15:36 +08:00
|
|
|
return I18n.t('user.password.instructions', {count: Discourse.SiteSettings.min_password_length});
|
2013-12-17 06:31:05 +08:00
|
|
|
}.property(),
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
// Validate the name
|
|
|
|
nameValidation: function() {
|
|
|
|
// If blank, fail without a reason
|
|
|
|
if (this.blank('accountName')) return Discourse.InputValidation.create({ failed: true });
|
|
|
|
|
|
|
|
if (this.get('accountPasswordConfirm') === 0) {
|
|
|
|
this.fetchConfirmationValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If too short
|
|
|
|
if (this.get('accountName').length < 3) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.name.too_short')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks good!
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.name.ok')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}.property('accountName'),
|
|
|
|
|
|
|
|
// Check the email address
|
|
|
|
emailValidation: function() {
|
|
|
|
// If blank, fail without a reason
|
|
|
|
var email;
|
|
|
|
if (this.blank('accountEmail')) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
failed: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
email = this.get("accountEmail");
|
2013-07-26 01:01:27 +08:00
|
|
|
|
|
|
|
if (this.get('rejectedEmails').contains(email)) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
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')) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.authenticated', {
|
2013-05-31 02:12:33 +08:00
|
|
|
provider: this.get('authOptions.auth_provider')
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Discourse.Utilities.emailValid(email)) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.ok')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.email.invalid')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
2013-07-26 01:01:27 +08:00
|
|
|
}.property('accountEmail', 'rejectedEmails.@each'),
|
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'),
|
|
|
|
|
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);
|
|
|
|
}
|
2013-11-21 03:22:51 +08:00
|
|
|
if (this.get('emailValidation.ok') && (this.blank('accountUsername') || this.get('authOptions.email'))) {
|
|
|
|
// 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'),
|
|
|
|
|
|
|
|
fetchExistingUsername: Discourse.debounce(function() {
|
|
|
|
var self = this;
|
|
|
|
Discourse.User.checkUsername(null, this.get('accountEmail')).then(function(result) {
|
2013-11-21 03:22:51 +08:00
|
|
|
if (result.suggestion && (self.blank('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()) {
|
|
|
|
return this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
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 {
|
|
|
|
return this.set('uniqueUsernameValidation', Discourse.InputValidation.create({ failed: true }));
|
|
|
|
}
|
|
|
|
} else if (this.shouldCheckUsernameMatch()) {
|
|
|
|
this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
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')) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
|
|
|
reason: I18n.t('user.username.prefilled')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
// If blank, fail without a reason
|
|
|
|
if (this.blank('accountUsername')) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
failed: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If too short
|
2014-06-10 06:26:42 +08:00
|
|
|
if (this.get('accountUsername').length < Discourse.SiteSettings.min_username_length) {
|
2013-05-31 02:12:33 +08:00
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
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')) {
|
2013-05-31 02:12:33 +08:00
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
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
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
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() {
|
|
|
|
return !this.blank('accountUsername') && this.get('accountUsername').length > 2;
|
|
|
|
},
|
|
|
|
|
|
|
|
checkUsernameAvailability: Discourse.debounce(function() {
|
|
|
|
var _this = this;
|
|
|
|
if (this.shouldCheckUsernameMatch()) {
|
|
|
|
return Discourse.User.checkUsername(this.get('accountUsername'), this.get('accountEmail')).then(function(result) {
|
|
|
|
_this.set('globalNicknameExists', false);
|
|
|
|
if (result.available) {
|
|
|
|
if (result.global_match) {
|
|
|
|
_this.set('globalNicknameExists', true);
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.global_match')
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.available')
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (result.suggestion) {
|
|
|
|
if (result.global_match !== void 0 && result.global_match === false) {
|
|
|
|
_this.set('globalNicknameExists', true);
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.global_mismatch', result)
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.username.not_available', result)
|
2013-05-31 02:12:33 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else if (result.errors) {
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
|
|
|
reason: result.errors.join(' ')
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
_this.set('globalNicknameExists', true);
|
|
|
|
return _this.set('uniqueUsernameValidation', Discourse.InputValidation.create({
|
|
|
|
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() {
|
|
|
|
var basicValidation, uniqueUsername;
|
|
|
|
basicValidation = this.get('basicUsernameValidation');
|
|
|
|
uniqueUsername = this.get('uniqueUsernameValidation');
|
|
|
|
if (uniqueUsername) {
|
|
|
|
return uniqueUsername;
|
|
|
|
}
|
|
|
|
return basicValidation;
|
|
|
|
}.property('uniqueUsernameValidation', 'basicUsernameValidation'),
|
|
|
|
|
|
|
|
usernameNeedsToBeValidatedWithEmail: function() {
|
|
|
|
return( this.get('globalNicknameExists') || false );
|
|
|
|
},
|
|
|
|
|
|
|
|
// Validate the password
|
|
|
|
passwordValidation: function() {
|
|
|
|
var password;
|
|
|
|
if (!this.get('passwordRequired')) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If blank, fail without a reason
|
|
|
|
password = this.get("accountPassword");
|
|
|
|
if (this.blank('accountPassword')) {
|
|
|
|
return Discourse.InputValidation.create({ failed: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// If too short
|
2013-12-20 05:15:36 +08:00
|
|
|
if (password.length < Discourse.SiteSettings.min_password_length) {
|
2013-05-31 02:12:33 +08:00
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
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)) {
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t('user.password.common')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:12:33 +08:00
|
|
|
// Looks good!
|
|
|
|
return Discourse.InputValidation.create({
|
|
|
|
ok: true,
|
2013-07-09 07:32:16 +08:00
|
|
|
reason: I18n.t('user.password.ok')
|
2013-05-31 02:12:33 +08:00
|
|
|
});
|
2013-12-21 05:34:34 +08:00
|
|
|
}.property('accountPassword', 'rejectedPasswords.@each'),
|
2013-05-31 02:12:33 +08:00
|
|
|
|
|
|
|
fetchConfirmationValue: function() {
|
|
|
|
var createAccountController = this;
|
|
|
|
return Discourse.ajax('/users/hp.json').then(function (json) {
|
|
|
|
createAccountController.set('accountPasswordConfirm', json.value);
|
|
|
|
createAccountController.set('accountChallenge', json.challenge.split("").reverse().join(""));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-18 01:37:49 +08:00
|
|
|
actions: {
|
2014-08-15 00:51:16 +08:00
|
|
|
externalLogin: function(provider) {
|
|
|
|
this.get('controllers.login').send('externalLogin', provider);
|
|
|
|
},
|
|
|
|
|
2013-12-18 01:37:49 +08:00
|
|
|
createAccount: function() {
|
2014-09-27 02:48:34 +08:00
|
|
|
var self = this,
|
|
|
|
attrs = this.getProperties('accountName', 'accountEmail', 'accountPassword', 'accountUsername', 'accountPasswordConfirm', 'accountChallenge'),
|
|
|
|
userFields = this.get('userFields');
|
|
|
|
|
|
|
|
// Add the userfields to the data
|
|
|
|
if (!Em.empty(userFields)) {
|
|
|
|
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) {
|
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:
|
|
|
|
var $hidden_login_form = $('#hidden-login-form');
|
2014-10-01 16:23:50 +08:00
|
|
|
var account_created_url = Discourse.getURL('/users/' + self.get('accountUsername') + '/account-created');
|
2014-09-27 02:48:34 +08:00
|
|
|
$hidden_login_form.find('input[name=username]').val(attrs.accountName);
|
|
|
|
$hidden_login_form.find('input[name=password]').val(attrs.accountPassword);
|
2014-10-01 16:23:50 +08:00
|
|
|
$hidden_login_form.find('input[name=redirect]').val(account_created_url);
|
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');
|
|
|
|
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; }
|
|
|
|
|
|
|
|
var userFields = this.site.get('user_fields');
|
|
|
|
if (userFields) {
|
|
|
|
userFields = userFields.map(function(f) {
|
|
|
|
return Ember.Object.create({
|
|
|
|
value: null,
|
|
|
|
field: f
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.set('userFields', userFields);
|
|
|
|
}.on('init')
|
|
|
|
|
2013-07-09 07:32:16 +08:00
|
|
|
});
|