From fb94deab884e60225b77dad5e19d98fc150d2699 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 5 Jan 2021 15:41:27 -0500 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20move=20auth=20me?= =?UTF-8?q?thod=20form=20to=20type-specific=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/components/form/auth-method/index.hbs | 62 ++----------------- .../form/auth-method/password/index.hbs | 57 +++++++++++++++++ 2 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 ui/admin/app/components/form/auth-method/password/index.hbs diff --git a/ui/admin/app/components/form/auth-method/index.hbs b/ui/admin/app/components/form/auth-method/index.hbs index 2704507ec..e5fa05828 100644 --- a/ui/admin/app/components/form/auth-method/index.hbs +++ b/ui/admin/app/components/form/auth-method/index.hbs @@ -1,57 +1,5 @@ - - - - - {{#if @model.errors.name}} - - {{#each @model.errors.name as |error|}} - {{error.message}} - {{/each}} - - {{/if}} - - - - {{#if @model.errors.description}} - - {{#each @model.errors.description as |error|}} - {{error.message}} - {{/each}} - - {{/if}} - - - - +{{component (concat "form/auth-method/" @model.type) + model=@model + submit=@submit + cancel=@cancel +}} diff --git a/ui/admin/app/components/form/auth-method/password/index.hbs b/ui/admin/app/components/form/auth-method/password/index.hbs new file mode 100644 index 000000000..2704507ec --- /dev/null +++ b/ui/admin/app/components/form/auth-method/password/index.hbs @@ -0,0 +1,57 @@ + + + + + {{#if @model.errors.name}} + + {{#each @model.errors.name as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + {{#if @model.errors.description}} + + {{#each @model.errors.description as |error|}} + {{error.message}} + {{/each}} + + {{/if}} + + + + From ce9a4ae9b2d8cc91cb5acaafdb95aabb791cb2b0 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 5 Jan 2021 17:41:02 -0500 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20new=20auth=20met?= =?UTF-8?q?hod=20route=20must=20be=20reached=20with=20a=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../addon/components/rose/dropdown/link/index.hbs | 2 ++ ui/admin/app/components/form/auth-method/index.hbs | 12 +++++++----- .../controllers/scopes/scope/auth-methods/new.js | 2 ++ .../app/routes/scopes/scope/auth-methods/new.js | 12 ++++++++++-- .../templates/scopes/scope/auth-methods/index.hbs | 14 +++++++++++--- ui/admin/tests/acceptance/auth-methods-test.js | 2 +- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/addons/rose/addon/components/rose/dropdown/link/index.hbs b/addons/rose/addon/components/rose/dropdown/link/index.hbs index 0213454f6..f4c2623f8 100644 --- a/addons/rose/addon/components/rose/dropdown/link/index.hbs +++ b/addons/rose/addon/components/rose/dropdown/link/index.hbs @@ -2,6 +2,7 @@ @@ -11,6 +12,7 @@ {{yield}} diff --git a/ui/admin/app/components/form/auth-method/index.hbs b/ui/admin/app/components/form/auth-method/index.hbs index e5fa05828..907940056 100644 --- a/ui/admin/app/components/form/auth-method/index.hbs +++ b/ui/admin/app/components/form/auth-method/index.hbs @@ -1,5 +1,7 @@ -{{component (concat "form/auth-method/" @model.type) - model=@model - submit=@submit - cancel=@cancel -}} +{{#if @model.type}} + {{component (concat "form/auth-method/" @model.type) + model=@model + submit=@submit + cancel=@cancel + }} +{{/if}} diff --git a/ui/admin/app/controllers/scopes/scope/auth-methods/new.js b/ui/admin/app/controllers/scopes/scope/auth-methods/new.js index ba550f456..4e562ae91 100644 --- a/ui/admin/app/controllers/scopes/scope/auth-methods/new.js +++ b/ui/admin/app/controllers/scopes/scope/auth-methods/new.js @@ -8,6 +8,8 @@ export default class ScopesScopeAuthMethodsNewController extends Controller { // =attributes + queryParams = ['type']; + /** * Translated new auth-method breadcrumb * @type {string} diff --git a/ui/admin/app/routes/scopes/scope/auth-methods/new.js b/ui/admin/app/routes/scopes/scope/auth-methods/new.js index dca9fcbdb..04ab0aacf 100644 --- a/ui/admin/app/routes/scopes/scope/auth-methods/new.js +++ b/ui/admin/app/routes/scopes/scope/auth-methods/new.js @@ -1,17 +1,25 @@ import Route from '@ember/routing/route'; export default class ScopesScopeAuthMethodsNewRoute extends Route { + // =attributes + + queryParams = { + type: { + refreshModel: true + } + }; + // =methods /** * Create a new unsaved auth-method. * @return {AuthMethodModel} */ - model() { + model(params) { const scopeModel = this.modelFor('scopes.scope'); return this.store.createRecord('auth-method', { scopeModel, - type: 'password', + type: params.type, }); } } diff --git a/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs b/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs index a796c66f5..0d10df1b5 100644 --- a/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs +++ b/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs @@ -13,9 +13,17 @@ {{#if @model}} - - {{t "titles.new"}} - + + + {{t 'resources.auth-method.types.password'}} + + {{/if}} diff --git a/ui/admin/tests/acceptance/auth-methods-test.js b/ui/admin/tests/acceptance/auth-methods-test.js index b6d4bd912..bdc509df2 100644 --- a/ui/admin/tests/acceptance/auth-methods-test.js +++ b/ui/admin/tests/acceptance/auth-methods-test.js @@ -44,7 +44,7 @@ module('Acceptance | auth methods', function (hooks) { // Generate route URLs for resources urls.orgScope = `/scopes/${instances.scopes.org.id}`; urls.authMethods = `${urls.orgScope}/auth-methods`; - urls.newAuthMethod = `${urls.authMethods}/new`; + urls.newAuthMethod = `${urls.authMethods}/new?type=password`; urls.authMethod = `${urls.authMethods}/${instances.authMethod.id}`; }); From 2af8d7efb8e12ddcd395d01a3d6f97a7e1e52d59 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 6 Jan 2021 12:53:57 -0500 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20lookup=20authent?= =?UTF-8?q?icator=20by=20auth=20method=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/core/addon/decorators/notify.js | 23 +++++++-------- .../scopes/scope/authenticate/method.js | 29 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/addons/core/addon/decorators/notify.js b/addons/core/addon/decorators/notify.js index 42204023f..1ce13d91e 100644 --- a/addons/core/addon/decorators/notify.js +++ b/addons/core/addon/decorators/notify.js @@ -50,25 +50,22 @@ export function notifyError(notification, options = { catch: false }) { desc.value = async function () { const owner = getOwner(this); const notifyService = owner.lookup('service:notify'); - //const intlService = owner.lookup('service:intl'); + const intlService = owner.lookup('service:intl'); + try { return await method.apply(this, arguments); } catch (error) { - // As of now all error notifications pass a function, but this code - // could support either a function or a string. - // const candidateKey = (typeof notification === 'function') - // ? notification.apply(this, [error]) - // : notification; - const candidateKey = notification.apply(this, [error]); - // As of now, we only use untranslated strings for error notifications, - // but this code would support both translated and arbitrary strings. - // const text = intlService.exists(candidateKey) - // ? intlService.t(candidateKey) - // : candidateKey; - const text = candidateKey; + const candidateKey = (typeof notification === 'function') + ? notification.apply(this, [error]) + : notification; + + const text = intlService.exists(candidateKey) + ? intlService.t(candidateKey) + : candidateKey; notifyService.error(text, { closeAfter: null }); + if (options.catch) { // squelch the error } else { diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method.js b/ui/admin/app/routes/scopes/scope/authenticate/method.js index 7f9f87856..1caa35d95 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method.js @@ -2,6 +2,7 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; import { action } from '@ember/object'; import loading from 'ember-loading/decorator'; +import { notifyError } from 'core/decorators/notify'; export default class ScopesScopeAuthenticateMethodRoute extends Route { // =services @@ -35,28 +36,24 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { // =actions /** - * Delegates authentication to the `password` authenticator. - * If authentication fails, notifies with an alert. + * Delegates authentication to an authenticator of type matching the current + * auth method. If authentication fails, notifies with an alert. */ @action @loading + @notifyError(() => 'errors.authentication-failed.title', { catch: true }) async authenticate(creds) { this.resetCredentials(); - const authenticatorName = 'authenticator:password'; const scope = this.modelFor('scopes.scope'); - const authMethod = this.currentModel; + const authMethod = this.modelFor('scopes.scope.authenticate.method'); + const authenticatorName = `authenticator:${authMethod.type}`; const requestCookies = false; - try { - await this.session.authenticate( - authenticatorName, - creds, - requestCookies, - { scope, authMethod } - ); - this.transitionTo('index'); - } catch (e) { - const errorMessage = this.intl.t('errors.authentication-failed.title'); - this.notify.error(errorMessage, { closeAfter: null }); - } + await this.session.authenticate( + authenticatorName, + creds, + requestCookies, + { scope, authMethod } + ); + this.transitionTo('index'); } } From 8e3551ddaec06b14cb7a85fd5e479ffa78b35647 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 6 Jan 2021 13:46:22 -0500 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20authenticate=20r?= =?UTF-8?q?oute=20and=20forms=20to=20be=20type=20agnostic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/form/authenticate/index.hbs | 7 ++++ .../form/authenticate/password/index.hbs | 28 ++++++++++++++ .../form/authenticate/password/index.js | 37 +++++++++++++++++++ .../scopes/scope/authenticate/method.js | 11 ------ .../scopes/scope/authenticate/method.hbs | 31 ++-------------- 5 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 ui/admin/app/components/form/authenticate/index.hbs create mode 100644 ui/admin/app/components/form/authenticate/password/index.hbs create mode 100644 ui/admin/app/components/form/authenticate/password/index.js diff --git a/ui/admin/app/components/form/authenticate/index.hbs b/ui/admin/app/components/form/authenticate/index.hbs new file mode 100644 index 000000000..e7ca752c2 --- /dev/null +++ b/ui/admin/app/components/form/authenticate/index.hbs @@ -0,0 +1,7 @@ +{{#if @model.type}} + {{component (concat "form/authenticate/" @model.type) + submit=@submit + cancel=false + disabled=@disabled + }} +{{/if}} diff --git a/ui/admin/app/components/form/authenticate/password/index.hbs b/ui/admin/app/components/form/authenticate/password/index.hbs new file mode 100644 index 000000000..ee47910d8 --- /dev/null +++ b/ui/admin/app/components/form/authenticate/password/index.hbs @@ -0,0 +1,28 @@ + + + + + + + + + diff --git a/ui/admin/app/components/form/authenticate/password/index.js b/ui/admin/app/components/form/authenticate/password/index.js new file mode 100644 index 000000000..d6336f382 --- /dev/null +++ b/ui/admin/app/components/form/authenticate/password/index.js @@ -0,0 +1,37 @@ +import Component from '@glimmer/component'; +import { tracked } from '@glimmer/tracking'; +import { action } from '@ember/object'; +import { run } from '@ember/runloop'; + +export default class FormAuthenticatePasswordComponent extends Component { + // =properties + + /** + * New password property + * @type {string} + */ + @tracked password; + + // =methods + + /** + * Unsets the password field. + */ + resetPassword() { + this.password = null; + } + + // =actions + + /** + * Submit credentials, but reset the password first in case of failure. + * Callback with no arguments otherwise. + * @param {function} fn + * @param {object} creds + */ + @action + submit(fn, creds) { + run(() => this.resetPassword()); + run(() => fn(creds)); + } +} diff --git a/ui/admin/app/routes/scopes/scope/authenticate/method.js b/ui/admin/app/routes/scopes/scope/authenticate/method.js index 1caa35d95..cfb2c5acf 100644 --- a/ui/admin/app/routes/scopes/scope/authenticate/method.js +++ b/ui/admin/app/routes/scopes/scope/authenticate/method.js @@ -23,16 +23,6 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { return this.store.findRecord('auth-method', id, adapterOptions); } - /** - * Resets the password field on the controller so it is not - * inadvertantly retained in memory. - */ - resetCredentials() { - this.controller.setProperties({ - password: null, - }); - } - // =actions /** @@ -43,7 +33,6 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route { @loading @notifyError(() => 'errors.authentication-failed.title', { catch: true }) async authenticate(creds) { - this.resetCredentials(); const scope = this.modelFor('scopes.scope'); const authMethod = this.modelFor('scopes.scope.authenticate.method'); const authenticatorName = `authenticator:${authMethod.type}`; diff --git a/ui/admin/app/templates/scopes/scope/authenticate/method.hbs b/ui/admin/app/templates/scopes/scope/authenticate/method.hbs index 709c1f491..6428c8118 100644 --- a/ui/admin/app/templates/scopes/scope/authenticate/method.hbs +++ b/ui/admin/app/templates/scopes/scope/authenticate/method.hbs @@ -1,28 +1,5 @@ - - - - - - - - - +/> From 9315eada5543da43b5696cf3398891f12e4b8d70 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 6 Jan 2021 14:30:14 -0500 Subject: [PATCH 5/5] =?UTF-8?q?chore:=20=F0=9F=A4=96=20authenticate=20form?= =?UTF-8?q?=20should=20show=20name=20or=20type=20in=20tabs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/admin/app/templates/scopes/scope/authenticate.hbs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ui/admin/app/templates/scopes/scope/authenticate.hbs b/ui/admin/app/templates/scopes/scope/authenticate.hbs index 68d98634c..49af65143 100644 --- a/ui/admin/app/templates/scopes/scope/authenticate.hbs +++ b/ui/admin/app/templates/scopes/scope/authenticate.hbs @@ -29,7 +29,11 @@ {{#each @model.authMethods as |authMethod|}} - {{t (concat "resources.auth-method.types." authMethod.type)}} + {{#if authMethod.name}} + {{authMethod.name}} + {{else}} + {{t (concat "resources.auth-method.types." authMethod.type)}} + {{/if}} {{/each}}