2024-04-15 11:51:03 -05:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2024-04-15 11:51:03 -05:00
|
|
|
*/
|
|
|
|
|
|
2024-03-22 13:35:23 -07:00
|
|
|
import Controller from '@ember/controller';
|
2025-03-12 08:29:54 -07:00
|
|
|
import { service } from '@ember/service';
|
2024-03-22 13:35:23 -07:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
|
import { action } from '@ember/object';
|
2024-06-10 17:00:57 -07:00
|
|
|
import { loading } from 'ember-loading';
|
|
|
|
|
import { confirm } from 'core/decorators/confirm';
|
|
|
|
|
import { notifySuccess, notifyError } from 'core/decorators/notify';
|
2024-03-22 13:35:23 -07:00
|
|
|
|
|
|
|
|
export default class ScopesScopeUsersIndexController extends Controller {
|
2024-06-10 17:00:57 -07:00
|
|
|
// =services
|
|
|
|
|
|
2025-12-08 18:20:55 -05:00
|
|
|
@service can;
|
2024-12-17 16:03:56 -08:00
|
|
|
@service intl;
|
|
|
|
|
@service router;
|
2025-06-02 13:26:11 -04:00
|
|
|
@tracked sortAttribute;
|
|
|
|
|
@tracked sortDirection;
|
2024-06-10 17:00:57 -07:00
|
|
|
|
2024-03-22 13:35:23 -07:00
|
|
|
// =attributes
|
|
|
|
|
|
2025-06-02 13:26:11 -04:00
|
|
|
queryParams = [
|
|
|
|
|
'search',
|
|
|
|
|
'page',
|
|
|
|
|
'pageSize',
|
|
|
|
|
'sortAttribute',
|
|
|
|
|
'sortDirection',
|
|
|
|
|
];
|
2024-03-22 13:35:23 -07:00
|
|
|
|
2025-03-18 11:20:50 -04:00
|
|
|
@tracked search;
|
2024-03-22 13:35:23 -07:00
|
|
|
@tracked page = 1;
|
|
|
|
|
@tracked pageSize = 10;
|
|
|
|
|
|
2024-12-17 16:03:56 -08:00
|
|
|
/**
|
|
|
|
|
* If can list (at least): return default welcome message.
|
|
|
|
|
* If can create (only): return create-but-not-list welcome message.
|
|
|
|
|
* If can neither list nor create: return neither-list-nor-create welcome message
|
|
|
|
|
* @type {string}
|
|
|
|
|
*/
|
|
|
|
|
get messageDescription() {
|
2025-12-08 18:20:55 -05:00
|
|
|
const canList = this.can.can('list model', this.scope, {
|
2024-12-17 16:03:56 -08:00
|
|
|
collection: 'users',
|
|
|
|
|
});
|
2025-12-08 18:20:55 -05:00
|
|
|
const canCreate = this.can.can('create model', this.scope, {
|
2024-12-17 16:03:56 -08:00
|
|
|
collection: 'users',
|
|
|
|
|
});
|
|
|
|
|
const resource = this.intl.t('resources.user.title_plural');
|
|
|
|
|
let description = 'descriptions.neither-list-nor-create';
|
|
|
|
|
|
|
|
|
|
if (canList) {
|
|
|
|
|
description = 'resources.user.description';
|
|
|
|
|
} else if (canCreate) {
|
|
|
|
|
description = 'descriptions.create-but-not-list';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.intl.t(description, { resource });
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 13:35:23 -07:00
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles input on each keystroke and the search queryParam
|
|
|
|
|
* @param {object} event
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
handleSearchInput(event) {
|
|
|
|
|
const { value } = event.target;
|
|
|
|
|
this.search = value;
|
|
|
|
|
this.page = 1;
|
|
|
|
|
}
|
2024-06-10 17:00:57 -07:00
|
|
|
|
|
|
|
|
// =actions
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rollback changes on an user.
|
|
|
|
|
* @param {UserModel} user
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
cancel(user) {
|
|
|
|
|
const { isNew } = user;
|
|
|
|
|
user.rollbackAttributes();
|
|
|
|
|
if (isNew) this.router.transitionTo('scopes.scope.users');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save an user in current scope.
|
|
|
|
|
* @param {UserModel} user
|
|
|
|
|
* @param {Event} e
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
@loading
|
|
|
|
|
@notifyError(({ message }) => message)
|
|
|
|
|
@notifySuccess(({ isNew }) =>
|
|
|
|
|
isNew ? 'notifications.create-success' : 'notifications.save-success',
|
|
|
|
|
)
|
|
|
|
|
async save(user) {
|
|
|
|
|
await user.save();
|
2025-12-08 18:20:55 -05:00
|
|
|
if (this.can.can('read model', user)) {
|
2024-06-10 17:00:57 -07:00
|
|
|
await this.router.transitionTo('scopes.scope.users.user', user);
|
|
|
|
|
} else {
|
2025-07-02 13:06:17 -07:00
|
|
|
this.router.transitionTo('scopes.scope.users');
|
2024-06-10 17:00:57 -07:00
|
|
|
}
|
|
|
|
|
await this.router.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete user in current scope and redirect to index.
|
|
|
|
|
* @param {UserModel} user
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
@loading
|
|
|
|
|
@confirm('questions.delete-confirm')
|
|
|
|
|
@notifyError(({ message }) => message, { catch: true })
|
|
|
|
|
@notifySuccess('notifications.delete-success')
|
|
|
|
|
async delete(user) {
|
|
|
|
|
await user.destroyRecord();
|
|
|
|
|
this.router.replaceWith('scopes.scope.users');
|
|
|
|
|
await this.router.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an account from the current role and redirect to accounts index.
|
|
|
|
|
* @param {UserModel} user
|
|
|
|
|
* @param {AccountModel} account
|
|
|
|
|
*/
|
|
|
|
|
@action
|
|
|
|
|
@loading
|
|
|
|
|
@notifyError(({ message }) => message, { catch: true })
|
|
|
|
|
@notifySuccess('notifications.remove-success')
|
|
|
|
|
async removeAccount(user, account) {
|
|
|
|
|
await user.removeAccount(account.id);
|
|
|
|
|
await this.router.refresh();
|
|
|
|
|
}
|
2025-06-02 13:26:11 -04:00
|
|
|
|
|
|
|
|
@action
|
|
|
|
|
sortBy(attribute, direction) {
|
|
|
|
|
this.sortAttribute = attribute;
|
|
|
|
|
this.sortDirection = direction;
|
|
|
|
|
// Reset to the first page when changing sort
|
|
|
|
|
this.page = 1;
|
|
|
|
|
}
|
2024-03-22 13:35:23 -07:00
|
|
|
}
|