refactor: 💡 remove route-action usage for roles (#2292)

* refactor: 💡 remove route-action usage for roles

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12608
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-05-23 15:35:49 -07:00
1 parent 534c4419e7
commit b9b4e33f5a
23 files changed
+466 -182

No files matched your search

@@ -6,13 +6,17 @@
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';
export default class ScopesScopeRolesIndexController extends Controller {
// =services
@service intl;
@service can;
@service router;
// =attributes
@@ -22,7 +26,7 @@ export default class ScopesScopeRolesIndexController extends Controller {
@tracked page = 1;
@tracked pageSize = 10;
// =methods
// =actions
/**
* Handles input on each keystroke and the search queryParam
@@ -35,4 +39,65 @@ export default class ScopesScopeRolesIndexController extends Controller {
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();
}
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeRolesNewController extends Controller {
@controller('scopes/scope/roles/index') roles;
}
@@ -4,10 +4,16 @@
*/
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeRolesRoleAddPrincipalsController extends Controller {
// =services
@service router;
// =attributes
/**
@@ -26,4 +32,28 @@ export default class ScopesScopeRolesRoleAddPrincipalsController extends Control
const sorted = [global, ...orgsAndProjects].flat();
return sorted;
}
// =actions
/**
* Save principal IDs to current role via the API.
* @param {RoleModel} role
* @param {[string]} principalIDs
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async addPrincipals(role, principalIDs) {
await role.addPrincipals(principalIDs);
this.router.replaceWith('scopes.scope.roles.role.principals');
}
/**
* Redirect to role principals as if nothing ever happened.
*/
@action
cancel() {
this.router.replaceWith('scopes.scope.roles.role.principals');
}
}
@@ -0,0 +1,63 @@
import Controller, { inject as controller } from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeRolesRoleGrantsController extends Controller {
@controller('scopes/scope/roles/index') roles;
// =services
@service router;
// =actions
/**
* Adds a new grant to the role at the beginning of the grants list.
* Grant creation is not immediately permanent; users may rollback the change
* via "cancel" or commit it via "save".
* @param {RoleModel} role
* @param {string} grantString
*/
@action
addGrant(role, grantString) {
role.grant_strings = [grantString].concat(role.grant_strings);
}
/**
* Removes a grant from the role. Grant removal is not immediately permanent;
* users may rollback the change via "cancel" or commit it via "save".
* @param {RoleModel} role
* @param {string} grantString
*/
@action
removeGrant(role, grantString) {
role.grant_strings = role.grant_strings.filter(
(str) => str !== grantString,
);
}
/**
* Save a role.
* @param {RoleModel} role
* @param {[string]} grantStrings
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.save-success')
async save(role, grantStrings) {
await role.saveGrantStrings(grantStrings);
}
/**
* Rollback changes on a role.
* @param {RoleModel} role
*/
@action
async cancel(role) {
role.rollbackAttributes();
await this.router.refresh('scopes.scope.roles.role');
}
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeRolesRoleIndexController extends Controller {
@controller('scopes/scope/roles/index') roles;
}
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeRolesRolePrincipalsController extends Controller {
@controller('scopes/scope/roles/index') roles;
}