2011-02-10 04:39:59 +00:00
|
|
|
|
using System.Web;
|
|
|
|
|
using Portoa.Web.Session;
|
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Api.Persistence;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Security {
|
2011-02-10 21:04:13 +00:00
|
|
|
|
public class SessionBasedUserProvider : ICurrentUserProvider {
|
2011-02-10 04:39:59 +00:00
|
|
|
|
private readonly IUserService userService;
|
|
|
|
|
private readonly ISessionStore sessionStore;
|
|
|
|
|
private readonly HttpContextBase httpContext;
|
|
|
|
|
|
2011-02-10 21:04:13 +00:00
|
|
|
|
public SessionBasedUserProvider(IUserService userService, ISessionStore sessionStore, HttpContextBase httpContext) {
|
2011-02-10 04:39:59 +00:00
|
|
|
|
this.userService = userService;
|
|
|
|
|
this.sessionStore = sessionStore;
|
|
|
|
|
this.httpContext = httpContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User CurrentUser {
|
|
|
|
|
get {
|
|
|
|
|
var user = sessionStore["user"] as User;
|
|
|
|
|
|
|
|
|
|
if (user == null) {
|
2011-02-18 01:46:02 +00:00
|
|
|
|
//if we're logged in, then use the authenticated user (this inconsistency between cookie/session occurs when the app restarts)
|
|
|
|
|
if (httpContext.Request.IsAuthenticated) {
|
|
|
|
|
user = userService.FindByUsername(httpContext.User.Identity.Name);
|
|
|
|
|
} else {
|
|
|
|
|
//identify user by IP address
|
|
|
|
|
var ipAddress = httpContext.Request.UserHostAddress;
|
|
|
|
|
if (string.IsNullOrEmpty(ipAddress)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user = userService.FindByIpAddress(ipAddress);
|
|
|
|
|
if (user == null) {
|
|
|
|
|
user = new User {
|
|
|
|
|
IpAddress = ipAddress,
|
|
|
|
|
Group = UserGroup.User
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user = userService.Save(user);
|
|
|
|
|
}
|
2011-02-10 04:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionStore["user"] = user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|