Files
threshold-ui/addons/api/mirage/factories/auth-method.js
T

72 lines
2.0 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-06-18 15:51:40 -04:00
import factory from '../generated/factories/auth-method';
2023-01-09 08:48:49 -08:00
import { trait } from 'miragejs';
2021-08-16 13:31:28 -07:00
import permissions from '../helpers/permissions';
2022-06-01 14:02:18 -04:00
import generateId from '../helpers/id';
2020-06-18 15:51:40 -04:00
import {
TYPE_AUTH_METHOD_PASSWORD,
TYPE_AUTH_METHOD_OIDC,
2023-07-18 17:47:20 -04:00
TYPE_AUTH_METHOD_LDAP,
TYPES_AUTH_METHOD,
} from 'api/models/auth-method';
2021-01-15 11:10:33 -05:00
2020-09-14 11:26:54 -04:00
export default factory.extend({
2021-08-16 13:31:28 -07:00
authorized_actions: () =>
2021-08-17 08:47:16 -07:00
permissions.authorizedActionsFor('auth-method') || [
2021-08-16 13:31:28 -07:00
'no-op',
'read',
'update',
'delete',
'authenticate',
2021-08-16 13:31:28 -07:00
],
authorized_collection_actions() {
switch (this.type) {
case TYPE_AUTH_METHOD_PASSWORD:
return { accounts: ['create', 'list'] };
case TYPE_AUTH_METHOD_LDAP:
case TYPE_AUTH_METHOD_OIDC:
return {
accounts: ['create', 'list'],
'managed-groups': ['create', 'list'],
};
}
2021-09-03 10:22:54 -07:00
},
2022-06-01 14:02:18 -04:00
id: () => generateId('am_'),
2020-09-14 11:26:54 -04:00
2021-01-15 11:10:33 -05:00
// Cycle through available types
type: (i) => TYPES_AUTH_METHOD[i % TYPES_AUTH_METHOD.length],
2021-01-15 11:10:33 -05:00
2020-09-14 11:26:54 -04:00
/**
* Adds accounts (with associated users) to auth method and managed groups.
2020-09-14 11:26:54 -04:00
*/
withAccountsAndUsersAndManagedGroups: trait({
2020-09-14 11:26:54 -04:00
afterCreate(authMethod, server) {
2021-06-17 14:09:31 -04:00
const { scope, type } = authMethod;
2021-03-04 15:47:38 -05:00
server.createList('user', 5, { scope }).map((user) => {
2021-06-17 14:09:31 -04:00
const { id } = server.create('account', { scope, type, authMethod });
2020-09-30 10:45:39 -04:00
user.update({ accountIds: [id] });
});
2023-07-18 17:47:20 -04:00
if (type === TYPE_AUTH_METHOD_OIDC || type === TYPE_AUTH_METHOD_LDAP) {
2021-11-12 11:22:16 -05:00
server
.createList('managed-group', 2, { scope, authMethod })
.map((managedGroup) => {
const accounts = server.createList('account', 2, {
scope,
type,
authMethod,
});
const accountIds = accounts.map((account) => account.id);
managedGroup.update({ memberIds: accountIds });
});
}
2021-03-04 15:47:38 -05:00
},
}),
2020-09-14 11:26:54 -04:00
});