Files
threshold-ui/ui/admin/app/routes/scopes/scope/authenticate.js
T
Cameron Perera d2d332b7d1 refactor: 💡 only show scopes with auth methods at authenticate (#2129)
* refactor: 💡 only show scopes with auth methods at authenticate

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* test: 💍 verify scopes with no auth methods are hidden

when authenticating

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* refactor: 💡 update logic for fetching scopes and auth methods

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* refactor: 💡 PR feedback changes

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* refactor: 💡 added unless block back to template

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* refactor: 💡 update admin authenticate route and template

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355

* test: 💍 add test for orgs with no auth methods admin ui

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-2355
2024-02-08 10:01:20 -06:00

64 lines
1.6 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class ScopesScopeAuthenticateRoute extends Route {
// =services
@service session;
@service router;
@service resourceFilterStore;
// =methods
beforeModel() {
if (this.session.isAuthenticated)
this.router.transitionTo('scopes.scope.index');
}
/**
* Returns all auth methods for the current scope, along with the current
* scope and all scopes (for org navigation).
* @return {Promise} `{scope, scopes, authMethods}`
*/
async model() {
const { id: scope_id } = this.modelFor('scopes.scope');
// Preload all authenticatable auth methods into the store
const authMethodsForAllScopes = await this.resourceFilterStore.queryBy(
'auth-method',
{
authorized_actions: [{ contains: 'authenticate' }],
},
{
scope_id: 'global',
recursive: true,
},
);
const scopeIDs = new Set(
authMethodsForAllScopes.map((authMethod) => authMethod.scopeID),
);
// Fetch org scopes and filter out any that have no auth methods
const scopes = this.modelFor('scopes').filter(({ id: scope_id }) =>
scopeIDs.has(scope_id),
);
// Filter out auth methods that are not for the current scope
const authMethods = authMethodsForAllScopes.filter(
(authMethod) => authMethod.scopeID === scope_id,
);
return {
scope: this.modelFor('scopes.scope'),
scopes,
authMethods,
};
}
}