RED-9987: added sendSetPasswordMail flag in add user dialog.

This commit is contained in:
Nicoleta Panaghiu 2024-09-03 14:51:06 +03:00
parent ca0fe4cf13
commit 901ba934af
8 changed files with 71 additions and 38 deletions

View File

@ -1,6 +1,6 @@
<div
[translateParams]="{
type: user ? 'edit' : 'create'
type: !!user() ? 'edit' : 'create',
}"
[translate]="'add-edit-user.title'"
class="dialog-header heading-l"
@ -37,9 +37,17 @@
</div>
</div>
@if (!user()) {
<div class="iqser-input-group">
<label [translate]="'add-edit-user.form.account-setup'"></label>
<mat-checkbox formControlName="sendSetPasswordMail">{{ 'add-edit-user.form.send-email' | translate }}</mat-checkbox>
<span [translate]="'add-edit-user.form.send-email-explanation'" class="hint"></span>
</div>
}
<div
(click)="toggleResetPassword.emit()"
*ngIf="!!user"
*ngIf="!!user()"
[translate]="'add-edit-user.form.reset-password'"
class="mt-24 fit-content link-action"
></div>
@ -48,14 +56,14 @@
<div class="dialog-actions">
<iqser-icon-button
[disabled]="form.invalid || !changed"
[label]="(user ? 'add-edit-user.actions.save-changes' : 'add-edit-user.actions.save') | translate"
[label]="(user() ? 'add-edit-user.actions.save-changes' : 'add-edit-user.actions.save') | translate"
[submit]="true"
[type]="iconButtonTypes.primary"
></iqser-icon-button>
<iqser-icon-button
(action)="delete()"
*ngIf="user && !disabledDelete(user)"
*ngIf="user() && !disabledDelete(user())"
[label]="'add-edit-user.actions.delete' | translate"
[type]="iconButtonTypes.dark"
icon="iqser:trash"

View File

@ -5,3 +5,7 @@
margin-top: 8px;
width: 300px;
}
.hint {
margin-left: 23px;
}

View File

