Skip to content

Commit

Permalink
NAS-113704 / 25.04 / Fix for User Quota exclude root if it is not per…
Browse files Browse the repository at this point in the history
…mitted (Filter user options by roles) (#10949)

* NAS-113704: Filter user options by roles

* NAS-113704: Fix remarks

---------

Co-authored-by: Boris Vasilenko <[email protected]>
  • Loading branch information
bvasilenko and bvasilenko authored Nov 5, 2024
1 parent ea32264 commit 8b74fc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
formControlName="users"
[label]="'Apply To Users' | translate"
[tooltip]="tooltips.users"
[required]="true"
[autocompleteProvider]="usersProvider"
[allowNewEntries]="false"
></ix-chips>
</ix-fieldset>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { mockAuth } from 'app/core/testing/utils/mock-auth.utils';
import { mockCall, mockWebSocket } from 'app/core/testing/utils/mock-websocket.utils';
import { DatasetQuotaType } from 'app/enums/dataset.enum';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { IxChipsHarness } from 'app/modules/forms/ix-forms/components/ix-chips/ix-chips.harness';
import { IxFormHarness } from 'app/modules/forms/ix-forms/testing/ix-form.harness';
import { SlideInRef } from 'app/modules/slide-ins/slide-in-ref';
import { SLIDE_IN_DATA } from 'app/modules/slide-ins/slide-in.token';
Expand All @@ -31,7 +32,10 @@ describe('DatasetQuotaAddFormComponent', () => {
mockCall('pool.dataset.set_quota'),
]),
mockProvider(UserService, {
userQueryDsCache: () => of(),
userQueryDsCache: () => of([
{ username: 'john', roles: [] },
{ username: 'jill', roles: [] },
]),
groupQueryDsCache: () => of(),
}),
mockProvider(SlideInService),
Expand Down Expand Up @@ -65,17 +69,17 @@ describe('DatasetQuotaAddFormComponent', () => {
await form.fillForm({
'User Data Quota (Examples: 500 KiB, 500M, 2 TB)': '500M',
'User Object Quota': 2000,
'Apply To Users': ['jill', 'john'],
});

const usersInput = await form.getControl('Apply To Users') as IxChipsHarness;
await usersInput.selectSuggestionValue('john');

const saveButton = await loader.getHarness(MatButtonHarness.with({ text: 'Save' }));
await saveButton.click();

expect(ws.call).toHaveBeenCalledWith('pool.dataset.set_quota', [
'my-dataset',
[
{ id: 'jill', quota_type: DatasetQuotaType.User, quota_value: 524288000 },
{ id: 'jill', quota_type: DatasetQuotaType.UserObj, quota_value: 2000 },
{ id: 'john', quota_type: DatasetQuotaType.User, quota_value: 524288000 },
{ id: 'john', quota_type: DatasetQuotaType.UserObj, quota_value: 2000 },
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MatCard, MatCardContent } from '@angular/material/card';
import { FormBuilder } from '@ngneat/reactive-forms';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { map } from 'rxjs/operators';
import { combineLatest, map } from 'rxjs';
import { RequiresRolesDirective } from 'app/directives/requires-roles/requires-roles.directive';
import { DatasetQuotaType } from 'app/enums/dataset.enum';
import { Role } from 'app/enums/role.enum';
Expand All @@ -26,6 +26,7 @@ import { SlideInRef } from 'app/modules/slide-ins/slide-in-ref';
import { SLIDE_IN_DATA } from 'app/modules/slide-ins/slide-in.token';
import { SnackbarService } from 'app/modules/snackbar/services/snackbar.service';
import { TestDirective } from 'app/modules/test-id/test.directive';
import { AuthService } from 'app/services/auth/auth.service';
import { UserService } from 'app/services/user.service';
import { WebSocketService } from 'app/services/ws.service';

Expand Down Expand Up @@ -106,8 +107,15 @@ export class DatasetQuotaAddFormComponent implements OnInit {
};

usersProvider: ChipsProvider = (query) => {
return this.userService.userQueryDsCache(query).pipe(
map((users) => users.map((user) => user.username)),
return combineLatest([
this.userService.userQueryDsCache(query),
this.authService.user$.pipe(map((user) => user?.privilege?.roles?.$set || [])),
]).pipe(
map(([users, currentRoles]) => {
return users
.filter((user) => user.roles.every((role) => currentRoles.includes(role)))
.map((user) => user.username);
}),
);
};

Expand All @@ -120,6 +128,7 @@ export class DatasetQuotaAddFormComponent implements OnInit {
private datasetId: string;

constructor(
private authService: AuthService,
private formBuilder: FormBuilder,
private ws: WebSocketService,
private snackbar: SnackbarService,
Expand Down

0 comments on commit 8b74fc7

Please sign in to comment.