fix: is default (#1737)

This commit is contained in:
Max Peintner 2021-05-17 11:18:51 +02:00 committed by GitHub
parent 4a388e410a
commit fe2342f15c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,14 +5,14 @@ import { Subscription } from 'rxjs';
import { switchMap } from 'rxjs/operators'; import { switchMap } from 'rxjs/operators';
import { LoginMethodComponentType } from 'src/app/modules/mfa-table/mfa-table.component'; import { LoginMethodComponentType } from 'src/app/modules/mfa-table/mfa-table.component';
import { import {
GetLoginPolicyResponse as AdminGetLoginPolicyResponse, GetLoginPolicyResponse as AdminGetLoginPolicyResponse,
UpdateLoginPolicyRequest, UpdateLoginPolicyRequest,
UpdateLoginPolicyResponse, UpdateLoginPolicyResponse,
} from 'src/app/proto/generated/zitadel/admin_pb'; } from 'src/app/proto/generated/zitadel/admin_pb';
import { IDP, IDPLoginPolicyLink, IDPOwnerType, IDPStylingType } from 'src/app/proto/generated/zitadel/idp_pb'; import { IDP, IDPLoginPolicyLink, IDPOwnerType, IDPStylingType } from 'src/app/proto/generated/zitadel/idp_pb';
import { import {
AddCustomLoginPolicyRequest, AddCustomLoginPolicyRequest,
GetLoginPolicyResponse as MgmtGetLoginPolicyResponse, GetLoginPolicyResponse as MgmtGetLoginPolicyResponse,
} from 'src/app/proto/generated/zitadel/management_pb'; } from 'src/app/proto/generated/zitadel/management_pb';
import { LoginPolicy, PasswordlessType } from 'src/app/proto/generated/zitadel/policy_pb'; import { LoginPolicy, PasswordlessType } from 'src/app/proto/generated/zitadel/policy_pb';
import { AdminService } from 'src/app/services/admin.service'; import { AdminService } from 'src/app/services/admin.service';
@ -21,232 +21,232 @@ import { ToastService } from 'src/app/services/toast.service';
import { CnslLinks } from '../../links/links.component'; import { CnslLinks } from '../../links/links.component';
import { import {
IAM_COMPLEXITY_LINK, IAM_COMPLEXITY_LINK,
IAM_LABEL_LINK, IAM_LABEL_LINK,
IAM_POLICY_LINK, IAM_POLICY_LINK,
ORG_COMPLEXITY_LINK, ORG_COMPLEXITY_LINK,
ORG_IAM_POLICY_LINK, ORG_IAM_POLICY_LINK,
} from '../../policy-grid/policy-links'; } from '../../policy-grid/policy-links';
import { PolicyComponentServiceType } from '../policy-component-types.enum'; import { PolicyComponentServiceType } from '../policy-component-types.enum';
import { AddIdpDialogComponent } from './add-idp-dialog/add-idp-dialog.component'; import { AddIdpDialogComponent } from './add-idp-dialog/add-idp-dialog.component';
@Component({ @Component({
selector: 'app-login-policy', selector: 'app-login-policy',
templateUrl: './login-policy.component.html', templateUrl: './login-policy.component.html',
styleUrls: ['./login-policy.component.scss'], styleUrls: ['./login-policy.component.scss'],
}) })
export class LoginPolicyComponent implements OnDestroy { export class LoginPolicyComponent implements OnDestroy {
public LoginMethodComponentType: any = LoginMethodComponentType; public LoginMethodComponentType: any = LoginMethodComponentType;
public passwordlessTypes: Array<PasswordlessType> = []; public passwordlessTypes: Array<PasswordlessType> = [];
public loginData!: LoginPolicy.AsObject; public loginData!: LoginPolicy.AsObject;
private sub: Subscription = new Subscription(); private sub: Subscription = new Subscription();
public service!: ManagementService | AdminService; public service!: ManagementService | AdminService;
public PolicyComponentServiceType: any = PolicyComponentServiceType; public PolicyComponentServiceType: any = PolicyComponentServiceType;
public serviceType: PolicyComponentServiceType = PolicyComponentServiceType.MGMT; public serviceType: PolicyComponentServiceType = PolicyComponentServiceType.MGMT;
public idps: IDPLoginPolicyLink.AsObject[] = []; public idps: IDPLoginPolicyLink.AsObject[] = [];
public loading: boolean = false; public loading: boolean = false;
public disabled: boolean = true; public disabled: boolean = true;
public IDPStylingType: any = IDPStylingType; public IDPStylingType: any = IDPStylingType;
public nextLinks: CnslLinks[] = []; public nextLinks: CnslLinks[] = [];
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private toast: ToastService, private toast: ToastService,
private dialog: MatDialog, private dialog: MatDialog,
private injector: Injector, private injector: Injector,
) { ) {
this.sub = this.route.data.pipe(switchMap(data => { this.sub = this.route.data.pipe(switchMap(data => {
this.serviceType = data.serviceType; this.serviceType = data.serviceType;
switch (this.serviceType) { switch (this.serviceType) {
case PolicyComponentServiceType.MGMT: case PolicyComponentServiceType.MGMT:
this.service = this.injector.get(ManagementService as Type<ManagementService>); this.service = this.injector.get(ManagementService as Type<ManagementService>);
this.passwordlessTypes = [ this.passwordlessTypes = [
PasswordlessType.PASSWORDLESS_TYPE_ALLOWED, PasswordlessType.PASSWORDLESS_TYPE_ALLOWED,
PasswordlessType.PASSWORDLESS_TYPE_NOT_ALLOWED, PasswordlessType.PASSWORDLESS_TYPE_NOT_ALLOWED,
]; ];
this.nextLinks = [ this.nextLinks = [
ORG_COMPLEXITY_LINK, ORG_COMPLEXITY_LINK,
ORG_IAM_POLICY_LINK, ORG_IAM_POLICY_LINK,
]; ];
break; break;
case PolicyComponentServiceType.ADMIN: case PolicyComponentServiceType.ADMIN:
this.service = this.injector.get(AdminService as Type<AdminService>); this.service = this.injector.get(AdminService as Type<AdminService>);
this.passwordlessTypes = [ this.passwordlessTypes = [
PasswordlessType.PASSWORDLESS_TYPE_ALLOWED, PasswordlessType.PASSWORDLESS_TYPE_ALLOWED,
PasswordlessType.PASSWORDLESS_TYPE_NOT_ALLOWED, PasswordlessType.PASSWORDLESS_TYPE_NOT_ALLOWED,
]; ];
this.nextLinks = [ this.nextLinks = [
IAM_COMPLEXITY_LINK, IAM_COMPLEXITY_LINK,
IAM_POLICY_LINK, IAM_POLICY_LINK,
IAM_LABEL_LINK, IAM_LABEL_LINK,
]; ];
break; break;
} }
return this.route.params; return this.route.params;
})).subscribe(() => { })).subscribe(() => {
this.fetchData(); this.fetchData();
}); });
}
private fetchData(): void {
this.getData().then(resp => {
if (resp.policy) {
this.loginData = resp.policy;
this.loading = false;
this.disabled = this.isDefault ?? false;
}
});
this.getIdps().then(resp => {
this.idps = resp;
});
}
public ngOnDestroy(): void {
this.sub.unsubscribe();
}
private async getData():
Promise<AdminGetLoginPolicyResponse.AsObject | MgmtGetLoginPolicyResponse.AsObject> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).getLoginPolicy();
case PolicyComponentServiceType.ADMIN:
return (this.service as AdminService).getLoginPolicy();
} }
}
private fetchData(): void { private async getIdps(): Promise<IDPLoginPolicyLink.AsObject[]> {
this.getData().then(resp => { switch (this.serviceType) {
if (resp.policy) { case PolicyComponentServiceType.MGMT:
this.loginData = resp.policy; return (this.service as ManagementService).listLoginPolicyIDPs()
this.loading = false; .then((resp) => {
this.disabled = ((this.loginData as LoginPolicy.AsObject)?.isDefault) ?? false; return resp.resultList;
} });
}); case PolicyComponentServiceType.ADMIN:
this.getIdps().then(resp => { return (this.service as AdminService).listLoginPolicyIDPs()
this.idps = resp; .then((providers) => {
}); return providers.resultList;
});
} }
}
public ngOnDestroy(): void { private async updateData():
this.sub.unsubscribe(); Promise<UpdateLoginPolicyResponse.AsObject> {
} switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
private async getData(): const mgmtreq = new AddCustomLoginPolicyRequest();
Promise<AdminGetLoginPolicyResponse.AsObject | MgmtGetLoginPolicyResponse.AsObject> { mgmtreq.setAllowExternalIdp(this.loginData.allowExternalIdp);
switch (this.serviceType) { mgmtreq.setAllowRegister(this.loginData.allowRegister);
case PolicyComponentServiceType.MGMT: mgmtreq.setAllowUsernamePassword(this.loginData.allowUsernamePassword);
return (this.service as ManagementService).getLoginPolicy(); mgmtreq.setForceMfa(this.loginData.forceMfa);
case PolicyComponentServiceType.ADMIN: mgmtreq.setPasswordlessType(this.loginData.passwordlessType);
return (this.service as AdminService).getLoginPolicy(); if ((this.loginData as LoginPolicy.AsObject).isDefault) {
} return (this.service as ManagementService).addCustomLoginPolicy(mgmtreq);
}
private async getIdps(): Promise<IDPLoginPolicyLink.AsObject[]> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).listLoginPolicyIDPs()
.then((resp) => {
return resp.resultList;
});
case PolicyComponentServiceType.ADMIN:
return (this.service as AdminService).listLoginPolicyIDPs()
.then((providers) => {
return providers.resultList;
});
}
}
private async updateData():
Promise<UpdateLoginPolicyResponse.AsObject> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
const mgmtreq = new AddCustomLoginPolicyRequest();
mgmtreq.setAllowExternalIdp(this.loginData.allowExternalIdp);
mgmtreq.setAllowRegister(this.loginData.allowRegister);
mgmtreq.setAllowUsernamePassword(this.loginData.allowUsernamePassword);
mgmtreq.setForceMfa(this.loginData.forceMfa);
mgmtreq.setPasswordlessType(this.loginData.passwordlessType);
if ((this.loginData as LoginPolicy.AsObject).isDefault) {
return (this.service as ManagementService).addCustomLoginPolicy(mgmtreq);
} else {
return (this.service as ManagementService).updateCustomLoginPolicy(mgmtreq);
}
case PolicyComponentServiceType.ADMIN:
const adminreq = new UpdateLoginPolicyRequest();
adminreq.setAllowExternalIdp(this.loginData.allowExternalIdp);
adminreq.setAllowRegister(this.loginData.allowRegister);
adminreq.setAllowUsernamePassword(this.loginData.allowUsernamePassword);
adminreq.setForceMfa(this.loginData.forceMfa);
adminreq.setPasswordlessType(this.loginData.passwordlessType);
return (this.service as AdminService).updateLoginPolicy(adminreq);
}
}
public savePolicy(): void {
this.updateData().then(() => {
this.toast.showInfo('POLICY.LOGIN_POLICY.SAVED', true);
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
public removePolicy(): void {
if (this.serviceType === PolicyComponentServiceType.MGMT) {
(this.service as ManagementService).resetLoginPolicyToDefault().then(() => {
this.toast.showInfo('POLICY.TOAST.RESETSUCCESS', true);
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
}
public openDialog(): void {
const dialogRef = this.dialog.open(AddIdpDialogComponent, {
data: {
serviceType: this.serviceType,
},
width: '400px',
});
dialogRef.afterClosed().subscribe(resp => {
if (resp && resp.idp && resp.type) {
this.addIdp(resp.idp, resp.type).then(() => {
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
});
}
private addIdp(idp: IDP.AsObject | IDP.AsObject, ownerType: IDPOwnerType): Promise<any> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).addIDPToLoginPolicy(idp.id, ownerType);
case PolicyComponentServiceType.ADMIN:
return (this.service as AdminService).addIDPToLoginPolicy(idp.id);
}
}
public removeIdp(idp: IDPLoginPolicyLink.AsObject): void {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
(this.service as ManagementService).removeIDPFromLoginPolicy(idp.idpId).then(() => {
const index = this.idps.findIndex(temp => temp === idp);
if (index > -1) {
this.idps.splice(index, 1);
}
}, error => {
this.toast.showError(error);
});
break;
case PolicyComponentServiceType.ADMIN:
(this.service as AdminService).removeIDPFromLoginPolicy(idp.idpId).then(() => {
const index = this.idps.findIndex(temp => temp === idp);
if (index > -1) {
this.idps.splice(index, 1);
}
}, error => {
this.toast.showError(error);
});
break;
}
}
public get isDefault(): boolean {
if (this.loginData && this.serviceType === PolicyComponentServiceType.MGMT) {
return (this.loginData as LoginPolicy.AsObject).isDefault;
} else { } else {
return false; return (this.service as ManagementService).updateCustomLoginPolicy(mgmtreq);
} }
case PolicyComponentServiceType.ADMIN:
const adminreq = new UpdateLoginPolicyRequest();
adminreq.setAllowExternalIdp(this.loginData.allowExternalIdp);
adminreq.setAllowRegister(this.loginData.allowRegister);
adminreq.setAllowUsernamePassword(this.loginData.allowUsernamePassword);
adminreq.setForceMfa(this.loginData.forceMfa);
adminreq.setPasswordlessType(this.loginData.passwordlessType);
return (this.service as AdminService).updateLoginPolicy(adminreq);
} }
}
public savePolicy(): void {
this.updateData().then(() => {
this.toast.showInfo('POLICY.LOGIN_POLICY.SAVED', true);
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
public removePolicy(): void {
if (this.serviceType === PolicyComponentServiceType.MGMT) {
(this.service as ManagementService).resetLoginPolicyToDefault().then(() => {
this.toast.showInfo('POLICY.TOAST.RESETSUCCESS', true);
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
}
public openDialog(): void {
const dialogRef = this.dialog.open(AddIdpDialogComponent, {
data: {
serviceType: this.serviceType,
},
width: '400px',
});
dialogRef.afterClosed().subscribe(resp => {
if (resp && resp.idp && resp.type) {
this.addIdp(resp.idp, resp.type).then(() => {
this.loading = true;
setTimeout(() => {
this.fetchData();
}, 2000);
}).catch(error => {
this.toast.showError(error);
});
}
});
}
private addIdp(idp: IDP.AsObject | IDP.AsObject, ownerType: IDPOwnerType): Promise<any> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).addIDPToLoginPolicy(idp.id, ownerType);
case PolicyComponentServiceType.ADMIN:
return (this.service as AdminService).addIDPToLoginPolicy(idp.id);
}
}
public removeIdp(idp: IDPLoginPolicyLink.AsObject): void {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
(this.service as ManagementService).removeIDPFromLoginPolicy(idp.idpId).then(() => {
const index = this.idps.findIndex(temp => temp === idp);
if (index > -1) {
this.idps.splice(index, 1);
}
}, error => {
this.toast.showError(error);
});
break;
case PolicyComponentServiceType.ADMIN:
(this.service as AdminService).removeIDPFromLoginPolicy(idp.idpId).then(() => {
const index = this.idps.findIndex(temp => temp === idp);
if (index > -1) {
this.idps.splice(index, 1);
}
}, error => {
this.toast.showError(error);
});
break;
}
}
public get isDefault(): boolean {
if (this.loginData && this.serviceType === PolicyComponentServiceType.MGMT) {
return (this.loginData as LoginPolicy.AsObject).isDefault;
} else {
return false;
}
}
} }