@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { Component, input, OnInit, output } from '@angular/core';
import { ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { AdminDialogService } from '../../../services/admin-dialog.service';
import { BaseFormComponent, IconButtonComponent, LoadingService, Toaster } from '@iqser/common-ui';
@ -20,15 +20,15 @@ import { NgForOf, NgIf } from '@angular/common';
standalone: true,
imports: [TranslateModule, ReactiveFormsModule, MatCheckbox, NgForOf, IconButtonComponent, NgIf],
})
export class UserDetailsComponent extends BaseFormComponent implements OnChanges {
/** 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' };
@Input() user: User;
@Output() readonly toggleResetPassword = new EventEmitter();
@Output() readonly closeDialog = new EventEmitter();
@Output() readonly cancel = new EventEmitter();
export class UserDetailsComponent extends BaseFormComponent implements OnInit {
user = input<User>();
readonly toggleResetPassword = output();
readonly closeDialog = output<boolean>();
readonly cancel = output();
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 */
readonly #ROLE_REQUIREMENTS = { RED_MANAGER: 'RED_USER', RED_ADMIN: 'RED_USER_ADMIN' };
constructor(
private readonly _formBuilder: UntypedFormBuilder,
@ -49,13 +49,17 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
}, []);
}
private get _rolesControls(): any {
get sendSetPasswordMail() {
return !this.form.controls.sendSetPasswordMail.value;
}
get #rolesControls() {
return this.ROLES.reduce(
(prev, role) => ({
...prev,
[role]: [
{
value: this.user && this.user.has(role),
value: this.user() && this.user().has(role),
disabled: this.shouldBeDisabled(role),
},
],
@ -64,20 +68,20 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
);
}
ngOnChanges() {
this.form = this._getForm();
ngOnInit() {
this.form = this.#getForm();
this.initialFormValue = this.form.getRawValue();
}
shouldBeDisabled(role: string): boolean {
const isCurrentAdminUser = this.user && 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)),
Object.keys(this.#ROLE_REQUIREMENTS).reduce(
(value, key) => value || (role === this.#ROLE_REQUIREMENTS[key] && this.user()?.roles.includes(key)),
false,
)
);
@ -85,9 +89,13 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
async save() {
this._loadingService.start();
const userData: IProfileUpdateRequest = { ...this.form.getRawValue(), roles: this.activeRoles };
const userData: IProfileUpdateRequest = {
...this.form.getRawValue(),
roles: this.activeRoles,
sendSetPasswordMail: this.sendSetPasswordMail,
};
if (!this.user) {
if (!this.user()) {
await firstValueFrom(this._userService.create(userData))
.then(() => {
this.closeDialog.emit(true);
@ -101,22 +109,22 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
this._loadingService.stop();
});
} else {
await firstValueFrom(this._userService.updateProfile(userData, this.user.id));
await firstValueFrom(this._userService.updateProfile(userData, this.user().id));
this.closeDialog.emit(true);
}
}
delete() {
this._dialogService.deleteUsers([this.user.id], () => this.closeDialog.emit(true));
this._dialogService.deleteUsers([this.user().id], () => this.closeDialog.emit(true));
}
setRolesRequirements(checked: boolean, role: string): void {
if (Object.keys(this._ROLE_REQUIREMENTS).includes(role)) {
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();
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();
this.form.controls[this.#ROLE_REQUIREMENTS[role]].enable();
}
}
}
@ -127,18 +135,19 @@ export class UserDetailsComponent extends BaseFormComponent implements OnChanges
return user.id === this._userService.currentUser.id || (userAdmin && !currentUserAdmin);
}
private _getForm(): UntypedFormGroup {
#getForm(): UntypedFormGroup {
return this._formBuilder.group({
firstName: [this.user?.firstName, Validators.required],
lastName: [this.user?.lastName, Validators.required],
firstName: [this.user()?.firstName, Validators.required],
lastName: [this.user()?.lastName, Validators.required],
email: [
{
value: this.user?.email,
disabled: !!this.user?.email,
value: this.user()?.email,
disabled: !!this.user()?.email,
},
[Validators.required, Validators.email],
],
...this._rolesControls,
...this.#rolesControls,
sendSetPasswordMail: [false],
});
}
}

View File

@ -206,11 +206,14 @@
"generic": "Speichern des Benutzers fehlgeschlagen."
},
"form": {
"account-setup": "User account setup",
"email": "E-Mail",
"first-name": "Vorname",
"last-name": "Nachname",
"reset-password": "Passwort zurücksetzen",
"role": "Rolle"
"role": "Rolle",
"send-email": "Do not send email requesting the user to set a password",
"send-email-explanation": "Select this option if you use SSO. Please note that you will need to inform the user directly."
},
"title": "{type, select, edit{Benutzer bearbeiten} create{Neuen Benutzer erstellen} other{}}"
},

View File

@ -206,11 +206,14 @@
"generic": "Failed to save user."
},
"form": {
"account-setup": "User account setup",
"email": "E-mail",
"first-name": "First name",
"last-name": "Last name",
"reset-password": "Reset password",
"role": "Role"
"role": "Role",
"send-email": "Do not send email requesting the user to set a password",
"send-email-explanation": "Select this option if you use SSO. Please note that you will need to inform the user directly."
},
"title": "{type, select, edit{Edit} create{Add new} other{}} user"
},

View File

@ -206,11 +206,14 @@
"generic": "Benutzer konnte nicht gespeichert werden!"
},
"form": {
"account-setup": "User account setup",
"email": "E-Mail",
"first-name": "Vorname",
"last-name": "Nachname",
"reset-password": "Passwort zurücksetzen",
"role": "Rolle"
"role": "Rolle",
"send-email": "Do not send email requesting the user to set a password",
"send-email-explanation": "Select this option if you use SSO. Please note that you will need to inform the user directly."
},
"title": "{type, select, edit{Benutzer bearbeiten} create{Neuen Benutzer hinzufügen} other{}}"
},

View File

@ -206,11 +206,14 @@
"generic": "Failed to save user!"
},
"form": {
"account-setup": "User account setup",
"email": "E-mail",
"first-name": "First name",
"last-name": "Last name",
"reset-password": "Reset password",
"role": "Role"
"role": "Role",
"send-email": "Do not send email requesting the user to set a password",
"send-email-explanation": "Select this option if you use SSO. Please note that you will need to inform the user directly."
},
"title": "{type, select, edit{Edit} create{Add New} other{}} user"
},

@ -1 +1 @@
Subproject commit f685955f8f49529eef366f9d4c25c5bf510e6740
Subproject commit efd7e2a085be179765fc2d20b40b7787dc161a5b