From 14eb61b75d27ff485f5822c51b4badea3d06fc0f Mon Sep 17 00:00:00 2001 From: Sk Shaifat Murshed Date: Fri, 31 Jan 2025 18:50:30 +0530 Subject: [PATCH] Fixed issue. --- .../custom-roles/custom-roles.component.html | 61 ++- .../custom-roles/custom-roles.component.ts | 87 +++- .../custom-users/custom-users.component.html | 437 +++++++++++++----- .../custom-users/custom-users.component.ts | 348 ++++++++++---- 4 files changed, 700 insertions(+), 233 deletions(-) diff --git a/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.html b/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.html index acf212d..6024e0d 100644 --- a/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.html +++ b/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.html @@ -12,7 +12,7 @@ - name + Role name Actions @@ -22,11 +22,11 @@ {{ role.name }} - + - + @@ -40,19 +40,66 @@

{{ (selected?.id ? 'AbpIdentity::Edit' : 'AbpIdentity::NewRole') | abpLocalization }}

- + + + +
+ +
+ + + Name is required +
+ + +
+ + +
+ + +
+ + +
+
+ {{ + 'AbpIdentity::Save' | abpLocalization + }} +
- + - {{ + diff --git a/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.ts b/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.ts index 693138f..27e206d 100644 --- a/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.ts +++ b/angular/src/app/modules/custom-identity/custom-roles/custom-roles.component.ts @@ -13,7 +13,7 @@ import { PagedAndSortedResultRequestDto, PagedResultDto, } from '@abp/ng.core'; -import { IdentityRoleDto, IdentityRoleService } from '@abp/ng.identity/proxy'; +import { IdentityRoleCreateDto, IdentityRoleDto, IdentityRoleService, IdentityRoleUpdateDto } from '@abp/ng.identity/proxy'; import { ThemeSharedModule, ConfirmationService, @@ -22,7 +22,7 @@ import { } from '@abp/ng.theme.shared'; import { CommonModule } from '@angular/common'; import { Component, inject, Injector, OnInit } from '@angular/core'; -import { FormsModule, UntypedFormGroup } from '@angular/forms'; +import { FormControl, FormGroup, FormsModule, UntypedFormGroup, Validators } from '@angular/forms'; import { ButtonModule } from 'primeng/button'; import { TableModule } from 'primeng/table'; import { finalize } from 'rxjs'; @@ -31,6 +31,8 @@ import { NgxDatatableModule } from '@swimlane/ngx-datatable'; import { eIdentityComponents } from '@abp/ng.identity'; import { PermissionManagementModule } from '@abp/ng.permission-management'; import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; +import { MenuItem } from 'primeng/api'; +import { Dropdown, DropdownModule } from 'primeng/dropdown'; @Component({ selector: 'app-custom-roles', @@ -47,7 +49,8 @@ import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; ExtensibleModule, PermissionManagementModule, CoreModule, - NgbNavModule + NgbNavModule, + DropdownModule ], templateUrl: './custom-roles.component.html', providers: [ @@ -87,49 +90,97 @@ export class CustomRolesComponent implements OnInit { }; ngOnInit() { + this.hookToQuery(); } + // buildForm() { + // const data = new FormPropData(this.injector, this.selected); + // this.form = generateFormFromProps(data); + // } buildForm() { - const data = new FormPropData(this.injector, this.selected); - this.form = generateFormFromProps(data); + this.form = new FormGroup({ + name: new FormControl(this.selected?.name || '', Validators.required), + isDefault: new FormControl(this.selected?.isDefault || false), + isPublic: new FormControl(this.selected?.isPublic || false) + }); + } - + openModal() { this.buildForm(); this.isModalVisible = true; } add() { - this.selected = {} as IdentityRoleDto; - this.openModal(); + debugger + // this.selected = {} as IdentityRoleDto; + // this.openModal(); + this.selected = { name: '', isDefault: false, isPublic: false } as IdentityRoleDto; + this.isModalVisible = true; } edit(id: string) { debugger + // this.service.get(id).subscribe(res => { + // this.selected = res; + // this.openModal(); + // }); this.service.get(id).subscribe(res => { this.selected = res; - this.openModal(); + this.isModalVisible = true; }); } + // save() { + // debugger + // if (!this.form.valid) return; + // this.modalBusy = true; + + // const { id } = this.selected || {}; + // (id + // ? this.service.update(id, { ...this.selected, ...this.form.value }) + // : this.service.create(this.form.value) + // ) + // .pipe(finalize(() => (this.modalBusy = false))) + // .subscribe(() => { + // this.isModalVisible = false; + // this.toasterService.success('AbpUi::SavedSuccessfully'); + // this.list.get(); + // }); + // } save() { - if (!this.form.valid) return; + debugger + if (!this.selected?.name) { + return; + } + this.modalBusy = true; - - const { id } = this.selected || {}; - (id - ? this.service.update(id, { ...this.selected, ...this.form.value }) - : this.service.create(this.form.value) - ) - .pipe(finalize(() => (this.modalBusy = false))) + + const { id, name, isDefault, isPublic, concurrencyStamp } = this.selected; + + const roleDto = { + name, + isDefault, + isPublic, + concurrencyStamp + }; + + const saveObservable = id + ? this.service.update(id, roleDto) // Update call + : this.service.create(roleDto); // Create call + + saveObservable + .pipe(finalize(() => (this.modalBusy = false))) .subscribe(() => { this.isModalVisible = false; this.toasterService.success('AbpUi::SavedSuccessfully'); this.list.get(); }); } - + + + delete(id: string, name: string) { this.confirmationService .warn('AbpIdentity::RoleDeletionConfirmationMessage', 'AbpIdentity::AreYouSure', { diff --git a/angular/src/app/modules/custom-identity/custom-users/custom-users.component.html b/angular/src/app/modules/custom-identity/custom-users/custom-users.component.html index f37936d..779d8d7 100644 --- a/angular/src/app/modules/custom-identity/custom-users/custom-users.component.html +++ b/angular/src/app/modules/custom-identity/custom-users/custom-users.component.html @@ -8,24 +8,29 @@ --> -
-
-
-
- -
-
- -
+
+
+
+
+
+
+ +
+
- - - - - Username - First Name - Last Name - Email - Status - Actions - - - - - - {{ user.userName }} - {{ user.name }} - {{ user.surname }} - {{ user.email }} - - - - - - - - - - - - - - -
+ + + + Username + First Name + Last Name + Email + Phone No + Status + Actions + + + + + + {{ user.userName }} + {{ user.name }} + {{ user.surname }} + {{ user.email }} + {{ user.phoneNumber }} + + + + +
+ + +
+ + + + +
+
- - - -

{{ (selected?.id ? 'AbpIdentity::Edit' : 'AbpIdentity::NewUser') | abpLocalization }}

-
- - - @if (form) { -
- -
-
- } @else { -
- } -
- - - - {{ +
+ + + +

+ {{ (selected?.id ? 'AbpIdentity::EditUser' : 'AbpIdentity::NewUser') | abpLocalization }} +

+
+ + +
+ + +
+ +
+ +
+
+
+ + + + + +
+ + + + diff --git a/angular/src/app/modules/custom-identity/custom-users/custom-users.component.ts b/angular/src/app/modules/custom-identity/custom-users/custom-users.component.ts index b42e136..78f8d8c 100644 --- a/angular/src/app/modules/custom-identity/custom-users/custom-users.component.ts +++ b/angular/src/app/modules/custom-identity/custom-users/custom-users.component.ts @@ -1,29 +1,83 @@ -import { FormPropData,EXTENSIONS_IDENTIFIER ,generateFormFromProps, ExtensibleModule} from '@abp/ng.components/extensible'; +import { + FormPropData, + EXTENSIONS_IDENTIFIER, + generateFormFromProps, + ExtensibleModule, +} from '@abp/ng.components/extensible'; import { PageModule } from '@abp/ng.components/page'; -import { CoreModule, ListResultDto, ListService, LocalizationModule, PagedResultDto } from '@abp/ng.core'; +import { + CoreModule, + ListResultDto, + ListService, + LocalizationModule, + PagedResultDto, +} from '@abp/ng.core'; import { eIdentityComponents } from '@abp/ng.identity'; -import { GetIdentityUsersInput, IdentityRoleDto, IdentityUserDto, IdentityUserService } from '@abp/ng.identity/proxy'; +import { + GetIdentityUsersInput, + IdentityRoleDto, + IdentityUserDto, + IdentityUserService, + IdentityUserUpdateDto, +} from '@abp/ng.identity/proxy'; import { CommonModule } from '@angular/common'; -import { Component, inject, Injector, OnInit, TemplateRef, TrackByFunction, ViewChild } from '@angular/core'; -import { AbstractControl, FormsModule, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; +import { + Component, + ElementRef, + HostListener, + inject, + Injector, + OnInit, + TemplateRef, + TrackByFunction, + ViewChild, +} from '@angular/core'; +import { + AbstractControl, + FormsModule, + NgForm, + UntypedFormArray, + UntypedFormBuilder, + UntypedFormGroup, +} from '@angular/forms'; import { ButtonModule } from 'primeng/button'; import { TableModule } from 'primeng/table'; -import { Confirmation, eFormComponets, ThemeSharedModule,ToasterService ,ConfirmationService} from '@abp/ng.theme.shared'; +import { + Confirmation, + eFormComponets, + ThemeSharedModule, + ToasterService, + ConfirmationService, +} from '@abp/ng.theme.shared'; import { finalize, switchMap, tap } from 'rxjs'; -import { ePermissionManagementComponents, PermissionManagementModule } from '@abp/ng.permission-management'; +import { + ePermissionManagementComponents, + PermissionManagementModule, +} from '@abp/ng.permission-management'; import { NgxDatatableModule } from '@swimlane/ngx-datatable'; -import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; +import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; import { PaginatorModule } from 'primeng/paginator'; import { TagModule } from 'primeng/tag'; // For active/inactive status +import { SplitButtonModule } from 'primeng/splitbutton'; @Component({ selector: 'app-custom-users', standalone: true, - imports: [CommonModule,FormsModule,TableModule,ButtonModule,PageModule,LocalizationModule,ThemeSharedModule,NgxDatatableModule,ExtensibleModule, - PermissionManagementModule, - CoreModule, - NgbNavModule, - PaginatorModule,TagModule + imports: [ + CommonModule, + FormsModule, + TableModule, + ButtonModule, + PageModule, + LocalizationModule, + ThemeSharedModule, + NgxDatatableModule, + ExtensibleModule, + PermissionManagementModule, + CoreModule, + NgbNavModule, + PaginatorModule, + TagModule,SplitButtonModule ], templateUrl: './custom-users.component.html', providers: [ @@ -33,15 +87,16 @@ import { TagModule } from 'primeng/tag'; // For active/inactive status useValue: eIdentityComponents.Users, }, ], - styleUrl: './custom-users.component.scss' + styleUrl: './custom-users.component.scss', }) -export class CustomUsersComponent implements OnInit{ +export class CustomUsersComponent implements OnInit { protected readonly list = inject(ListService); protected readonly confirmationService = inject(ConfirmationService); protected readonly service = inject(IdentityUserService); protected readonly toasterService = inject(ToasterService); private readonly fb = inject(UntypedFormBuilder); private readonly injector = inject(Injector); + @ViewChild('firstDropdownItem') firstDropdownItem: ElementRef; data: PagedResultDto = { items: [], totalCount: 0 }; @@ -49,12 +104,13 @@ export class CustomUsersComponent implements OnInit{ modalContent!: TemplateRef; form!: UntypedFormGroup; - + password:string; selected?: IdentityUserDto; selectedUserRoles?: IdentityRoleDto[]; - roles?: IdentityRoleDto[]; + // roles?: IdentityRoleDto[]; + roles: (IdentityRoleDto & { selected: boolean })[] = []; // Use intersection type to add 'selected' visiblePermissions = false; @@ -71,8 +127,32 @@ export class CustomUsersComponent implements OnInit{ inputKey = eFormComponets.FormCheckboxComponent; trackByFn: TrackByFunction = (index, item) => Object.keys(item)[0] || index; + isDropdownOpen = false; + + toggleDropdown() { + this.isDropdownOpen = !this.isDropdownOpen; + + // After the dropdown is shown, focus the first item in the dropdown + setTimeout(() => { + if (this.firstDropdownItem) { + this.firstDropdownItem.nativeElement.focus(); + } + }, 100); // Delay to ensure dropdown is rendered + } + + closeDropdown() { + this.isDropdownOpen = false; + } + actionItems = [ + { label: 'Edit', icon: 'pi pi-pencil', command: () => this.edit("") }, + { label: 'Delete', icon: 'pi pi-trash', command: () => this.delete("","") }, + // { label: 'Permissions', icon: 'pi pi-shield', command: () => this.openPermissionsModal("") }, + ]; + + onVisiblePermissionChange = (event: boolean) => { + debugger this.visiblePermissions = event; }; @@ -83,87 +163,189 @@ export class CustomUsersComponent implements OnInit{ ngOnInit() { this.hookToQuery(); } + formValid() { + return true; + } + // buildForm() { + // debugger + // const data = new FormPropData(this.injector, this.selected); + // this.form = generateFormFromProps(data); + + // this.service.getAssignableRoles().subscribe(({ items }) => { + // this.roles = items; + // if (this.roles) { + // this.form.addControl( + // 'roleNames', + // this.fb.array( + // this.roles.map(role => + // this.fb.group({ + // [role.name as string]: [ + // this.selected?.id + // ? !!this.selectedUserRoles?.find(userRole => userRole.id === role.id) + // : role.isDefault, + // ], + // }), + // ), + // ), + // ); + // } + // }); + // } - buildForm() { - debugger - const data = new FormPropData(this.injector, this.selected); - this.form = generateFormFromProps(data); + // openModal() { + // this.buildForm(); + // this.isModalVisible = true; + // } + // add() { + // debugger + // this.selected = {} as IdentityUserDto; + // this.selectedUserRoles = [] as IdentityRoleDto[]; + // this.openModal(); + // } + + // edit(id: string) { + // debugger + // this.service + // .get(id) + // .pipe( + // tap(user => (this.selected = user)), + // switchMap(() => this.service.getRoles(id)), + // ) + // .subscribe(userRole => { + // debugger + + // this.selectedUserRoles = userRole.items || []; + // this.openModal(); + // }); + // } + + // save() { + // if (!this.form.valid || this.modalBusy) return; + // this.modalBusy = true; + + // const { roleNames = [] } = this.form.value; + // const mappedRoleNames = + // roleNames + // .filter((role: { [key: string]: any }) => !!role[Object.keys(role)[0]]) + // .map((role: { [key: string]: any }) => Object.keys(role)[0]) || []; + + // const { id } = this.selected || {}; + + // (id + // ? this.service.update(id, { + // ...this.selected, + // ...this.form.value, + // roleNames: mappedRoleNames, + // }) + // : this.service.create({ ...this.form.value, roleNames: mappedRoleNames }) + // ) + // .pipe(finalize(() => (this.modalBusy = false))) + // .subscribe(() => { + // this.isModalVisible = false; + // this.toasterService.success('AbpUi::SavedSuccessfully'); + // this.list.get(); + // }); + // } + buildForm() { this.service.getAssignableRoles().subscribe(({ items }) => { - this.roles = items; - if (this.roles) { - this.form.addControl( - 'roleNames', - this.fb.array( - this.roles.map(role => - this.fb.group({ - [role.name as string]: [ - this.selected?.id - ? !!this.selectedUserRoles?.find(userRole => userRole.id === role.id) - : role.isDefault, - ], - }), - ), - ), - ); + this.roles = items.map(role => ({ + ...role, + selected: this.selectedUserRoles.some(userRole => userRole.id === role.id), + })); + + // Initialize the selected roles based on existing data for selected user + if (this.selected?.id) { + this.roles.forEach(role => { + role.selected = this.selectedUserRoles.some(userRole => userRole.id === role.id); + }); + } else { + // Default values when creating a new user + this.roles.forEach(role => { + role.selected = role.isDefault; + }); } }); } openModal() { - this.buildForm(); + this.loadRoles(); // Load roles when modal opens this.isModalVisible = true; } - + loadRoles() { + this.service.getAssignableRoles().subscribe(({ items }) => { + this.roles = items.map(role => ({ + ...role, // spread IdentityRoleDto properties + selected: false, // Initially no role is selected + })); + }); + } add() { - debugger this.selected = {} as IdentityUserDto; this.selectedUserRoles = [] as IdentityRoleDto[]; + this.password = ''; // Store password separately + this.openModal(); } edit(id: string) { - debugger this.service .get(id) - .pipe( - tap(user => (this.selected = user)), - switchMap(() => this.service.getRoles(id)), - ) - .subscribe(userRole => { - debugger - - this.selectedUserRoles = userRole.items || []; - this.openModal(); + .pipe(finalize(() => this.buildForm())) + .subscribe(user => { + this.selected = user; + this.service.getRoles(id).subscribe(userRoles => { + this.selectedUserRoles = userRoles.items || []; + }); }); + this.openModal(); } - save() { - if (!this.form.valid || this.modalBusy) return; + // Save user data (either create or update) + save(form: NgForm) { + debugger; + if (this.modalBusy || form.invalid) return; + this.modalBusy = true; - const { roleNames = [] } = this.form.value; - const mappedRoleNames = - roleNames - .filter((role: { [key: string]: any }) => !!role[Object.keys(role)[0]]) - .map((role: { [key: string]: any }) => Object.keys(role)[0]) || []; - - const { id } = this.selected || {}; - - (id - ? this.service.update(id, { - ...this.selected, - ...this.form.value, - roleNames: mappedRoleNames, - }) - : this.service.create({ ...this.form.value, roleNames: mappedRoleNames }) - ) - .pipe(finalize(() => (this.modalBusy = false))) - .subscribe(() => { - this.isModalVisible = false; - this.toasterService.success('AbpUi::SavedSuccessfully'); - this.list.get(); - }); + // Prepare roleNames array + // Prepare roleNames array + const selectedRoleNames = this.roles + .filter(role => role.selected) // Only include selected roles + .map(role => role.name); // Extract role names + + // Create user payload + + const userPayload: any = { + userName: this.selected.userName, + name: this.selected.name, + surname: this.selected.surname, + email: this.selected.email, + phoneNumber: this.selected.phoneNumber, + isActive: this.selected.isActive, + lockoutEnabled: this.selected.lockoutEnabled, + roleNames: selectedRoleNames, // Include selected roles + + }; + + // Only include password if creating a new user + if (!this.selected.id && this.password) { + userPayload.password = this.password; + } + // Handle create or update + const saveObservable = this.selected.id + ? this.service.update(this.selected.id, userPayload) // Update if id exists + : this.service.create(userPayload); // Create if no id + + saveObservable.pipe(finalize(() => (this.modalBusy = false))).subscribe(() => { + this.isModalVisible = false; + this.toasterService.success('AbpUi::SavedSuccessfully'); + this.list.get(); // Reload the list if needed + }); + } + + private getRoleNames() { + return this.roles.filter(role => role.selected).map(role => role.name); } delete(id: string, userName: string) { @@ -191,17 +373,19 @@ export class CustomUsersComponent implements OnInit{ // this.list.hookToQuery(query => this.service.getList(query)).subscribe(res => (this.data = res)); // } private hookToQuery() { - this.list.hookToQuery(query => { - console.log('Query:', query); // Log the query - return this.service.getList(query); - }).subscribe(res => { -debugger - this.data = res; - }); + this.list + .hookToQuery(query => { + console.log('Query:', query); // Log the query + return this.service.getList(query); + }) + .subscribe(res => { + debugger; + this.data = res; + }); } openPermissionsModal(providerKey: string, entityDisplayName?: string) { - debugger + debugger; this.providerKey = providerKey; this.entityDisplayName = entityDisplayName; setTimeout(() => {