Files
threshold-ui/ui/admin/app/routes/scopes/scope/auth-methods/auth-method/accounts.js
T

103 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-09-14 15:58:06 -04:00
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
2022-01-03 09:28:24 -05:00
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
2020-09-14 15:58:06 -04:00
export default class ScopesScopeAuthMethodsAuthMethodAccountsRoute extends Route {
// =services
@service intl;
@service notify;
2021-09-03 10:22:54 -07:00
@service can;
@service router;
2020-09-14 15:58:06 -04:00
// =methods
/**
2020-09-16 09:48:07 -04:00
* Returns accounts for the current auth method.
2020-09-14 15:58:06 -04:00
* @return {Promise{[AccountModel]}}
*/
2022-01-14 16:56:59 -05:00
async model() {
2021-09-03 10:22:54 -07:00
const authMethod = this.modelFor('scopes.scope.auth-methods.auth-method');
const { id: auth_method_id } = authMethod;
2022-01-14 16:56:59 -05:00
let accounts;
2021-09-03 10:22:54 -07:00
if (
this.can.can('list collection', authMethod, {
collection: 'accounts',
})
) {
2022-01-14 16:56:59 -05:00
accounts = await this.store.query('account', { auth_method_id });
2021-09-03 10:22:54 -07:00
}
2022-01-14 16:56:59 -05:00
return {
authMethod,
accounts,
};
2020-09-14 15:58:06 -04:00
}
// =actions
2020-09-16 09:48:07 -04:00
/**
* Rollback changes on an account.
* @param {AccountModel} account
*/
@action
cancel(account) {
2020-09-14 17:43:07 -04:00
const { isNew } = account;
account.rollbackAttributes();
2020-09-16 14:33:04 -04:00
if (isNew)
this.router.transitionTo(
'scopes.scope.auth-methods.auth-method.accounts'
);
}
/**
* Save an account in current scope.
* @param {AccountModel} account
*/
@action
@loading
@notifyError(({ message }) => message)
2020-11-16 15:08:59 -05:00
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success'
)
async save(account, password) {
2020-09-14 17:43:07 -04:00
const { isNew } = account;
const adapterOptions = {};
2020-09-16 14:33:04 -04:00
if (isNew) {
adapterOptions.password = password;
}
await account.save({ adapterOptions });
if (this.can.can('read model', account)) {
await this.router.transitionTo(
'scopes.scope.auth-methods.auth-method.accounts.account',
account
);
} else {
await this.router.transitionTo(
'scopes.scope.auth-methods.auth-method.accounts'
);
}
this.refresh();
}
2020-09-14 17:12:51 -04:00
/**
* Delete an account in current scope and redirect to index
* @param {AccountModel} account
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async deleteAccount(account) {
await account.destroyRecord();
await this.router.replaceWith(
'scopes.scope.auth-methods.auth-method.accounts'
);
this.refresh();
2020-09-14 17:12:51 -04:00
}
2020-09-14 15:58:06 -04:00
}