mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-15 20:11:30 +01:00
Refactor accept-organization and accept-emergency (#1026)
This commit is contained in:
parent
b8aff13ce2
commit
14e9784297
@ -1,7 +1,4 @@
|
||||
import {
|
||||
Component,
|
||||
OnInit,
|
||||
} from '@angular/core';
|
||||
import { Component } from '@angular/core';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
Router,
|
||||
@ -17,39 +14,30 @@ import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
||||
import { StateService } from 'jslib-common/abstractions/state.service';
|
||||
import { UserService } from 'jslib-common/abstractions/user.service';
|
||||
import { EmergencyAccessAcceptRequest } from 'jslib-common/models/request/emergencyAccessAcceptRequest';
|
||||
import { BaseAcceptComponent } from '../common/base.accept.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-accept-emergency',
|
||||
templateUrl: 'accept-emergency.component.html',
|
||||
})
|
||||
export class AcceptEmergencyComponent implements OnInit {
|
||||
loading = true;
|
||||
authed = false;
|
||||
export class AcceptEmergencyComponent extends BaseAcceptComponent {
|
||||
|
||||
name: string;
|
||||
email: string;
|
||||
actionPromise: Promise<any>;
|
||||
|
||||
constructor(private router: Router, private toasterService: ToasterService,
|
||||
private i18nService: I18nService, private route: ActivatedRoute,
|
||||
private apiService: ApiService, private userService: UserService,
|
||||
private stateService: StateService) { }
|
||||
protected requiredParameters: string[] = ['id', 'name', 'email', 'token'];
|
||||
protected failedShortMessage = 'emergencyInviteAcceptFailedShort';
|
||||
protected failedMessage = 'emergencyInviteAcceptFailed';
|
||||
|
||||
ngOnInit() {
|
||||
let fired = false;
|
||||
this.route.queryParams.subscribe(async qParams => {
|
||||
if (fired) {
|
||||
return;
|
||||
constructor(router: Router, toasterService: ToasterService,
|
||||
i18nService: I18nService, route: ActivatedRoute,
|
||||
private apiService: ApiService, userService: UserService,
|
||||
stateService: StateService) {
|
||||
super(router, toasterService, i18nService, route, userService, stateService);
|
||||
}
|
||||
fired = true;
|
||||
await this.stateService.remove('emergencyInvitation');
|
||||
let error = qParams.id == null || qParams.name == null || qParams.email == null || qParams.token == null;
|
||||
let errorMessage: string = null;
|
||||
if (!error) {
|
||||
this.authed = await this.userService.isAuthenticated();
|
||||
if (this.authed) {
|
||||
|
||||
async authedHandler(qParams: any): Promise<void> {
|
||||
const request = new EmergencyAccessAcceptRequest();
|
||||
request.token = qParams.token;
|
||||
try {
|
||||
this.actionPromise = this.apiService.postEmergencyAccessAccept(qParams.id, request);
|
||||
await this.actionPromise;
|
||||
const toast: Toast = {
|
||||
@ -60,34 +48,13 @@ export class AcceptEmergencyComponent implements OnInit {
|
||||
};
|
||||
this.toasterService.popAsync(toast);
|
||||
this.router.navigate(['/vault']);
|
||||
} catch (e) {
|
||||
error = true;
|
||||
errorMessage = e.message;
|
||||
}
|
||||
} else {
|
||||
await this.stateService.save('emergencyInvitation', qParams);
|
||||
this.email = qParams.email;
|
||||
|
||||
async unauthedHandler(qParams: any): Promise<void> {
|
||||
this.name = qParams.name;
|
||||
if (this.name != null) {
|
||||
// Fix URL encoding of space issue with Angular
|
||||
this.name = this.name.replace(/\+/g, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
const toast: Toast = {
|
||||
type: 'error',
|
||||
title: null,
|
||||
body: errorMessage != null ? this.i18nService.t('emergencyInviteAcceptFailedShort', errorMessage) :
|
||||
this.i18nService.t('emergencyInviteAcceptFailed'),
|
||||
timeout: 10000,
|
||||
};
|
||||
this.toasterService.popAsync(toast);
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
import {
|
||||
Component,
|
||||
OnInit,
|
||||
} from '@angular/core';
|
||||
|
||||
import { Component } from '@angular/core';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
Router,
|
||||
@ -25,40 +21,28 @@ import { OrganizationUserResetPasswordEnrollmentRequest } from 'jslib-common/mod
|
||||
|
||||
import { Utils } from 'jslib-common/misc/utils';
|
||||
import { Policy } from 'jslib-common/models/domain/policy';
|
||||
import { BaseAcceptComponent } from '../common/base.accept.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-accept-organization',
|
||||
templateUrl: 'accept-organization.component.html',
|
||||
})
|
||||
export class AcceptOrganizationComponent implements OnInit {
|
||||
loading = true;
|
||||
authed = false;
|
||||
export class AcceptOrganizationComponent extends BaseAcceptComponent {
|
||||
orgName: string;
|
||||
email: string;
|
||||
actionPromise: Promise<any>;
|
||||
|
||||
constructor(private router: Router, private toasterService: ToasterService,
|
||||
private i18nService: I18nService, private route: ActivatedRoute,
|
||||
private apiService: ApiService, private userService: UserService,
|
||||
private stateService: StateService, private cryptoService: CryptoService,
|
||||
private policyService: PolicyService) { }
|
||||
protected requiredParameters: string[] = ['organizationId', 'organizationUserId', 'token'];
|
||||
|
||||
ngOnInit() {
|
||||
let fired = false;
|
||||
this.route.queryParams.subscribe(async qParams => {
|
||||
if (fired) {
|
||||
return;
|
||||
constructor(router: Router, toasterService: ToasterService,
|
||||
i18nService: I18nService, route: ActivatedRoute,
|
||||
private apiService: ApiService, userService: UserService,
|
||||
stateService: StateService, private cryptoService: CryptoService,
|
||||
private policyService: PolicyService) {
|
||||
super(router, toasterService, i18nService, route, userService, stateService);
|
||||
}
|
||||
fired = true;
|
||||
await this.stateService.remove('orgInvitation');
|
||||
let error = qParams.organizationId == null || qParams.organizationUserId == null || qParams.token == null;
|
||||
let errorMessage: string = null;
|
||||
if (!error) {
|
||||
this.authed = await this.userService.isAuthenticated();
|
||||
if (this.authed) {
|
||||
|
||||
async authedHandler(qParams: any): Promise<void> {
|
||||
const request = new OrganizationUserAcceptRequest();
|
||||
request.token = qParams.token;
|
||||
try {
|
||||
if (await this.performResetPasswordAutoEnroll(qParams)) {
|
||||
this.actionPromise = this.apiService.postOrganizationUserAccept(qParams.organizationId,
|
||||
qParams.organizationUserId, request).then(() => {
|
||||
@ -98,36 +82,15 @@ export class AcceptOrganizationComponent implements OnInit {
|
||||
};
|
||||
this.toasterService.popAsync(toast);
|
||||
this.router.navigate(['/vault']);
|
||||
} catch (e) {
|
||||
error = true;
|
||||
errorMessage = e.message;
|
||||
}
|
||||
} else {
|
||||
await this.stateService.save('orgInvitation', qParams);
|
||||
this.email = qParams.email;
|
||||
|
||||
async unauthedHandler(qParams: any): Promise<void> {
|
||||
this.orgName = qParams.organizationName;
|
||||
if (this.orgName != null) {
|
||||
// Fix URL encoding of space issue with Angular
|
||||
this.orgName = this.orgName.replace(/\+/g, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
const toast: Toast = {
|
||||
type: 'error',
|
||||
title: null,
|
||||
body: errorMessage != null ? this.i18nService.t('inviteAcceptFailedShort', errorMessage) :
|
||||
this.i18nService.t('inviteAcceptFailed'),
|
||||
timeout: 10000,
|
||||
};
|
||||
this.toasterService.popAsync(toast);
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
private async performResetPasswordAutoEnroll(qParams: any): Promise<boolean> {
|
||||
let policyList: Policy[] = null;
|
||||
|
@ -76,13 +76,6 @@ export class LoginComponent extends BaseLoginComponent {
|
||||
}
|
||||
|
||||
async goAfterLogIn() {
|
||||
const orgInvite = await this.stateService.get<any>('orgInvitation');
|
||||
const emergencyInvite = await this.stateService.get<any>('emergencyInvitation');
|
||||
if (orgInvite != null) {
|
||||
this.router.navigate(['accept-organization'], { queryParams: orgInvite });
|
||||
} else if (emergencyInvite != null) {
|
||||
this.router.navigate(['accept-emergency'], { queryParams: emergencyInvite });
|
||||
} else {
|
||||
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
||||
if (loginRedirect != null) {
|
||||
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
||||
@ -91,5 +84,4 @@ export class LoginComponent extends BaseLoginComponent {
|
||||
this.router.navigate([this.successRoute]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,13 +60,6 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
|
||||
}
|
||||
|
||||
async goAfterLogIn() {
|
||||
const orgInvite = await this.stateService.get<any>('orgInvitation');
|
||||
const emergencyInvite = await this.stateService.get<any>('emergencyInvitation');
|
||||
if (orgInvite != null) {
|
||||
this.router.navigate(['accept-organization'], { queryParams: orgInvite });
|
||||
} else if (emergencyInvite != null) {
|
||||
this.router.navigate(['accept-emergency'], { queryParams: emergencyInvite });
|
||||
} else {
|
||||
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
||||
if (loginRedirect != null) {
|
||||
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
||||
@ -79,5 +72,4 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
90
src/app/common/base.accept.component.ts
Normal file
90
src/app/common/base.accept.component.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import {
|
||||
Directive,
|
||||
OnInit,
|
||||
} from '@angular/core';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
Router,
|
||||
} from '@angular/router';
|
||||
|
||||
import {
|
||||
Toast,
|
||||
ToasterService,
|
||||
} from 'angular2-toaster';
|
||||
|
||||
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
||||
import { StateService } from 'jslib-common/abstractions/state.service';
|
||||
import { UserService } from 'jslib-common/abstractions/user.service';
|
||||
|
||||
@Directive()
|
||||
export abstract class BaseAcceptComponent implements OnInit {
|
||||
loading = true;
|
||||
authed = false;
|
||||
email: string;
|
||||
actionPromise: Promise<any>;
|
||||
|
||||
protected requiredParameters: string[] = [];
|
||||
protected failedShortMessage = 'inviteAcceptFailedShort';
|
||||
protected failedMessage = 'inviteAcceptFailed';
|
||||
|
||||
constructor(protected router: Router, protected toasterService: ToasterService,
|
||||
protected i18nService: I18nService, protected route: ActivatedRoute,
|
||||
protected userService: UserService, private stateService: StateService) { }
|
||||
|
||||
abstract authedHandler(qParams: any): Promise<void>;
|
||||
abstract unauthedHandler(qParams: any): Promise<void>;
|
||||
|
||||
ngOnInit() {
|
||||
let fired = false;
|
||||
this.route.queryParams.subscribe(async qParams => {
|
||||
if (fired) {
|
||||
return;
|
||||
}
|
||||
fired = true;
|
||||
await this.stateService.remove('loginRedirect');
|
||||
|
||||
let error = this.requiredParameters.some(e => qParams?.[e] == null || qParams[e] === '');
|
||||
let errorMessage: string = null;
|
||||
if (!error) {
|
||||
this.authed = await this.userService.isAuthenticated();
|
||||
|
||||
if (this.authed) {
|
||||
try {
|
||||
await this.authedHandler(qParams);
|
||||
} catch (e) {
|
||||
error = true;
|
||||
errorMessage = e.message;
|
||||
}
|
||||
} else {
|
||||
await this.stateService.save('loginRedirect', {
|
||||
route: this.getRedirectRoute(),
|
||||
qParams: qParams,
|
||||
});
|
||||
|
||||
this.email = qParams.email;
|
||||
await this.unauthedHandler(qParams);
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
const toast: Toast = {
|
||||
type: 'error',
|
||||
title: null,
|
||||
body: errorMessage != null ? this.i18nService.t(this.failedShortMessage, errorMessage) :
|
||||
this.i18nService.t(this.failedMessage),
|
||||
timeout: 10000,
|
||||
};
|
||||
this.toasterService.popAsync(toast);
|
||||
this.router.navigate(['/']);
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
getRedirectRoute() {
|
||||
const urlTree = this.router.parseUrl(this.router.url);
|
||||
urlTree.queryParams = {};
|
||||
return urlTree.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user