Skip to content
Snippets Groups Projects
Commit 3218479c authored by Hamza HRAMCHI's avatar Hamza HRAMCHI
Browse files

FIX #17436 TIME 1:08 admin groups: link front/back + add unlinkGroup function

parent c91d408f
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ import { AlertController, ModalController, PopoverController } from '@ionic/angu
import { UsersComponent } from './list/users.component';
import { of } from 'rxjs';
import { GroupModalComponent } from './modal/group-modal.component';
import { FunctionsService } from '../../service/functions.service';
export interface Group {
......@@ -42,6 +43,7 @@ export class GroupComponent implements OnInit {
usersList: any[];
sortedData: any[];
groups: any[] = [];
allGroups: any[] = [];
constructor(
public http: HttpClient,
......@@ -54,7 +56,8 @@ export class GroupComponent implements OnInit {
public authService: AuthService,
public popoverController: PopoverController,
public modalController: ModalController,
public alertController: AlertController
public alertController: AlertController,
public functions: FunctionsService
) {
this.displayedColumns = ['firstname', 'lastname', 'actions'];
this.group = {
......@@ -131,7 +134,7 @@ export class GroupComponent implements OnInit {
}
async openGroupList() {
await this.getGroups();
await this.getPrivilegeParameters();
const modal = await this.modalController.create({
component: GroupModalComponent,
componentProps: {
......@@ -142,30 +145,30 @@ export class GroupComponent implements OnInit {
const { data } = await modal.onWillDismiss();
if (data !== undefined) {
this.groups = data;
const manageUers: any = this.group.privileges.find((item: any) => item.id === 'manage_users');
this.updatePrivilege(manageUers);
const privilege: any = this.group.privileges.find((item: any) => item.id === 'manage_users');
this.updatePrivilege(privilege);
}
}
getGroups() {
getPrivilegeParameters() {
const privilege: any = this.group.privileges.find((item: any) => item.id === 'manage_users');
return new Promise((resolve) => {
this.http.get('../rest/groups').pipe(
map((data: any) => {
data = data.groups.map((item: any) => ({
id: item.id,
label: item.label,
checked: true
}));
return data;
}),
this.http.get('../rest/groups/' + this.group.id + '/privilege/' + privilege.id).pipe(
tap((data: any) => {
const result: any[] = data;
const ids: number[] = this.groups.map((el: any) => el.id);
result.forEach((element: any) => {
if (ids.indexOf(element.id) === -1) {
this.groups.push(element);
}
});
if (!this.functions.empty(data.parameters.authorized)) {
const result: number[] = data.parameters.authorized;
const ids: number[] = this.allGroups.map((item: any) => item.id);
result.forEach((element: any) => {
if (ids.indexOf(element) === -1) {
const newGroup: any = this.allGroups.find((group: any) => group.id === element);
this.groups.push({
id: newGroup.id,
label: newGroup.label,
checked: true
});
}
});
}
resolve(true);
}),
catchError((err: any) => {
......@@ -351,8 +354,14 @@ export class GroupComponent implements OnInit {
});
await alert.present();
} else {
if (privilege.id === 'manage_users' && !privilege.checked) {
this.groups = [];
if (privilege.id === 'manage_users') {
console.log('privilege', privilege);
console.log('toggle', toggle);
if (!privilege.checked) {
this.getGroups();
} else {
this.groups = [];
}
}
if (!toggle) {
privilege.checked = !privilege.checked;
......@@ -363,6 +372,18 @@ export class GroupComponent implements OnInit {
}
}
getGroups() {
this.http.get('../rest/groups').pipe(
tap((data: any) => {
this.allGroups = data.groups;
}),
catchError((err: any) => {
this.notificationService.handleErrors(err);
return of(false);
})
).subscribe();
}
updatePrivilege(privilege: any) {
const objTosend: any = {
checked: privilege.checked,
......
......@@ -123,7 +123,7 @@
<ion-label>{{group.label}}</ion-label>
<ion-checkbox *ngIf="creationMode" slot="end" [value]="group.id" [checked]="group.checked" (ionChange)="group.checked = !group.checked"></ion-checkbox>
<ion-button *ngIf="!creationMode" slot="end" fill="clear" shape="round"
(click)="$event.stopPropagation();" [title]="'lang.unlinkGroup' | translate">
(click)="unlinkGroup(group.id);" [title]="'lang.unlinkGroup' | translate">
<ion-icon color="danger" slot="icon-only" name="close-outline"></ion-icon>
</ion-button>
</ion-item>
......
......@@ -377,4 +377,38 @@ export class UserComponent implements OnInit {
).subscribe();
});
}
async unlinkGroup(groupId: number) {
const alert = await this.alertController.create({
header: this.translate.instant('lang.confirmMsg'),
buttons: [
{
text: this.translate.instant('lang.no'),
role: 'cancel',
cssClass: 'secondary',
handler: () => { }
},
{
text: this.translate.instant('lang.yes'),
handler: () => {
this.http.delete(`../rest/user/${this.user.id}/group/${groupId}/unlinkgroup`)
.pipe(
finalize(() => this.loading = false)
)
.subscribe({
next: data => {
const indexToDelete = this.user['groups'].findIndex((group: any) => group.id === groupId);
this.user['groups'].splice(indexToDelete, 1);
this.notificationService.success('lang.groupDeleted');
},
error: err => {
this.notificationService.handleErrors(err);
}
});
}
}
]
});
await alert.present();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment