-
Notifications
You must be signed in to change notification settings - Fork 55
/
current-user.repository.ts
141 lines (115 loc) · 4.05 KB
/
current-user.repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { UmbCurrentUserServerDataSource } from './current-user.server.data-source.js';
import { UMB_CURRENT_USER_STORE_CONTEXT } from './current-user.store.token.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
import type { UmbNotificationContext } from '@umbraco-cms/backoffice/notification';
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
/**
* A repository for the current user
* @class UmbCurrentUserRepository
* @augments {UmbRepositoryBase}
*/
export class UmbCurrentUserRepository extends UmbRepositoryBase {
#currentUserSource = new UmbCurrentUserServerDataSource(this._host);
#currentUserStore?: typeof UMB_CURRENT_USER_STORE_CONTEXT.TYPE;
#init: Promise<unknown>;
protected notificationContext?: UmbNotificationContext;
constructor(host: UmbControllerHost) {
super(host);
this.#init = Promise.all([
this.consumeContext(UMB_CURRENT_USER_STORE_CONTEXT, (instance) => {
this.#currentUserStore = instance;
}).asPromise(),
this.consumeContext(UMB_NOTIFICATION_CONTEXT, (instance) => {
this.notificationContext = instance;
}).asPromise(),
]);
}
/**
* Request the current user
* @returns {*}
* @memberof UmbCurrentUserRepository
*/
async requestCurrentUser() {
await this.#init;
const { data, error } = await this.#currentUserSource.getCurrentUser();
if (data) {
this.#currentUserStore?.set(data);
}
return { data, error, asObservable: () => this.#currentUserStore!.data };
}
/**
* Request the current user's external login providers
* @memberof UmbCurrentUserRepository
*/
async requestExternalLoginProviders() {
await this.#init;
const { data, error } = await this.#currentUserSource.getExternalLoginProviders();
if (data) {
this.#currentUserStore?.setExternalLoginProviders(data);
}
return { data, error, asObservable: () => this.#currentUserStore!.externalLoginProviders };
}
/**
* Request the current user's available MFA login providers
* @memberof UmbCurrentUserRepository
*/
async requestMfaLoginProviders() {
await this.#init;
const { data, error } = await this.#currentUserSource.getMfaLoginProviders();
if (data) {
this.#currentUserStore?.setMfaProviders(data);
}
return { data, error, asObservable: () => this.#currentUserStore!.mfaProviders };
}
/**
* Enable an MFA provider
* @param provider The provider to enable
* @param providerName
* @param code The activation code of the provider to enable
* @param secret
* @memberof UmbCurrentUserRepository
*/
async enableMfaProvider(providerName: string, code: string, secret: string) {
const { error } = await this.#currentUserSource.enableMfaProvider(providerName, code, secret);
if (error) {
return { error };
}
this.#currentUserStore?.updateMfaProvider({ providerName, isEnabledOnUser: true });
return {};
}
/**
* Disable an MFA provider
* @param provider The provider to disable
* @param providerName
* @param code The activation code of the provider to disable
* @memberof UmbCurrentUserRepository
*/
async disableMfaProvider(providerName: string, code: string) {
const { error } = await this.#currentUserSource.disableMfaProvider(providerName, code);
if (error) {
return { error };
}
this.#currentUserStore?.updateMfaProvider({ providerName, isEnabledOnUser: false });
return {};
}
/**
* Change password for current user
* @param userId
* @param newPassword
* @param oldPassword
* @param isCurrentUser
* @returns
*/
async changePassword(newPassword: string, oldPassword: string) {
if (!newPassword) throw new Error('New password is missing');
if (!oldPassword) throw new Error('Old password is missing');
const { data, error } = await this.#currentUserSource.changePassword(newPassword, oldPassword);
if (!error) {
const notification = { data: { message: `Password changed` } };
this.notificationContext?.peek('positive', notification);
}
return { data, error };
}
}
export default UmbCurrentUserRepository;