import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; import { action } from '@ember/object'; import { loading } from 'ember-loading'; import { confirm } from 'core/decorators/confirm'; import { notifySuccess, notifyError } from 'core/decorators/notify'; export default class ScopesScopeUsersRoute extends Route { // =services @service intl; @service notify; @service session; @service can; @service router; // =methods /** * If arriving here unauthenticated, redirect to index for further processing. */ beforeModel() { if (!this.session.isAuthenticated) this.router.transitionTo('index'); } /** * Load all users under current scope. * @return {Promise{[UserModel]}} */ async model() { const scope = this.modelFor('scopes.scope'); const { id: scope_id } = scope; if (this.can.can('list collection', scope, { collection: 'users' })) { return this.store.query('user', { scope_id }); } } // =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(); if (this.can.can('read model', user)) { await this.router.transitionTo('scopes.scope.users.user', user); } else { await this.router.transitionTo('scopes.scope.users'); } this.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(); await this.router.replaceWith('scopes.scope.users'); this.refresh(); } }