1+ using System ;
2+ using Microsoft . Extensions . Options ;
3+ using Shuttle . Core . Contract ;
4+ using Shuttle . Core . Data ;
5+
6+ namespace Shuttle . Access . Mvc . DataStore
7+ {
8+ public class DataStoreAccessService : CachedAccessService , IAccessService
9+ {
10+ private readonly AccessOptions _accessOptions ;
11+ private readonly IDatabaseContextFactory _databaseContextFactory ;
12+ private readonly ISessionRepository _sessionRepository ;
13+ private readonly string _connectionStringName ;
14+
15+ public DataStoreAccessService ( IOptionsMonitor < ConnectionStringOptions > connectionStringOptions , IOptions < AccessOptions > accessOptions , IDatabaseContextFactory databaseContextFactory , ISessionRepository sessionRepository ) {
16+ Guard . AgainstNull ( connectionStringOptions , nameof ( connectionStringOptions ) ) ;
17+ Guard . AgainstNull ( accessOptions , nameof ( accessOptions ) ) ;
18+ Guard . AgainstNull ( accessOptions . Value , nameof ( accessOptions . Value ) ) ;
19+ Guard . AgainstNull ( databaseContextFactory , nameof ( databaseContextFactory ) ) ;
20+ Guard . AgainstNull ( sessionRepository , nameof ( sessionRepository ) ) ;
21+
22+ _accessOptions = accessOptions . Value ;
23+ _connectionStringName = accessOptions . Value . ConnectionStringName ;
24+ _databaseContextFactory = databaseContextFactory ;
25+ _sessionRepository = sessionRepository ;
26+ }
27+
28+ public new bool Contains ( Guid token )
29+ {
30+ var result = base . Contains ( token ) ;
31+
32+ if ( ! result )
33+ {
34+ Cache ( token ) ;
35+
36+ return base . Contains ( token ) ;
37+ }
38+
39+ return true ;
40+ }
41+
42+ public new bool HasPermission ( Guid token , string permission )
43+ {
44+ if ( ! base . Contains ( token ) )
45+ {
46+ Cache ( token ) ;
47+ }
48+
49+ return base . HasPermission ( token , permission ) ;
50+ }
51+
52+ public new void Remove ( Guid token )
53+ {
54+ base . Remove ( token ) ;
55+ }
56+
57+ private void Cache ( Guid token )
58+ {
59+ if ( base . Contains ( token ) )
60+ {
61+ return ;
62+ }
63+
64+ using ( _databaseContextFactory . Create ( _connectionStringName ) )
65+ {
66+ var session = _sessionRepository . Find ( token ) ;
67+
68+ if ( session != null )
69+ {
70+ Cache ( token , session . Permissions , _accessOptions . SessionDuration ) ;
71+ }
72+ }
73+ }
74+ }
75+ }
0 commit comments