forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceStackProvider.cs
More file actions
264 lines (227 loc) · 8.22 KB
/
Copy pathServiceStackProvider.cs
File metadata and controls
264 lines (227 loc) · 8.22 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Configuration;
using ServiceStack.Data;
using ServiceStack.Messaging;
using ServiceStack.Redis;
using ServiceStack.Web;
namespace ServiceStack
{
//implemented by PageBase and
public interface IHasServiceStackProvider
{
IServiceStackProvider ServiceStackProvider { get; }
}
public interface IServiceStackProvider : IDisposable
{
void SetResolver(IResolver resolver);
IResolver GetResolver();
IAppSettings AppSettings { get; }
IHttpRequest Request { get; }
IHttpResponse Response { get; }
ICacheClient Cache { get; }
IDbConnection Db { get; }
IRedisClient Redis { get; }
IMessageFactory MessageFactory { get; set; }
IMessageProducer MessageProducer { get; }
ISessionFactory SessionFactory { get; }
ISession SessionBag { get; }
bool IsAuthenticated { get; }
T TryResolve<T>();
T ResolveService<T>();
object Execute(object requestDto);
object Execute(IRequest request);
IAuthSession GetSession(bool reload = false);
TUserSession SessionAs<TUserSession>();
void ClearSession();
void PublishMessage<T>(T message);
}
//Add extra functionality common to ASP.NET ServiceStackPage or ServiceStackController
public static class ServiceStackProviderExtensions
{
public static bool IsAuthorized(this IHasServiceStackProvider hasProvider, AuthenticateAttribute authAttr)
{
if (authAttr == null)
return true;
var authSession = hasProvider.ServiceStackProvider.GetSession();
return authSession != null && authSession.IsAuthenticated;
}
public static bool HasAccess(
this IHasServiceStackProvider hasProvider,
ICollection<RequiredRoleAttribute> roleAttrs,
ICollection<RequiresAnyRoleAttribute> anyRoleAttrs,
ICollection<RequiredPermissionAttribute> permAttrs,
ICollection<RequiresAnyPermissionAttribute> anyPermAttrs)
{
if (roleAttrs.Count + anyRoleAttrs.Count + permAttrs.Count + anyPermAttrs.Count == 0)
return true;
var authSession = hasProvider.ServiceStackProvider.GetSession();
if (authSession == null || !authSession.IsAuthenticated)
return false;
var httpReq = hasProvider.ServiceStackProvider.Request;
var userAuthRepo = httpReq.TryResolve<IAuthRepository>();
var hasRoles = roleAttrs.All(x => x.HasAllRoles(httpReq, authSession, userAuthRepo));
if (!hasRoles)
return false;
var hasAnyRole = anyRoleAttrs.All(x => x.HasAnyRoles(httpReq, authSession, userAuthRepo));
if (!hasAnyRole)
return false;
var hasPermssions = permAttrs.All(x => x.HasAllPermissions(httpReq, authSession, userAuthRepo));
if (!hasPermssions)
return false;
var hasAnyPermission = anyPermAttrs.All(x => x.HasAnyPermissions(httpReq, authSession, userAuthRepo));
if (!hasAnyPermission)
return false;
return true;
}
}
public class ServiceStackProvider : IServiceStackProvider
{
public ServiceStackProvider(IHttpRequest request, IResolver resolver = null)
{
this.request = request;
this.resolver = resolver ?? Service.GlobalResolver ?? HostContext.AppHost;
}
private IResolver resolver;
public virtual void SetResolver(IResolver resolver)
{
this.resolver = resolver;
}
public virtual IResolver GetResolver()
{
return resolver;
}
public IAppSettings AppSettings
{
get { return HostContext.AppSettings; }
}
private readonly IHttpRequest request;
public virtual IHttpRequest Request
{
get { return request; }
}
public virtual IHttpResponse Response
{
get { return (IHttpResponse)Request.Response; }
}
public virtual T TryResolve<T>()
{
return this.GetResolver() == null
? default(T)
: this.GetResolver().TryResolve<T>();
}
public virtual T ResolveService<T>()
{
var service = TryResolve<T>();
var requiresContext = service as IRequiresRequest;
if (requiresContext != null)
{
requiresContext.Request = Request;
}
return service;
}
public object Execute(object requestDto)
{
return HostContext.ServiceController.Execute(requestDto, Request);
}
public object Execute(IRequest request)
{
return HostContext.ServiceController.Execute(Request);
}
public object ForwardRequest()
{
return HostContext.ServiceController.Execute(Request);
}
private ICacheClient cache;
public virtual ICacheClient Cache
{
get
{
return cache ?? (cache = HostContext.AppHost.GetCacheClient());
}
}
private IDbConnection db;
public virtual IDbConnection Db
{
get { return db ?? (db = TryResolve<IDbConnectionFactory>().OpenDbConnection()); }
}
private IRedisClient redis;
public virtual IRedisClient Redis
{
get { return redis ?? (redis = TryResolve<IRedisClientsManager>().GetClient()); }
}
private IMessageFactory messageFactory;
public virtual IMessageFactory MessageFactory
{
get { return messageFactory ?? (messageFactory = TryResolve<IMessageFactory>()); }
set { messageFactory = value; }
}
private IMessageProducer messageProducer;
public virtual IMessageProducer MessageProducer
{
get { return messageProducer ?? (messageProducer = MessageFactory.CreateMessageProducer()); }
}
private ISessionFactory sessionFactory;
public virtual ISessionFactory SessionFactory
{
get { return sessionFactory ?? (sessionFactory = TryResolve<ISessionFactory>()) ?? new SessionFactory(Cache); }
}
/// <summary>
/// Typed UserSession
/// </summary>
public virtual TUserSession SessionAs<TUserSession>()
{
var ret = TryResolve<TUserSession>();
return !Equals(ret, default(TUserSession))
? ret
: SessionFeature.GetOrCreateSession<TUserSession>(Cache, Request, Response);
}
public virtual void ClearSession()
{
Cache.ClearSession();
}
/// <summary>
/// Dynamic Session Bag
/// </summary>
private ISession session;
public virtual ISession SessionBag
{
get
{
return session ?? (session = TryResolve<ISession>() //Easier to mock
?? SessionFactory.GetOrCreateSession(Request, Response));
}
}
public virtual IAuthSession GetSession(bool reload = false)
{
var req = this.Request;
if (req.GetSessionId() == null)
req.Response.CreateSessionIds(req);
return req.GetSession(reload);
}
public virtual bool IsAuthenticated
{
get { return this.GetSession().IsAuthenticated; }
}
public virtual void PublishMessage<T>(T message)
{
if (MessageProducer == null)
throw new NullReferenceException("No IMessageFactory was registered, cannot PublishMessage");
MessageProducer.Publish(message);
}
public virtual void Dispose()
{
if (db != null)
db.Dispose();
if (redis != null)
redis.Dispose();
if (messageProducer != null)
messageProducer.Dispose();
}
}
}