refactor: 💡 remove route-action usage for auth-methods (#2178)

* refactor: 💡 remove route-action usage for auth-methods

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12603
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-04-22 10:33:06 -07:00
1 parent f65f483611
commit 148f2db941
22 files changed
+469 -276

No files matched your search

@@ -23,7 +23,7 @@
<form.radioGroup
@name='theme'
@selectedValue={{@model.state}}
@changed={{route-action 'changeState'}}
@changed={{fn @changeState @model}}
as |radioGroup|
>
{{#each @model.options.state as |state|}}
@@ -48,14 +48,14 @@
{{#if @model.isPrimary}}
<dropdown.button
@disabled={{@model.canSave}}
{{on 'click' (route-action 'removeAsPrimary' @model)}}
{{on 'click' (fn @removeAsPrimary @model)}}
>
{{t 'resources.auth-method.actions.remove-as-primary'}}
</dropdown.button>
{{else}}
<dropdown.button
@disabled={{@model.canSave}}
{{on 'click' (route-action 'makePrimary' @model)}}
{{on 'click' (fn @makePrimary @model)}}
>
{{t 'resources.auth-method.actions.make-primary'}}
</dropdown.button>
@@ -86,7 +86,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
{{on 'click' (fn @delete @model)}}
>
{{t 'resources.auth-method.actions.delete'}}
</dropdown.button>
@@ -3,7 +3,7 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { loading } from 'ember-loading';
@@ -11,6 +11,8 @@ import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeAuthMethodsAuthMethodAccountsIndexController extends Controller {
@controller('scopes/scope/auth-methods/index') authMethods;
// =services
@service can;
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeAuthMethodsAuthMethodIndexController extends Controller {
@controller('scopes/scope/auth-methods/index') authMethods;
}
@@ -3,7 +3,7 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { loading } from 'ember-loading';
@@ -11,6 +11,8 @@ import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeAuthMethodsAuthMethodManagedGroupsIndexController extends Controller {
@controller('scopes/scope/auth-methods/index') authMethods;
// =services
@service can;
@@ -5,14 +5,19 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { tracked } from '@glimmer/tracking';
import { debounce } from 'core/decorators/debounce';
import { TYPES_AUTH_METHOD } from 'api/models/auth-method';
export default class ScopesScopeAuthMethodsIndexController extends Controller {
// =services
@service router;
@service can;
@service intl;
// =attributes
@@ -92,6 +97,111 @@ export default class ScopesScopeAuthMethodsIndexController extends Controller {
this.page = 1;
}
/**
* Rollback changes to an auth-method.
* @param {AuthMethodModel} authMethod
*/
@action
cancel(authMethod) {
const { isNew } = authMethod;
authMethod.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.auth-methods');
}
/**
* Save an auth-method in current scope.
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(authMethod) {
await authMethod.save();
if (this.can.can('read model', authMethod)) {
await this.router.transitionTo(
'scopes.scope.auth-methods.auth-method',
authMethod,
);
} else {
await this.router.transitionTo('scopes.scope.auth-methods');
}
await this.router.refresh();
}
/**
* Delete an auth method in current scope and redirect to index
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(authMethod) {
await authMethod.destroyRecord();
// Reload the scope, since this is where the primary_auth_method_id is
// stored. An auth method deletion could affect this field.
await this.scopeModel.reload();
this.router.replaceWith('scopes.scope.auth-methods');
await this.router.refresh();
}
/**
* Elects the specified auth method to primary for the current scope.
* @param {AuthMethodModel} authMethod
* @param {string} authMethod.id
*/
@action
@loading
@confirm('resources.auth-method.questions.make-primary-confirm', {
title: 'resources.auth-method.questions.make-primary',
})
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('resources.auth-method.notifications.make-primary-success')
async makePrimary({ id }) {
this.scopeModel.primary_auth_method_id = id;
// Attempt to save the change to the scope. If this operation fails,
// we rollback the change and rethrow the error so the user can be notified.
try {
await this.scopeModel.save();
} catch (e) {
this.scopeModel.rollbackAttributes();
throw e;
}
await this.router.refresh();
}
/**
* Sets the `primary_auth_method_id` field to null for the current scope
* and saves it if and only if the specified auth method is in fact primary
* for the current scope.
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@confirm('resources.auth-method.questions.remove-as-primary-confirm', {
title: 'resources.auth-method.questions.remove-as-primary',
})
@notifyError(({ message }) => message, { catch: true })
@notifySuccess(
'resources.auth-method.notifications.remove-as-primary-success',
)
async removeAsPrimary(authMethod) {
this.scopeModel.primary_auth_method_id = null;
// Attempt to save the change to the scope. If this operation fails,
// we rollback the change and rethrow the error so the user can be notified.
try {
await this.scopeModel.save();
} catch (e) {
this.scopeModel.rollbackAttributes();
throw e;
}
await authMethod.reload();
}
/**
* Removes an item from array `property` at `index` on the
* passed `authMethod`. This is used to manage entries in fragment array
@@ -101,7 +211,7 @@ export default class ScopesScopeAuthMethodsIndexController extends Controller {
* @param {number} index
*/
@action
async removeItemByIndex(authMethod, property, index) {
removeItemByIndex(authMethod, property, index) {
const array = authMethod.get(property).filter((item, i) => i !== index);
authMethod.set(property, array);
}
@@ -115,9 +225,78 @@ export default class ScopesScopeAuthMethodsIndexController extends Controller {
* @param {string} value
*/
@action
async addStringItem(authMethod, property, value) {
addStringItem(authMethod, property, value) {
const existingArray = authMethod[property] ?? [];
const array = [...existingArray, { value }];
authMethod.set(property, array);
}
/**
* Adds an account map fragment to the passed `authMethod`.
* @param {AuthMethodModel} authMethod
* @param {string} field
* @param {string} from
* @param {string} to
*/
@action
addAccountMapItem(authMethod, field, from, to) {
const existingArray = authMethod[field] ?? [];
const array = [...existingArray, { from, to }];
authMethod.set(field, array);
}
/**
* Copies the contents of string array fields in order to force the instance
* into a dirty state. This ensures that `model.rollbackAttributes()` reverts
* to the original expected array.
*
* The deep copy implemented here is required to ensure that both the
* array itself and its members are all new.
*
* @param {authMethodModel} authMethod
*/
@action
edit(authMethod) {
if (authMethod.claims_scopes) {
authMethod.claims_scopes = structuredClone(authMethod.claims_scopes);
}
if (authMethod.signing_algorithms) {
authMethod.signing_algorithms = structuredClone(
authMethod.signing_algorithms,
);
}
if (authMethod.allowed_audiences) {
authMethod.allowed_audiences = structuredClone(
authMethod.allowed_audiences,
);
}
if (authMethod.idp_ca_certs) {
authMethod.idp_ca_certs = structuredClone(authMethod.idp_ca_certs);
}
if (authMethod.account_claim_maps) {
authMethod.account_claim_maps = structuredClone(
authMethod.account_claim_maps,
);
}
if (authMethod.certificates) {
authMethod.certificates = structuredClone(authMethod.certificates);
}
if (authMethod.account_attribute_maps) {
authMethod.account_attribute_maps = structuredClone(
authMethod.account_attribute_maps,
);
}
}
/**
* Update state of OIDC or LDAP auth method
* @param {string} state
* @param {authMethodModel} authMethod
*/
@action
@notifyError(({ message }) => message)
@notifySuccess('notifications.save-success')
async changeState(authMethod, state) {
await authMethod.changeState(state);
}
}
@@ -3,9 +3,11 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeAuthMethodsNewController extends Controller {
@controller('scopes/scope/auth-methods/index') authMethods;
// =services
// =attributes
@@ -5,16 +5,11 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeAuthMethodsRoute extends Route {
// =services
@service session;
@service can;
@service router;
// =methods
@@ -25,157 +20,4 @@ export default class ScopesScopeAuthMethodsRoute extends Route {
beforeModel() {
if (!this.session.isAuthenticated) this.router.transitionTo('index');
}
// =actions
/**
* Rollback changes to an auth-method.
* @param {AuthMethodModel} authMethod
*/
@action
cancel(authMethod) {
const { isNew } = authMethod;
authMethod.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.auth-methods');
}
/**
* Save an auth-method in current scope.
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(authMethod) {
await authMethod.save();
if (this.can.can('read model', authMethod)) {
await this.router.transitionTo(
'scopes.scope.auth-methods.auth-method',
authMethod,
);
} else {
await this.router.transitionTo('scopes.scope.auth-methods');
}
this.refresh();
}
/**
* Delete an auth method in current scope and redirect to index
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(authMethod) {
const scopeModel = this.modelFor('scopes.scope');
await authMethod.destroyRecord();
// Reload the scope, since this is where the primary_auth_method_id is
// stored. An auth method deletion could affect this field.
await scopeModel.reload();
this.router.replaceWith('scopes.scope.auth-methods');
this.refresh();
}
/**
* Elects the specified auth method to primary for the current scope.
* @param {AuthMethodModel} authMethod
* @param {string} authMethod.id
*/
@action
@loading
@confirm('resources.auth-method.questions.make-primary-confirm', {
title: 'resources.auth-method.questions.make-primary',
})
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('resources.auth-method.notifications.make-primary-success')
async makePrimary({ id }) {
const scopeModel = this.modelFor('scopes.scope');
scopeModel.primary_auth_method_id = id;
// Attempt to save the change to the scope. If this operation fails,
// we rollback the change and rethrow the error so the user can be notified.
try {
await scopeModel.save();
} catch (e) {
scopeModel.rollbackAttributes();
throw e;
}
await this.refresh();
}
/**
* Sets the `primary_auth_method_id` field to null for the current scope
* and saves it if and only if the specified auth method is in fact primary
* for the current scope.
* @param {AuthMethodModel} authMethod
*/
@action
@loading
@confirm('resources.auth-method.questions.remove-as-primary-confirm', {
title: 'resources.auth-method.questions.remove-as-primary',
})
@notifyError(({ message }) => message, { catch: true })
@notifySuccess(
'resources.auth-method.notifications.remove-as-primary-success',
)
async removeAsPrimary(authMethod) {
const scopeModel = this.modelFor('scopes.scope');
scopeModel.primary_auth_method_id = null;
// Attempt to save the change to the scope. If this operation fails,
// we rollback the change and rethrow the error so the user can be notified.
try {
await scopeModel.save();
} catch (e) {
scopeModel.rollbackAttributes();
throw e;
}
await authMethod.reload();
}
/**
* Removes an item from array `property` at `index` on the
* passed `authMethod`. This is used to manage entries in fragment array
* fields such as `signing_algorithms`.
* @param {AuthMethodModel} authMethod
* @param {string} property
* @param {number} index
*/
@action
async removeItemByIndex(authMethod, property, index) {
const array = authMethod.get(property).filter((item, i) => i !== index);
authMethod.set(property, array);
}
/**
* Adds a string item to array `property` on the passed `authMethod`.
* This is used to manage entries in fragment OIDC string array fields such
* as `signing_algorithms`.
* @param {AuthMethodModel} authMethod
* @param {string} property
* @param {string} value
*/
@action
async addStringItem(authMethod, property, value) {
const existingArray = authMethod[property] ?? [];
const array = [...existingArray, { value }];
authMethod.set(property, array);
}
/**
* Adds an account map fragment to the passed `authMethod`.
* @param {AuthMethodModel} authMethod
* @param {string} field
* @param {string} from
* @param {string} to
*/
@action
async addAccountMapItem(authMethod, field, from, to) {
const existingArray = authMethod[field] ?? [];
const array = [...existingArray, { from, to }];
authMethod.set(field, array);
}
}
@@ -4,8 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
import { paramValueFinder } from 'admin/utils/param-value-finder';
@@ -47,61 +45,4 @@ export default class ScopesScopeAuthMethodsAuthMethodRoute extends Route {
);
}
}
// =actions
/**
* Copies the contents of string array fields in order to force the instance
* into a dirty state. This ensures that `model.rollbackAttributes()` reverts
* to the original expected array.
*
* The deep copy implemented here is required to ensure that both the
* array itself and its members are all new.
*
* @param {authMethodModel} authMethod
*/
@action
edit(authMethod) {
if (authMethod.claims_scopes) {
authMethod.claims_scopes = structuredClone(authMethod.claims_scopes);
}
if (authMethod.signing_algorithms) {
authMethod.signing_algorithms = structuredClone(
authMethod.signing_algorithms,
);
}
if (authMethod.allowed_audiences) {
authMethod.allowed_audiences = structuredClone(
authMethod.allowed_audiences,
);
}
if (authMethod.idp_ca_certs) {
authMethod.idp_ca_certs = structuredClone(authMethod.idp_ca_certs);
}
if (authMethod.account_claim_maps) {
authMethod.account_claim_maps = structuredClone(
authMethod.account_claim_maps,
);
}
if (authMethod.certificates) {
authMethod.certificates = structuredClone(authMethod.certificates);
}
if (authMethod.account_attribute_maps) {
authMethod.account_attribute_maps = structuredClone(
authMethod.account_attribute_maps,
);
}
}
/**
* Update state of OIDC or LDAP auth method
* @param {string} state
*/
@action
@notifyError(({ message }) => message)
@notifySuccess('notifications.save-success')
async changeState(state) {
const model = this.modelFor('scopes.scope.auth-methods.auth-method');
await model.changeState(state);
}
}
@@ -18,7 +18,13 @@
</page.navigation>
<page.actions>
<AuthMethods::AuthMethod::Actions @model={{@model.authMethod}} />
<AuthMethods::AuthMethod::Actions
@model={{@model.authMethod}}
@changeState={{this.authMethods.changeState}}
@removeAsPrimary={{this.authMethods.removeAsPrimary}}
@makePrimary={{this.authMethods.makePrimary}}
@delete={{this.authMethods.delete}}
/>
</page.actions>
<page.body>
@@ -18,18 +18,24 @@
</page.navigation>
<page.actions>
<AuthMethods::AuthMethod::Actions @model={{@model}} />
<AuthMethods::AuthMethod::Actions
@model={{@model}}
@changeState={{this.authMethods.changeState}}
@removeAsPrimary={{this.authMethods.removeAsPrimary}}
@makePrimary={{this.authMethods.makePrimary}}
@delete={{this.authMethods.delete}}
/>
</page.actions>
<page.body>
<Form::AuthMethod
@model={{@model}}
@edit={{route-action 'edit' @model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@removeItemByIndex={{route-action 'removeItemByIndex' @model}}
@addStringItem={{route-action 'addStringItem' @model}}
@addAccountMapItem={{route-action 'addAccountMapItem' @model}}
@edit={{fn this.authMethods.edit @model}}
@submit={{fn this.authMethods.save @model}}
@cancel={{fn this.authMethods.cancel @model}}
@removeItemByIndex={{fn this.authMethods.removeItemByIndex @model}}
@addStringItem={{fn this.authMethods.addStringItem @model}}
@addAccountMapItem={{fn this.authMethods.addAccountMapItem @model}}
/>
</page.body>
@@ -18,7 +18,13 @@
</page.navigation>
<page.actions>
<AuthMethods::AuthMethod::Actions @model={{@model.authMethod}} />
<AuthMethods::AuthMethod::Actions
@model={{@model.authMethod}}
@changeState={{this.authMethods.changeState}}
@removeAsPrimary={{this.authMethods.removeAsPrimary}}
@makePrimary={{this.authMethods.makePrimary}}
@delete={{this.authMethods.delete}}
/>
</page.actions>
<page.body>
@@ -149,7 +149,7 @@
<dd.Interactive
@text={{t 'resources.auth-method.actions.make-primary'}}
data-test-make-primary-action
{{on 'click' (route-action 'makePrimary' B.data)}}
{{on 'click' (fn this.makePrimary B.data)}}
/>
{{/unless}}
{{#if B.data.isPrimary}}
@@ -157,7 +157,7 @@
@text={{t
'resources.auth-method.actions.remove-as-primary'
}}
{{on 'click' (route-action 'removeAsPrimary' B.data)}}
{{on 'click' (fn this.removeAsPrimary B.data)}}
/>
{{/if}}
</Hds::Dropdown>
@@ -25,11 +25,11 @@
<page.body>
<Form::AuthMethod
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@removeItemByIndex={{route-action 'removeItemByIndex' @model}}
@addStringItem={{route-action 'addStringItem' @model}}
@addAccountMapItem={{route-action 'addAccountMapItem' @model}}
@submit={{fn this.authMethods.save @model}}
@cancel={{fn this.authMethods.cancel @model}}
@removeItemByIndex={{fn this.authMethods.removeItemByIndex @model}}
@addStringItem={{fn this.authMethods.addStringItem @model}}
@addAccountMapItem={{fn this.authMethods.addAccountMapItem @model}}
/>
</page.body>
</Rose::Layout::Page>