Skip to content

Commit 789e800

Browse files
committed
feat: added impersonated token
1 parent 308a582 commit 789e800

File tree

18 files changed

+249
-66
lines changed

18 files changed

+249
-66
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class ImpersonateInput {
2+
tenantImpersonationId?: string;
3+
userId?: string;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class ImpersonateOutput {
2+
impersonationToken: string;
3+
tenancyName: string;
4+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable, Injector } from '@angular/core';
2+
import { ImpersonateInput } from '../models/impersonate-input.model';
3+
import { ImpersonateOutput } from '../models/impersonate-output.model';
4+
import { ServiceApiBase } from '../../shared/services/service-base';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class AccountService extends ServiceApiBase {
10+
11+
constructor(
12+
injector: Injector
13+
) {
14+
super(injector, 'api/auth/account');
15+
}
16+
17+
impersonate(impersonateInput: ImpersonateInput) {
18+
return this.post<ImpersonateInput, ImpersonateOutput>('impersonate', impersonateInput);
19+
}
20+
21+
backToImpersonator() {
22+
return this.post<ImpersonateOutput>('back-to-impersonator');
23+
}
24+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { EntityState } from '@ngrx/entity';
12
import { PermissionDto } from './permission-dto.model';
23

3-
export class PermissionStoreModel {
4+
export interface PermissionStoreModel extends EntityState<PermissionDto> {
45
loading: boolean;
5-
permissions: PermissionDto[];
6+
total: number;
67
}
78

projects/craftsjs-app/src/app/@redux/permission/selectors/permission.selector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
import { createSelector } from '@ngrx/store';
33
import { PermissionStoreModel } from '../models/permission-store.model';
4+
import { selectAll } from '../stores/permission.store';
45

56
export const selectPermissionState = state => <PermissionStoreModel>state.permissions.store;
67

78
export const selectAllPermissions = createSelector(
89
selectPermissionState,
9-
store => store.permissions
10+
selectAll
1011
);
1112

1213
export const selectPermissionsLoading = createSelector(
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import { createEntityAdapter, EntityAdapter } from '@ngrx/entity';
2+
import { Store, Action } from '@craftsjs/ngrx-action';
13
import { PermissionStoreModel } from '../models/permission-store.model';
24
import * as PermissionActions from '../actions/permission.actions';
3-
import { Store, Action } from '@craftsjs/ngrx-action';
5+
import { PermissionDto } from '../models/permission-dto.model';
46

7+
export const adapter: EntityAdapter<PermissionDto> = createEntityAdapter<PermissionDto>();
58

6-
@Store<PermissionStoreModel>({
9+
const initialState = adapter.getInitialState({
710
loading: false,
8-
permissions: []
9-
})
11+
total: 0
12+
});
13+
14+
@Store<PermissionStoreModel>(initialState)
1015
export class PermissionStore {
1116

1217
@Action(PermissionActions.loadPermissions)
@@ -16,7 +21,15 @@ export class PermissionStore {
1621

1722
@Action(PermissionActions.permissionsLoaded)
1823
permissionsLoaded(state: PermissionStoreModel, { payload: { permissions } }) {
19-
return { ...state, permissions, loading: false };
24+
return adapter.upsertMany<PermissionStoreModel>(permissions, {
25+
...state,
26+
loading: false,
27+
total: permissions.length
28+
});
2029
}
2130

2231
}
32+
33+
export const {
34+
selectAll
35+
} = adapter.getSelectors();

projects/craftsjs-app/src/app/admin/role/component/role-form/role-form.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export class RoleFormComponent extends FormBase implements OnInit {
4444
}
4545

4646
private _initFinalStep() {
47-
console.log(this.roleDto.permissions);
4847
this.formFinalStep = this._fb.group({
4948
permissions: [this.roleDto.permissions]
5049
});

projects/craftsjs-app/src/app/admin/tenant/components/tenant-list/tenant-list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<button *ngIf="'Page.Tenants.Delete' | isGranted" mat-icon-button (click)="deleteTenant(tenant)">
1919
<mat-icon suffix>delete</mat-icon>
2020
</button>
21-
<button *ngIf="'Page.Tenants.Impersonation' | isGranted" mat-icon-button (click)="openModalImpersonation(tenant.id)"
21+
<button mat-icon-button (click)="openModalImpersonation(tenant.id)"
2222
[matTooltip]="'tenant.impersonation' | translateAsync" matTooltipPosition="above">
2323
<mat-icon suffix>supervisor_account</mat-icon>
2424
</button>

projects/craftsjs-app/src/app/admin/tenant/components/tenant-list/user-list-impersonation/user-list-impersonation.component.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { UserDto } from '@redux/user/models/user-dto.model';
44
import { BehaviorSubject, Subject } from 'rxjs';
55
import { Store } from '@ngrx/store';
66
import * as UserActions from '@redux/user/actions/user.actions';
7+
import { AuthService } from '@craftsjs/boilerplate';
8+
import { takeUntil } from 'rxjs/operators';
9+
import { Router } from '@angular/router';
710

811
@Component({
912
selector: 'app-user-list-impersonation',
@@ -22,7 +25,9 @@ export class UserListImpersonationComponent implements OnDestroy {
2225
constructor(
2326
private _dialogRef: MatDialogRef<UserListImpersonationComponent>,
2427
private _store: Store,
25-
@Inject(MAT_DIALOG_DATA) public tenantId: number
28+
@Inject(MAT_DIALOG_DATA) public tenantId: string,
29+
private _authService: AuthService,
30+
private _router: Router
2631
) { }
2732

2833
close() {
@@ -31,25 +36,14 @@ export class UserListImpersonationComponent implements OnDestroy {
3136

3237
impersonation(user: UserDto) {
3338
console.log(user);
34-
// this.saveSubject.next(true);
35-
// this._accountService.impersonate({ UserId: user.id, tenantId: this.tenantId }).pipe(
36-
// takeUntil(this.unsubscribeAll),
37-
// switchMap((result) => {
38-
// this._authService.setTenantCookie(this.tenantId);
39-
// return this._authService.impersonatedAuthenticate(result.impersonationToken).pipe(
40-
// switchMap(() => {
41-
// return this._sessionService.init().pipe(
42-
// tap(() => {
43-
// this.saveSubject.next(false);
44-
// this.close();
45-
// this._router.navigate(['admin/profile']);
46-
// })
47-
// );
48-
// })
49-
// );
50-
// }
51-
// )
52-
// ).subscribe();
39+
this.saveSubject.next(true);
40+
this._authService.impersonatedAuthenticate({ userId: user.id, tenantImpersonationId: this.tenantId }).pipe(
41+
takeUntil(this.unsubscribeAll)
42+
).subscribe(() => {
43+
this.saveSubject.next(false);
44+
this.close();
45+
this._router.navigate(['admin/profile']);
46+
});
5347
}
5448

5549
ngOnDestroy(): void {

projects/craftsjs-app/src/app/admin/user/components/user-list/user-list.component.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ChangeDetectionStrategy, Input, ContentChild, TemplateRef } from '@angular/core';
1+
import { Component, ChangeDetectionStrategy, Input, ContentChild, TemplateRef, OnInit } from '@angular/core';
22
import { UserDataSourceService } from '../../services/user-data-source.service';
33
import { GetUserDto } from '@redux/user/models/get-user-dto.model';
44
import { Store } from '@ngrx/store';
@@ -17,7 +17,7 @@ import { ListComponentBase } from '../../../../shared/list/list-component-base';
1717
UserActionService
1818
]
1919
})
20-
export class UserListComponent extends ListComponentBase<GetUserDto> {
20+
export class UserListComponent extends ListComponentBase<GetUserDto> implements OnInit {
2121

2222
@ContentChild(TemplateRef, { static: true }) templateRef: TemplateRef<any>;
2323

@@ -36,7 +36,19 @@ export class UserListComponent extends ListComponentBase<GetUserDto> {
3636
this.displayedColumns = userDataSourceService.displayedColumns;
3737
}
3838

39+
ngOnInit() {
40+
if(this.tenantId !== undefined) {
41+
const filter = Object.assign({}, this.filter.getValue(), {tenantId: this.tenantId});
42+
this.filter.next({ ...filter });
43+
}
44+
super.ngOnInit()
45+
}
46+
3947
search(filter: GetUserDto) {
48+
if(this.tenantId !== undefined) {
49+
const filter = Object.assign({}, this.filter.getValue(), {tenantId: this.tenantId});
50+
this.filter.next({ ...filter });
51+
}
4052
this.filter.next({ ...filter });
4153
this._store.dispatch(UserActions.userClearStore());
4254
}

0 commit comments

Comments
 (0)