Files
threshold-ui/ui/admin/app/controllers/scopes/scope/roles/index.js
T
Lisbet Alvarez 45931be4da refactor: 💡 add grant scopes applied badge (#2374)
* refactor: 💡 add grant scopes applied badge

* test: 💍 add extra asserts
2024-07-15 17:23:00 -07:00

107 lines
2.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { tracked } from '@glimmer/tracking';
import { debounce } from 'core/decorators/debounce';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { GRANT_SCOPE_THIS } from 'api/models/role';
export default class ScopesScopeRolesIndexController extends Controller {
// =services
@service can;
@service router;
// =attributes
queryParams = ['search', 'page', 'pageSize'];
@tracked search = '';
@tracked page = 1;
@tracked pageSize = 10;
grantScopeThis = GRANT_SCOPE_THIS;
// =actions
/**
* Handles input on each keystroke and the search queryParam
* @param {object} event
*/
@action
@debounce(250)
handleSearchInput(event) {
const { value } = event.target;
this.search = value;
this.page = 1;
}
/**
* Rollback changes on an role.
* @param {RoleModel} role
*/
@action
cancel(role) {
const { isNew } = role;
role.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.roles');
}
/**
* Save an role in current scope.
* @param {RoleModel} role
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(role) {
await role.save();
if (this.can.can('read model', role)) {
await this.router.transitionTo('scopes.scope.roles.role', role);
} else {
await this.router.transitionTo('scopes.scope.roles');
}
await this.router.refresh();
}
/**
* Delete role in current scope and redirect to index.
* @param {RoleModel} role
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(role) {
await role.destroyRecord();
this.router.replaceWith('scopes.scope.roles');
await this.router.refresh();
}
/**
* Remove a principal from the current role and redirect to principals index.
* @param {RoleModel} role
* @param {UserModel, GroupModel, ManagedGroupModel} principal
*/
@action
@loading
@confirm('questions.remove-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.remove-success')
async removePrincipal(role, principal) {
await role.removePrincipal(principal.id);
await this.router.refresh();
}
}