Better support for external logins
This commit is contained in:
parent
0523750d18
commit
70931b78d9
|
@ -1,6 +1,15 @@
|
||||||
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
import ModalFunctionality from 'discourse/mixins/modal-functionality';
|
||||||
import DiscourseController from 'discourse/controllers/controller';
|
import DiscourseController from 'discourse/controllers/controller';
|
||||||
|
|
||||||
|
// This is happening outside of the app via popup
|
||||||
|
function showModal(modal) {
|
||||||
|
const route = Discourse.__container__.lookup('route:application');
|
||||||
|
Discourse.Route.showModal(route, modal);
|
||||||
|
}
|
||||||
|
const AuthErrors =
|
||||||
|
['requires_invite', 'awaiting_approval', 'awaiting_confirmation', 'admin_not_allowed_from_ip_address',
|
||||||
|
'not_allowed_from_ip_address'];
|
||||||
|
|
||||||
export default DiscourseController.extend(ModalFunctionality, {
|
export default DiscourseController.extend(ModalFunctionality, {
|
||||||
needs: ['modal', 'createAccount', 'forgotPassword', 'application'],
|
needs: ['modal', 'createAccount', 'forgotPassword', 'application'],
|
||||||
authenticate: null,
|
authenticate: null,
|
||||||
|
@ -146,42 +155,27 @@ export default DiscourseController.extend(ModalFunctionality, {
|
||||||
}).property('authenticate'),
|
}).property('authenticate'),
|
||||||
|
|
||||||
authenticationComplete: function(options) {
|
authenticationComplete: function(options) {
|
||||||
if (options.requires_invite) {
|
|
||||||
this.send('showLogin');
|
const self = this;
|
||||||
this.flash(I18n.t('login.requires_invite'), 'success');
|
function loginError(errorMsg, className) {
|
||||||
this.set('authenticate', null);
|
showModal('login');
|
||||||
return;
|
Ember.run.next(function() {
|
||||||
|
self.flash(errorMsg, className || 'success');
|
||||||
|
self.set('authenticate', null);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (options.awaiting_approval) {
|
|
||||||
this.send('showLogin');
|
for (let i=0; i<AuthErrors.length; i++) {
|
||||||
this.flash(I18n.t('login.awaiting_approval'), 'success');
|
const cond = AuthErrors[i];
|
||||||
this.set('authenticate', null);
|
if (options[cond]) {
|
||||||
return;
|
return loginError(I18n.t("login." + cond));
|
||||||
}
|
}
|
||||||
if (options.awaiting_activation) {
|
|
||||||
this.send('showLogin');
|
|
||||||
this.flash(I18n.t('login.awaiting_confirmation'), 'success');
|
|
||||||
this.set('authenticate', null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (options.admin_not_allowed_from_ip_address) {
|
|
||||||
this.send('showLogin');
|
|
||||||
this.flash(I18n.t('login.admin_not_allowed_from_ip_address'), 'success');
|
|
||||||
this.set('authenticate', null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (options.not_allowed_from_ip_address) {
|
|
||||||
this.send('showLogin');
|
|
||||||
this.flash(I18n.t('login.not_allowed_from_ip_address'), 'success');
|
|
||||||
this.set('authenticate', null);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.suspended) {
|
if (options.suspended) {
|
||||||
this.send('showLogin');
|
return loginError(options.suspended_message, 'error');
|
||||||
this.flash(options.suspended_message, 'error');
|
|
||||||
this.set('authenticate', null);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reload the page if we're authenticated
|
// Reload the page if we're authenticated
|
||||||
if (options.authenticated) {
|
if (options.authenticated) {
|
||||||
if (window.location.pathname === Discourse.getURL('/login')) {
|
if (window.location.pathname === Discourse.getURL('/login')) {
|
||||||
|
@ -199,7 +193,7 @@ export default DiscourseController.extend(ModalFunctionality, {
|
||||||
accountName: options.name,
|
accountName: options.name,
|
||||||
authOptions: Em.Object.create(options)
|
authOptions: Em.Object.create(options)
|
||||||
});
|
});
|
||||||
this.send('showCreateAccount');
|
showModal('createAccount');
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
function unlessReadOnly(method) {
|
||||||
|
return function() {
|
||||||
|
if (this.site.get("isReadOnly")) {
|
||||||
|
bootbox.alert(I18n.t("read_only_mode.login_disabled"));
|
||||||
|
} else {
|
||||||
|
this[method]();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const ApplicationRoute = Discourse.Route.extend({
|
const ApplicationRoute = Discourse.Route.extend({
|
||||||
|
|
||||||
siteTitle: Discourse.computed.setting('title'),
|
siteTitle: Discourse.computed.setting('title'),
|
||||||
|
@ -59,32 +69,9 @@ const ApplicationRoute = Discourse.Route.extend({
|
||||||
this.intermediateTransitionTo('exception');
|
this.intermediateTransitionTo('exception');
|
||||||
},
|
},
|
||||||
|
|
||||||
showLogin() {
|
showLogin: unlessReadOnly('handleShowLogin'),
|
||||||
if (this.site.get("isReadOnly")) {
|
|
||||||
bootbox.alert(I18n.t("read_only_mode.login_disabled"));
|
|
||||||
} else {
|
|
||||||
this.handleShowLogin();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
showCreateAccount() {
|
showCreateAccount: unlessReadOnly('handleShowCreateAccount'),
|
||||||
if (this.site.get("isReadOnly")) {
|
|
||||||
bootbox.alert(I18n.t("read_only_mode.login_disabled"));
|
|
||||||
} else {
|
|
||||||
this.handleShowCreateAccount();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
autoLogin(modal, onFail){
|
|
||||||
const methods = Em.get('Discourse.LoginMethod.all');
|
|
||||||
if (!Discourse.SiteSettings.enable_local_logins &&
|
|
||||||
methods.length === 1) {
|
|
||||||
Discourse.Route.showModal(this, modal);
|
|
||||||
this.controllerFor('login').send('externalLogin', methods[0]);
|
|
||||||
} else {
|
|
||||||
onFail();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
showForgotPassword() {
|
showForgotPassword() {
|
||||||
Discourse.Route.showModal(this, 'forgotPassword');
|
Discourse.Route.showModal(this, 'forgotPassword');
|
||||||
|
@ -114,12 +101,7 @@ const ApplicationRoute = Discourse.Route.extend({
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Close the current modal, and destroy its state.
|
||||||
/**
|
|
||||||
Close the current modal, and destroy its state.
|
|
||||||
|
|
||||||
@method closeModal
|
|
||||||
**/
|
|
||||||
closeModal() {
|
closeModal() {
|
||||||
this.render('hide-modal', {into: 'modal', outlet: 'modalBody'});
|
this.render('hide-modal', {into: 'modal', outlet: 'modalBody'});
|
||||||
},
|
},
|
||||||
|
@ -128,18 +110,11 @@ const ApplicationRoute = Discourse.Route.extend({
|
||||||
Hide the modal, but keep it with all its state so that it can be shown again later.
|
Hide the modal, but keep it with all its state so that it can be shown again later.
|
||||||
This is useful if you want to prompt for confirmation. hideModal, ask "Are you sure?",
|
This is useful if you want to prompt for confirmation. hideModal, ask "Are you sure?",
|
||||||
user clicks "No", showModal. If user clicks "Yes", be sure to call closeModal.
|
user clicks "No", showModal. If user clicks "Yes", be sure to call closeModal.
|
||||||
|
|
||||||
@method hideModal
|
|
||||||
**/
|
**/
|
||||||
hideModal() {
|
hideModal() {
|
||||||
$('#discourse-modal').modal('hide');
|
$('#discourse-modal').modal('hide');
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
Show the modal. Useful after calling hideModal.
|
|
||||||
|
|
||||||
@method showModal
|
|
||||||
**/
|
|
||||||
showModal() {
|
showModal() {
|
||||||
$('#discourse-modal').modal('show');
|
$('#discourse-modal').modal('show');
|
||||||
},
|
},
|
||||||
|
@ -153,11 +128,6 @@ const ApplicationRoute = Discourse.Route.extend({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
Deletes a user and all posts and topics created by that user.
|
|
||||||
|
|
||||||
@method deleteSpammer
|
|
||||||
**/
|
|
||||||
deleteSpammer: function (user) {
|
deleteSpammer: function (user) {
|
||||||
this.send('closeModal');
|
this.send('closeModal');
|
||||||
user.deleteAsSpammer(function() { window.location.reload(); });
|
user.deleteAsSpammer(function() { window.location.reload(); });
|
||||||
|
@ -177,26 +147,28 @@ const ApplicationRoute = Discourse.Route.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
handleShowLogin() {
|
handleShowLogin() {
|
||||||
const self = this;
|
if (this.siteSettings.enable_sso) {
|
||||||
|
|
||||||
if(Discourse.SiteSettings.enable_sso) {
|
|
||||||
const returnPath = encodeURIComponent(window.location.pathname);
|
const returnPath = encodeURIComponent(window.location.pathname);
|
||||||
window.location = Discourse.getURL('/session/sso?return_path=' + returnPath);
|
window.location = Discourse.getURL('/session/sso?return_path=' + returnPath);
|
||||||
} else {
|
} else {
|
||||||
this.send('autoLogin', 'login', function(){
|
this._autoLogin('login', () => this.controllerFor('login').resetForm());
|
||||||
Discourse.Route.showModal(self, 'login');
|
|
||||||
self.controllerFor('login').resetForm();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
handleShowCreateAccount() {
|
handleShowCreateAccount() {
|
||||||
const self = this;
|
this._autoLogin('createAccount');
|
||||||
|
},
|
||||||
|
|
||||||
|
_autoLogin(modal, notAuto) {
|
||||||
|
const methods = Em.get('Discourse.LoginMethod.all');
|
||||||
|
if (!this.siteSettings.enable_local_logins && methods.length === 1) {
|
||||||
|
this.controllerFor('login').send('externalLogin', methods[0]);
|
||||||
|
} else {
|
||||||
|
Discourse.Route.showModal(this, modal);
|
||||||
|
if (notAuto) { notAuto(); }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
self.send('autoLogin', 'createAccount', function(){
|
|
||||||
Discourse.Route.showModal(self, 'createAccount');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
RSVP.EventTarget.mixin(ApplicationRoute);
|
RSVP.EventTarget.mixin(ApplicationRoute);
|
||||||
|
|
|
@ -197,15 +197,10 @@ Discourse.Route.reopenClass({
|
||||||
appEvents.trigger('dom:clean');
|
appEvents.trigger('dom:clean');
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
showModal: function(route, name, model) {
|
||||||
Shows a modal
|
route.controllerFor('modal').set('modalClass', null);
|
||||||
|
route.render(name, {into: 'modal', outlet: 'modalBody'});
|
||||||
@method showModal
|
var controller = route.controllerFor(name);
|
||||||
**/
|
|
||||||
showModal: function(router, name, model) {
|
|
||||||
router.controllerFor('modal').set('modalClass', null);
|
|
||||||
router.render(name, {into: 'modal', outlet: 'modalBody'});
|
|
||||||
var controller = router.controllerFor(name);
|
|
||||||
if (controller) {
|
if (controller) {
|
||||||
if (model) {
|
if (model) {
|
||||||
controller.set('model', model);
|
controller.set('model', model);
|
||||||
|
|
Loading…
Reference in New Issue