Files
zitadel/console/src/app/services/auth-user.service.ts
T
Max PeintnerandGitHub 4688543d07 feat(console): getuserbyid, fix password policy validation, read iam (#233)
* remove displayname, show policy desc on patternerr

* provide password complexity string, org create

* user getuserbyid in userdetail, remove displayname

* show multiple org domains

* show zitadel project warnings, read iam

* add missing ngondestroy impl
2020-06-17 14:30:21 +02:00

283 lines
8.2 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Empty } from 'google-protobuf/google/protobuf/empty_pb';
import { Metadata } from 'grpc-web';
import { from, Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { AuthServicePromiseClient } from '../proto/generated/auth_grpc_web_pb';
import {
MfaOtpResponse,
MultiFactors,
MyProjectOrgSearchQuery,
MyProjectOrgSearchRequest,
MyProjectOrgSearchResponse,
PasswordChange,
UpdateUserAddressRequest,
UpdateUserEmailRequest,
UpdateUserPhoneRequest,
UpdateUserProfileRequest,
UserAddress,
UserEmail,
UserPhone,
UserProfile,
UserSessionViews,
VerifyMfaOtp,
VerifyUserPhoneRequest,
} from '../proto/generated/auth_pb';
import { GrpcBackendService } from './grpc-backend.service';
import { GrpcService, RequestFactory, ResponseMapper } from './grpc.service';
@Injectable({
providedIn: 'root',
})
export class AuthUserService {
private _roleCache: string[] = [];
constructor(private readonly grpcClient: GrpcService,
private grpcBackendService: GrpcBackendService,
) { }
public async request<TReq, TResp, TMappedResp>(
requestFn: RequestFactory<AuthServicePromiseClient, TReq, TResp>,
request: TReq,
responseMapper: ResponseMapper<TResp, TMappedResp>,
metadata?: Metadata,
): Promise<TMappedResp> {
const mappedRequestFn = requestFn(this.grpcClient.auth).bind(this.grpcClient.auth);
const response = await this.grpcBackendService.runRequest(
mappedRequestFn,
request,
metadata,
);
return responseMapper(response);
}
public async GetMyUserProfile(): Promise<UserProfile> {
return await this.request(
c => c.getMyUserProfile,
new Empty(),
f => f,
);
}
public async GetMyMfas(): Promise<MultiFactors> {
return await this.request(
c => c.getMyMfas,
new Empty(),
f => f,
);
}
public async SearchMyProjectOrgs(
limit: number,
offset: number,
queryList?: MyProjectOrgSearchQuery[],
): Promise<MyProjectOrgSearchResponse> {
const req: MyProjectOrgSearchRequest = new MyProjectOrgSearchRequest();
req.setOffset(offset);
req.setLimit(limit);
if (queryList) {
req.setQueriesList(queryList);
}
return await this.request(
c => c.searchMyProjectOrgs,
req,
f => f,
);
}
public async SaveMyUserProfile(profile: UserProfile.AsObject): Promise<UserProfile> {
const req = new UpdateUserProfileRequest();
req.setFirstName(profile.firstName);
req.setLastName(profile.lastName);
req.setNickName(profile.nickName);
req.setPreferredLanguage(profile.preferredLanguage);
req.setGender(profile.gender);
console.log(req.toObject());
return await this.request(
c => c.updateMyUserProfile,
req,
f => f,
);
}
public async getMyUserSessions(): Promise<UserSessionViews> {
return await this.request(
c => c.getMyUserSessions,
new Empty(),
f => f,
);
}
public async GetMyUserEmail(): Promise<UserEmail> {
return await this.request(
c => c.getMyUserEmail,
new Empty(),
f => f,
);
}
public async SaveMyUserEmail(email: UserEmail.AsObject): Promise<UserEmail> {
const req = new UpdateUserEmailRequest();
req.setEmail(email.email);
return await this.request(
c => c.changeMyUserEmail,
req,
f => f,
);
}
private async getMyzitadelPermissions(): Promise<any> {
return await this.request(
c => c.getMyZitadelPermissions,
new Empty(),
f => f,
);
}
public GetMyzitadelPermissions(): Observable<any> {
return from(this.getMyzitadelPermissions());
}
public hasRoles(userRoles: string[], requestedRoles: string[], each: boolean = false): boolean {
return each ?
requestedRoles.every(role => userRoles.includes(role)) :
requestedRoles.findIndex(role => {
return userRoles.findIndex(i => i.includes(role)) > -1;
// return userRoles.includes(role);
}) > -1;
}
public async GetMyUserPhone(): Promise<UserPhone> {
// return this.grpcClient.auth.getMyUserPhone(new Empty());
return await this.request(
c => c.getMyUserPhone,
new Empty(),
(f: UserPhone) => f,
);
}
public async SaveMyUserPhone(phone: UserPhone.AsObject): Promise<UserPhone> {
const req = new UpdateUserPhoneRequest();
req.setPhone(phone.phone);
return await this.request(
c => c.changeMyUserPhone,
req,
f => f,
);
}
public async GetMyUserAddress(): Promise<UserAddress> {
return await this.request(
c => c.getMyUserAddress,
new Empty(),
f => f,
);
}
public async ResendEmailVerification(): Promise<Empty> {
const req = new Empty();
return await this.request(
c => c.resendMyEmailVerificationMail,
req,
f => f,
);
}
public async ResendPhoneVerification(): Promise<Empty> {
const req = new Empty();
return await this.request(
c => c.resendMyPhoneVerificationCode,
req,
f => f,
);
}
public async ChangeMyPassword(oldPassword: string, newPassword: string): Promise<Empty> {
const req = new PasswordChange();
req.setOldPassword(oldPassword);
req.setNewPassword(newPassword);
return await this.request(
c => c.changeMyPassword,
req,
f => f,
);
}
public async AddMfaOTP(): Promise<MfaOtpResponse> {
return await this.request(
c => c.addMfaOTP,
new Empty(),
f => f,
);
}
public async RemoveMfaOTP(): Promise<Empty> {
return await this.request(
c => c.removeMfaOTP,
new Empty(),
f => f,
);
}
public async VerifyMfaOTP(code: string): Promise<Empty> {
const req = new VerifyMfaOtp();
req.setCode(code);
return await this.request(
c => c.verifyMfaOTP,
req,
f => f,
);
}
public async VerifyMyUserPhone(code: string): Promise<Empty> {
const req = new VerifyUserPhoneRequest();
req.setCode(code);
return await this.request(
c => c.verifyMyUserPhone,
req,
f => f,
);
}
public async SaveMyUserAddress(address: UserAddress.AsObject): Promise<UserAddress> {
const req = new UpdateUserAddressRequest();
req.setStreetAddress(address.streetAddress);
req.setPostalCode(address.postalCode);
req.setLocality(address.locality);
req.setRegion(address.region);
req.setCountry(address.country);
return await this.request(
c => c.updateMyUserAddress,
req,
f => f,
);
}
public isAllowed(roles: string[], each: boolean = false): Observable<boolean> {
if (roles && roles.length > 0) {
if (this._roleCache.length > 0) {
return of(this.hasRoles(this._roleCache, roles));
}
return this.GetMyzitadelPermissions().pipe(
switchMap(response => {
let userRoles = [];
if (response.toObject().permissionsList) {
userRoles = response.toObject().permissionsList;
} else {
userRoles = ['user.resourceowner'];
}
this._roleCache = userRoles;
return of(this.hasRoles(userRoles, roles, each));
}),
);
} else {
return of(false);
}
}
}