1
1
import { UmbDocumentPublicAccessRepository } from '../repository/public-access.repository.js' ;
2
- import { UmbDocumentDetailRepository } from '../../../repository/index.js' ;
2
+ import { UmbDocumentItemRepository } from '../../../repository/index.js' ;
3
3
import type { UmbInputDocumentElement } from '../../../components/index.js' ;
4
4
import type { UmbPublicAccessModalData , UmbPublicAccessModalValue } from './public-access-modal.token.js' ;
5
5
import { css , customElement , html , nothing , state } from '@umbraco-cms/backoffice/external/lit' ;
6
6
import { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal' ;
7
7
import { UmbTextStyles } from '@umbraco-cms/backoffice/style' ;
8
- import type { UmbInputMemberElement } from '@umbraco-cms/backoffice/member' ;
9
- import type { UmbInputMemberGroupElement } from '@umbraco-cms/backoffice/member-group' ;
8
+ import { UmbMemberDetailRepository , type UmbInputMemberElement } from '@umbraco-cms/backoffice/member' ;
9
+ import { UmbMemberGroupItemRepository , type UmbInputMemberGroupElement } from '@umbraco-cms/backoffice/member-group' ;
10
10
import type { PublicAccessRequestModel } from '@umbraco-cms/backoffice/external/backend-api' ;
11
11
import type { UUIRadioEvent } from '@umbraco-cms/backoffice/external/uui' ;
12
12
@@ -32,56 +32,52 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
32
32
private _selection : Array < string > = [ ] ;
33
33
34
34
@state ( )
35
- private _loginPageId ?: string ;
35
+ private _loginDocumentId ?: string ;
36
36
37
37
@state ( )
38
- private _errorPageId ?: string ;
38
+ private _errorDocumentId ?: string ;
39
39
40
40
// Init
41
41
42
42
firstUpdated ( ) {
43
43
this . #unique = this . data ?. unique ;
44
44
this . #getDocumentName( ) ;
45
- this . #getPublicAccessModel( ) ;
46
45
}
47
46
48
47
async #getDocumentName( ) {
49
48
if ( ! this . #unique) return ;
50
49
// Should this be done here or in the action file?
51
- const { data } = await new UmbDocumentDetailRepository ( this ) . requestByUnique ( this . #unique) ;
50
+ const { data } = await new UmbDocumentItemRepository ( this ) . requestItems ( [ this . #unique] ) ;
52
51
if ( ! data ) return ;
52
+ const item = data [ 0 ] ;
53
53
//TODO How do we ensure we get the correct variant?
54
- this . _documentName = data . variants [ 0 ] ?. name ;
54
+ this . _documentName = item . variants [ 0 ] ?. name ;
55
+
56
+ if ( item . isProtected ) {
57
+ this . #getPublicAccessModel( ) ;
58
+ }
55
59
}
56
60
57
61
async #getPublicAccessModel( ) {
58
62
if ( ! this . #unique) return ;
59
- //const { data } = (await this.#publicAccessRepository.read(this.#unique));
60
- // TODO Currently returning "void". Remove mock data when API is ready. Will it be Response or Request model?
61
- const data : any = undefined ;
62
- /*const data: PublicAccessResponseModel = {
63
- members: [{ name: 'Agent', id: '007' }],
64
- groups: [],
65
- loginPageId: '123',
66
- errorPageId: '456',
67
- };*/
63
+ const { data } = await this . #publicAccessRepository. read ( this . #unique) ;
68
64
69
65
if ( ! data ) return ;
70
66
this . #isNew = false ;
71
67
this . _startPage = false ;
72
68
73
69
// Specific or Groups
74
- this . _specific = data . members . length > 0 ? true : false ;
70
+ this . _specific = data . members . length > 0 ;
75
71
76
72
//selection
77
73
if ( data . members . length > 0 ) {
78
- this . _selection = data . members . map ( ( m : any ) => m . id ) ;
74
+ this . _selection = data . members . map ( ( m ) => m . id ) ;
79
75
} else if ( data . groups . length > 0 ) {
80
- this . _selection = data . groups . map ( ( g : any ) => g . id ) ;
76
+ this . _selection = data . groups . map ( ( g ) => g . id ) ;
81
77
}
82
78
83
- this . _loginPageId = data . loginPageId ;
84
- this . _errorPageId = data . errorPageId ;
79
+ this . _loginDocumentId = data . loginDocument . id ;
80
+ this . _errorDocumentId = data . errorDocument . id ;
85
81
}
86
82
87
83
// Modal events
@@ -91,30 +87,54 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
91
87
}
92
88
93
89
async #handleSave( ) {
94
- if ( ! this . _loginPageId || ! this . _errorPageId || ! this . #unique) return ;
95
-
96
- const groups = this . _specific ? [ ] : this . _selection ;
97
- const members = this . _specific ? this . _selection : [ ] ;
90
+ if ( ! this . _loginDocumentId || ! this . _errorDocumentId || ! this . #unique) return ;
98
91
92
+ // TODO: [v15] Currently the Management API doesn't support passing the member/group ids, only the userNames/names.
93
+ // This is a temporary solution where we have to look them up until the API is updated to support this.
99
94
const requestBody : PublicAccessRequestModel = {
100
- memberGroupNames : groups ,
101
- memberUserNames : members ,
102
- loginDocument : { id : this . _loginPageId } ,
103
- errorDocument : { id : this . _errorPageId } ,
95
+ memberGroupNames : [ ] ,
96
+ memberUserNames : [ ] ,
97
+ loginDocument : { id : this . _loginDocumentId } ,
98
+ errorDocument : { id : this . _errorDocumentId } ,
104
99
} ;
105
100
101
+ if ( this . _specific ) {
102
+ // Members
103
+ // user name is not part of the item model, so we need to look it up from the member detail repository
104
+ // be aware that the detail repository requires access to the member section.
105
+ const repo = new UmbMemberDetailRepository ( this ) ;
106
+ const promises = this . _selection . map ( ( memberId ) => repo . requestByUnique ( memberId ) ) ;
107
+ const responses = await Promise . all ( promises ) ;
108
+ const memberUserNames = responses
109
+ . filter ( ( response ) => response . data )
110
+ . map ( ( response ) => response . data ?. username ) as string [ ] ;
111
+
112
+ requestBody . memberUserNames = memberUserNames ;
113
+ } else {
114
+ // Groups
115
+ const repo = new UmbMemberGroupItemRepository ( this ) ;
116
+ const { data } = await repo . requestItems ( this . _selection ) ;
117
+ if ( ! data ) throw new Error ( 'No Member groups returned' ) ;
118
+
119
+ const groupNames = data
120
+ . filter ( ( groupItem ) => this . _selection . includes ( groupItem . unique ) )
121
+ . map ( ( groupItem ) => groupItem . name ) ;
122
+
123
+ requestBody . memberGroupNames = groupNames ;
124
+ }
125
+
106
126
if ( this . #isNew) {
107
- this . #publicAccessRepository. create ( this . #unique, requestBody ) ;
127
+ await this . #publicAccessRepository. create ( this . #unique, requestBody ) ;
108
128
} else {
109
- this . #publicAccessRepository. update ( this . #unique, requestBody ) ;
129
+ await this . #publicAccessRepository. update ( this . #unique, requestBody ) ;
110
130
}
111
131
112
132
this . modalContext ?. submit ( ) ;
113
133
}
114
134
115
- #handleDelete( ) {
135
+ async #handleDelete( ) {
116
136
if ( ! this . #unique) return ;
117
- this . #publicAccessRepository. delete ( this . #unique) ;
137
+ await this . #publicAccessRepository. delete ( this . #unique) ;
118
138
this . modalContext ?. submit ( ) ;
119
139
}
120
140
@@ -125,11 +145,11 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
125
145
// Change Events
126
146
127
147
#onChangeLoginPage( e : CustomEvent ) {
128
- this . _loginPageId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
148
+ this . _loginDocumentId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
129
149
}
130
150
131
151
#onChangeErrorPage( e : CustomEvent ) {
132
- this . _errorPageId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
152
+ this . _errorDocumentId = ( e . target as UmbInputDocumentElement ) . selection [ 0 ] ;
133
153
}
134
154
135
155
#onChangeGroup( e : CustomEvent ) {
@@ -182,7 +202,10 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
182
202
<small>
183
203
<umb-localize key="publicAccess_paLoginPageHelp"> Choose the page that contains the login form </umb-localize>
184
204
</small>
185
- <umb-input-document max="1" @change=${ this . #onChangeLoginPage} ></umb-input-document>
205
+ <umb-input-document
206
+ .value=${ this . _loginDocumentId ? this . _loginDocumentId : '' }
207
+ max="1"
208
+ @change=${ this . #onChangeLoginPage} ></umb-input-document>
186
209
</div>
187
210
<br />
188
211
<div class="select-item">
@@ -192,7 +215,10 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
192
215
Used when people are logged on, but do not have access
193
216
</umb-localize>
194
217
</small>
195
- <umb-input-document max="1" @change=${ this . #onChangeErrorPage} ></umb-input-document>
218
+ <umb-input-document
219
+ .value=${ this . _errorDocumentId ? this . _errorDocumentId : '' }
220
+ max="1"
221
+ @change=${ this . #onChangeErrorPage} ></umb-input-document>
196
222
</div>` ;
197
223
}
198
224
@@ -220,7 +246,7 @@ export class UmbPublicAccessModalElement extends UmbModalBaseElement<
220
246
look="primary"
221
247
color="positive"
222
248
label=${ this . localize . term ( 'buttons_save' ) }
223
- ?disabled=${ ! this . _loginPageId || ! this . _errorPageId || this . _selection . length === 0 }
249
+ ?disabled=${ ! this . _loginDocumentId || ! this . _errorDocumentId || this . _selection . length === 0 }
224
250
@click="${ this . #handleSave} "></uui-button>`
225
251
: html `<uui-button
226
252
slot="actions"
0 commit comments