vgquotes/Src/VideoGameQuotes.Web/Security/SessionBasedUserProvider.cs

46 lines
1.2 KiB
C#
Raw Normal View History

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 {
public class SessionBasedUserProvider : ICurrentUserProvider {
2011-02-10 04:39:59 +00:00
private readonly IUserService userService;
private readonly ISessionStore sessionStore;
private readonly HttpContextBase httpContext;
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) {
//identify user by IP address
var ipAddress = httpContext.Request.UserHostAddress;
if (string.IsNullOrEmpty(ipAddress)) {
2011-02-10 04:39:59 +00:00
return null;
}
user = userService.FindByIpAddress(ipAddress);
2011-02-10 04:39:59 +00:00
if (user == null) {
user = new User {
IpAddress = ipAddress,
2011-02-10 04:39:59 +00:00
Group = UserGroup.User
};
user = userService.Save(user);
}
sessionStore["user"] = user;
}
return user;
}
}
}
}