Skip to content

Commit

Permalink
added a permission check like the one on ManageUsersSummary component
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchadwick committed Feb 7, 2025
1 parent c6f9a43 commit bffafb8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 25 deletions.
52 changes: 27 additions & 25 deletions packages/frontend/app/components/ilios-users.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
{{t "general.users"}}
</span>
<div class="actions">
{{#if (or @showNewUserForm @showBulkNewUserForm)}}
<button
type="button"
{{on
"click"
(if
@showNewUserForm (fn @setShowNewUserForm false) (fn @setShowBulkNewUserForm false)
)
}}
data-test-collapse
>
<FaIcon @icon="minus" />
</button>
{{else}}
<button
type="button"
{{on "click" (fn @setShowNewUserForm true)}}
data-test-show-new-user-form
>
{{t "general.create"}}
</button>
{{#if (not-eq this.userSearchType "ldap")}}
{{#if @canCreate}}
{{#if (or @showNewUserForm @showBulkNewUserForm)}}
<button
type="button"
{{on "click" (fn @setShowBulkNewUserForm true)}}
data-test-show-bulk-new-user-form
{{on
"click"
(if
@showNewUserForm (fn @setShowNewUserForm false) (fn @setShowBulkNewUserForm false)
)
}}
data-test-collapse
>
{{t "general.createBulk"}}
<FaIcon @icon="minus" />
</button>
{{else}}
<button
type="button"
{{on "click" (fn @setShowNewUserForm true)}}
data-test-show-new-user-form
>
{{t "general.create"}}
</button>
{{#if (not-eq this.userSearchType "ldap")}}
<button
type="button"
{{on "click" (fn @setShowBulkNewUserForm true)}}
data-test-show-bulk-new-user-form
>
{{t "general.createBulk"}}
</button>
{{/if}}
{{/if}}
{{/if}}
</div>
Expand Down
32 changes: 32 additions & 0 deletions packages/frontend/app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { filter } from 'rsvp';
import { cached } from '@glimmer/tracking';
import { TrackedAsyncData } from 'ember-async-data';

export default class UsersController extends Controller {
@service store;
@service router;

queryParams = [
Expand All @@ -22,6 +26,34 @@ export default class UsersController extends Controller {
showBulkNewUserForm = false;
searchTerms = null;

@cached
get canCreateData() {
return new TrackedAsyncData(this.canCreateInSomeSchool(this.allSchools));
}

get canCreate() {
return this.canCreateData.isResolved ? this.canCreateData.value : false;
}

@cached
get allSchoolsData() {
return new TrackedAsyncData(this.store.findAll('school'));
}

get allSchools() {
return this.allSchoolsData.isResolved ? this.allSchoolsData.value : [];
}

async canCreateInSomeSchool(schools) {
if (!schools) {
return false;
}
const schoolsWithCreateUserPermission = await filter(schools, async (school) => {
return this.permissionChecker.canCreateUser(school);
});
return schoolsWithCreateUserPermission.length > 0;
}

@action
transitionToUser(userId) {
this.router.transitionTo('user', userId);
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/app/templates/users.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@setLimit={{set this "limit"}}
@query={{this.query}}
@setQuery={{set this "query"}}
@canCreate={{this.canCreate}}
@showNewUserForm={{this.showNewUserForm}}
@setShowNewUserForm={{set this "showNewUserForm"}}
@showBulkNewUserForm={{this.showBulkNewUserForm}}
Expand Down
29 changes: 29 additions & 0 deletions packages/frontend/tests/integration/components/ilios-users-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,37 @@ module('Integration | Component | ilios users', function (hooks) {
@setSearchTerms={{(noop)}}
@transitionToUser={{(noop)}}
/>`);

assert.strictEqual(component.title.text, 'Users');
});

test('it shows/hides new user creation buttons depending on user permission', async function (assert) {
this.set('canCreate', false);

await render(hbs`<IliosUsers
@limit='25'
@offset='25'
@query=''
@canCreate={{this.canCreate}}
@searchTerms={{(array)}}
@setQuery={{(noop)}}
@setLimit={{(noop)}}
@setOffset={{(noop)}}
@setShowNewUserForm={{(noop)}}
@setShowBulkNewUserForm={{(noop)}}
@setSearchTerms={{(noop)}}
@transitionToUser={{(noop)}}
/>`);

assert.notOk(component.showNewUserFormButton.isVisible);
assert.notOk(component.showBulkUsersFormButton.isVisible);

this.set('canCreate', true);

assert.ok(component.showNewUserFormButton.isVisible);
assert.ok(component.showBulkUsersFormButton.isVisible);
});

test('param passing', async function (assert) {
assert.expect(2);

Expand Down Expand Up @@ -271,6 +299,7 @@ module('Integration | Component | ilios users', function (hooks) {
assert.notOk(value);
});
await render(hbs`<IliosUsers
@canCreate={{true}}
@showBulkNewUserForm={{true}}
@searchTerms={{(array)}}
@setQuery={{(noop)}}
Expand Down

0 comments on commit bffafb8

Please sign in to comment.