1
1
using EPiServer . DataAccess ;
2
+ using EPiServer . Events ;
3
+ using EPiServer . Events . Clients ;
2
4
using EPiServer . Framework . TypeScanner ;
3
5
using EPiServer . Globalization ;
4
6
using EPiServer . Logging ;
5
7
using EPiServer . Security ;
6
8
using System . Collections . Concurrent ;
9
+ using System . Security . Policy ;
7
10
8
11
namespace Foundation . Infrastructure . Cms . Settings
9
12
{
@@ -16,6 +19,7 @@ public interface ISettingsService
16
19
void UnintializeSettings ( ) ;
17
20
void UpdateSettings ( Guid siteId , IContent content , bool isContentNotPublished ) ;
18
21
void UpdateSettings ( ) ;
22
+ void SettingsEvent_Raised ( object sender , EventNotificationEventArgs e ) ;
19
23
}
20
24
21
25
public static class ISettingsServiceExtensions
@@ -55,6 +59,7 @@ public class SettingsService : ISettingsService
55
59
private readonly ISiteDefinitionResolver _siteDefinitionResolver ;
56
60
private readonly IHttpContextAccessor _httpContextAccessor ;
57
61
private readonly IContextModeResolver _contextModeResolver ;
62
+ private readonly IEventRegistry _eventRegistry ;
58
63
59
64
public SettingsService (
60
65
IContentRepository contentRepository ,
@@ -67,7 +72,8 @@ public SettingsService(
67
72
ISiteDefinitionRepository siteDefinitionRepository ,
68
73
ISiteDefinitionResolver siteDefinitionResolver ,
69
74
IHttpContextAccessor httpContextAccessor ,
70
- IContextModeResolver contextModeResolver )
75
+ IContextModeResolver contextModeResolver ,
76
+ IEventRegistry eventRegistry )
71
77
{
72
78
_contentRepository = contentRepository ;
73
79
_contentVersionRepository = contentVersionRepository ;
@@ -80,6 +86,7 @@ public SettingsService(
80
86
_siteDefinitionResolver = siteDefinitionResolver ;
81
87
_httpContextAccessor = httpContextAccessor ;
82
88
_contextModeResolver = contextModeResolver ;
89
+ _eventRegistry = eventRegistry ;
83
90
}
84
91
85
92
public ConcurrentDictionary < string , Dictionary < Type , object > > SiteSettings { get ; } = new ConcurrentDictionary < string , Dictionary < Type , object > > ( ) ;
@@ -140,6 +147,45 @@ public T GetSiteSettings<T>(Guid? siteId = null)
140
147
return default ;
141
148
}
142
149
150
+ public void SettingsEvent_Raised ( object sender , EventNotificationEventArgs e )
151
+ {
152
+ if ( e . RaiserId == Initialize . SettingsRaiserId )
153
+ {
154
+ return ;
155
+ }
156
+
157
+ var eventParameter = e . Param as SettingsEvent ;
158
+ if ( eventParameter == null )
159
+ {
160
+ return ;
161
+ }
162
+
163
+ if ( eventParameter . IsDelete )
164
+ {
165
+ if ( eventParameter . SiteId == Guid . Empty )
166
+ {
167
+ return ;
168
+ }
169
+
170
+ foreach ( var key in SiteSettings . Keys . Where ( x => x . Contains ( eventParameter . SiteId . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ) )
171
+ {
172
+ SiteSettings . TryRemove ( key , out var value ) ;
173
+ }
174
+ }
175
+
176
+ if ( ! ContentReference . TryParse ( eventParameter . ContentReference , out var reference ) )
177
+ {
178
+ return ;
179
+ }
180
+
181
+ if ( ! _contentRepository . TryGet < IContent > ( reference , out var content ) )
182
+ {
183
+ return ;
184
+ }
185
+
186
+ UpdateSettings ( eventParameter . SiteId , content , ! eventParameter . IsPublished ) ;
187
+ }
188
+
143
189
public void UpdateSettings ( Guid siteId , IContent content , bool isContentNotPublished )
144
190
{
145
191
var contentType = content . GetOriginalType ( ) ;
@@ -192,6 +238,12 @@ public void UpdateSettings(Guid siteId, IContent content, bool isContentNotPubli
192
238
SiteSettings [ siteId . ToString ( ) + $ "-{ contentLanguage } "] [ contentType ] = content ;
193
239
SiteSettings [ $ "{ siteId } -common-draft-{ contentLanguage } "] [ contentType ] = content ;
194
240
}
241
+ _eventRegistry . Get ( Initialize . SettingsEventId ) . Raise ( Initialize . SettingsRaiserId , new SettingsEvent
242
+ {
243
+ ContentReference = content . ContentLink . ToString ( ) ,
244
+ SiteId = siteId ,
245
+ IsPublished = ! isContentNotPublished
246
+ } ) ;
195
247
}
196
248
catch ( KeyNotFoundException keyNotFoundException )
197
249
{
@@ -241,6 +293,7 @@ public void UpdateSettings()
241
293
return ;
242
294
}
243
295
296
+ SiteSettings . Clear ( ) ;
244
297
GlobalSettingsRoot = root . ContentLink ;
245
298
var children = _contentRepository . GetChildren < SettingsFolder > ( GlobalSettingsRoot ) . ToList ( ) ;
246
299
foreach ( var site in _siteDefinitionRepository . List ( ) )
@@ -326,6 +379,17 @@ private void SiteDeleted(object sender, SiteDefinitionEventArgs e)
326
379
}
327
380
328
381
_contentRepository . Delete ( folder . ContentLink , true , AccessLevel . NoAccess ) ;
382
+ foreach ( var key in SiteSettings . Keys . Where ( x => x . Contains ( e . Site . Id . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ) )
383
+ {
384
+ SiteSettings . TryRemove ( key , out var value ) ;
385
+ }
386
+ _eventRegistry . Get ( Initialize . SettingsEventId ) . Raise ( Initialize . SettingsRaiserId , new SettingsEvent
387
+ {
388
+ ContentReference = null ,
389
+ SiteId = e . Site . Id ,
390
+ IsPublished = false ,
391
+ IsDelete = true
392
+ } ) ;
329
393
}
330
394
331
395
private void SiteUpdated ( object sender , SiteDefinitionEventArgs e )
0 commit comments