RED-4595: Fixed users role assignment

This commit is contained in:
Adina Țeudan 2022-07-12 17:18:36 +03:00
parent faa00fc305
commit b4593807d2
2 changed files with 19 additions and 18 deletions

View File

@ -24,6 +24,8 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
readonly ROLES = ['RED_USER', 'RED_MANAGER', 'RED_USER_ADMIN', 'RED_ADMIN'];
readonly translations = rolesTranslations;
/** e.g. a RED_ADMIN is automatically a RED_USER_ADMIN => can't disable RED_USER_ADMIN as long as RED_ADMIN is checked */
private readonly _ROLE_REQUIREMENTS = { RED_MANAGER: 'RED_USER', RED_ADMIN: 'RED_USER_ADMIN' };
constructor(
@ -66,15 +68,14 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
}
shouldBeDisabled(role: string): boolean {
if (!this.user) {
return false;
}
const isCurrentAdminUser = this.user.isAdmin && this.user.id === this.userService.currentUser.id;
const isCurrentAdminUser = this.user && this.user.isAdmin && this.user.id === this.userService.currentUser.id;
return (
// RED_ADMIN can't remove own RED_ADMIN role
(role === 'RED_ADMIN' && isCurrentAdminUser) ||
// only RED_ADMINs can edit RED_ADMIN roles
(role === 'RED_ADMIN' && !this.userService.currentUser.isAdmin) ||
Object.keys(this._ROLE_REQUIREMENTS).reduce(
(value, key) => value || (role === this._ROLE_REQUIREMENTS[key] && this.user.roles.includes(key)),
(value, key) => value || (role === this._ROLE_REQUIREMENTS[key] && this.user?.roles.includes(key)),
false,
)
);
@ -107,6 +108,17 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
this._dialogService.deleteUsers([this.user.id], null, () => this.closeDialog.emit(true));
}
setRolesRequirements(checked: boolean, role: string): void {
if (Object.keys(this._ROLE_REQUIREMENTS).includes(role)) {
if (checked) {
this.form.patchValue({ [this._ROLE_REQUIREMENTS[role]]: true });
this.form.controls[this._ROLE_REQUIREMENTS[role]].disable();
} else {
this.form.controls[this._ROLE_REQUIREMENTS[role]].enable();
}
}
}
private _getForm(): UntypedFormGroup {
return this._formBuilder.group({
firstName: [this.user?.firstName, Validators.required],
@ -121,15 +133,4 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
...this._rolesControls,
});
}
setRolesRequirements(checked: boolean, role: string): void {
if (Object.keys(this._ROLE_REQUIREMENTS).includes(role)) {
if (checked) {
this.form.patchValue({ [this._ROLE_REQUIREMENTS[role]]: true });
this.form.controls[this._ROLE_REQUIREMENTS[role]].disable();
} else {
this.form.controls[this._ROLE_REQUIREMENTS[role]].enable();
}
}
}
}

View File

@ -90,7 +90,7 @@ export class UserService extends EntitiesService<IUser, User> {
return true;
}
getUsers(onlyRed = false, refreshCache = false): Observable<IUser[]> {
getUsers(onlyRed = false, refreshCache = true): Observable<IUser[]> {
const url = onlyRed ? `${this._defaultModelPath}/red` : this._defaultModelPath;
return super.getAll(url, [{ key: 'refreshCache', value: refreshCache }]);
}