11import { HttpErrorResponse } from "@angular/common/http" ;
2- import { Component , OnInit } from "@angular/core" ;
3- import { UntypedFormGroup } from "@angular/forms" ;
2+ import { Component , OnDestroy , OnInit } from "@angular/core" ;
3+ import { UntypedFormControl , UntypedFormGroup } from "@angular/forms" ;
44import { ActivatedRoute } from "@angular/router" ;
55import { UserRequest } from "../user.model" ;
66import { TranslateService } from "@ngx-translate/core" ;
77import { UserService } from "../user.service" ;
8- import { Subscription } from "rxjs" ;
8+ import { Subject , Subscription } from "rxjs" ;
99import { Location } from "@angular/common" ;
10- import { PermissionType } from "@app/admin/permission/permission.model" ;
11- import { AuthService , CurrentUserInfoResponse } from "@auth/auth.service" ;
12- import { SharedVariableService } from "@shared/shared-variable/shared-variable.service" ;
10+ import { PermissionResponse , PermissionType } from "@app/admin/permission/permission.model" ;
1311import { MeService } from "@shared/services/me.service" ;
1412import { OrganizationAccessScope } from "@shared/enums/access-scopes" ;
13+ import { PermissionService } from "@app/admin/permission/permission.service" ;
14+ import { SharedVariableService } from "@shared/shared-variable/shared-variable.service" ;
1515
1616@Component ( {
1717 selector : "app-user-edit" ,
1818 templateUrl : "./user-edit.component.html" ,
1919 styleUrls : [ "./user-edit.component.scss" ] ,
2020} )
21- export class UserEditComponent implements OnInit {
22- user = new UserRequest ( ) ;
21+ export class UserEditComponent implements OnInit , OnDestroy {
22+ public user = new UserRequest ( ) ;
23+ public permissions : PermissionResponse [ ] ;
24+ public permissionsSubscription : Subscription ;
2325 public errorMessage : string ;
2426 public errorMessages : any ;
2527 public errorFields : string [ ] ;
@@ -28,20 +30,22 @@ export class UserEditComponent implements OnInit {
2830 public backButtonTitle = "" ;
2931 public title = "" ;
3032 public submitButton = "" ;
31- id : number ;
32- subscription : Subscription ;
33- isGlobalAdmin = false ;
34- isKombit : boolean ;
35- canEdit : boolean ;
33+ public id : number ;
34+ public subscription : Subscription ;
35+ public isGlobalAdmin = false ;
36+ public isKombit : boolean ;
37+ public canEdit : boolean ;
38+ public permissionMultiCtrl : UntypedFormControl = new UntypedFormControl ( ) ;
39+ private _onDestroy = new Subject < void > ( ) ;
3640
3741 constructor (
3842 private translate : TranslateService ,
3943 private route : ActivatedRoute ,
4044 private userService : UserService ,
4145 private location : Location ,
42- private authService : AuthService ,
43- private sharedVariableService : SharedVariableService ,
44- private meService : MeService
46+ private meService : MeService ,
47+ private permissionService : PermissionService ,
48+ private sharedVariableService : SharedVariableService
4549 ) { }
4650
4751 ngOnInit ( ) : void {
@@ -60,6 +64,7 @@ export class UserEditComponent implements OnInit {
6064 }
6165 this . amIGlobalAdmin ( ) ;
6266 this . canEdit = this . meService . hasAccessToTargetOrganization ( OrganizationAccessScope . UserAdministrationWrite ) ;
67+ this . getPermissions ( this . sharedVariableService . getUserInfo ( ) . user . id ) ;
6368 }
6469
6570 private getUser ( id : number ) {
@@ -70,6 +75,8 @@ export class UserEditComponent implements OnInit {
7075 this . user . active = response . active ;
7176 this . user . globalAdmin = response . permissions . some ( perm => perm . name === PermissionType . GlobalAdmin ) ;
7277 this . isKombit = response . nameId != null ;
78+ this . user . permissionIds = response . permissions . map ( pm => pm . id ) ;
79+
7380 // We cannot set the password.
7481 } ) ;
7582 }
@@ -80,8 +87,7 @@ export class UserEditComponent implements OnInit {
8087
8188 private create ( ) : void {
8289 this . userService . post ( this . user ) . subscribe (
83- response => {
84- console . log ( response ) ;
90+ ( ) => {
8591 this . routeBack ( ) ;
8692 } ,
8793 ( error : HttpErrorResponse ) => {
@@ -132,4 +138,28 @@ export class UserEditComponent implements OnInit {
132138 routeBack ( ) : void {
133139 this . location . back ( ) ;
134140 }
141+
142+ public compare ( matOptionValue : number , ngModelObject : number ) : boolean {
143+ return matOptionValue === ngModelObject ;
144+ }
145+
146+ private getPermissions ( userId : number ) {
147+ this . permissionsSubscription = this . permissionService
148+ . getPermissionsWithoutUsers ( this . meService . hasGlobalAdmin ( ) ? undefined : userId )
149+ . subscribe ( res => {
150+ this . permissions = res . data . sort ( ( a , b ) => a . name . localeCompare ( b . name , "da-DK" , { numeric : true } ) ) ;
151+ if ( ! this . id ) {
152+ this . permissionMultiCtrl . setValue ( this . user . permissionIds ) ;
153+ }
154+ } ) ;
155+ }
156+
157+ ngOnDestroy ( ) {
158+ // prevent memory leak by unsubscribing
159+ if ( this . permissionsSubscription ) {
160+ this . permissionsSubscription . unsubscribe ( ) ;
161+ }
162+ this . _onDestroy . next ( ) ;
163+ this . _onDestroy . complete ( ) ;
164+ }
135165}
0 commit comments