vgquotes/Src/VideoGameQuotes.Web/Security/SessionBasedUserProvider.cs
tmont 9a1f0347c0 * tests for user provider
* username is no longer required
* added ip address property to user
2011-02-10 21:04:13 +00:00

46 lines
1.2 KiB
C#

using System.Web;
using Portoa.Web.Session;
using VideoGameQuotes.Api;
using VideoGameQuotes.Api.Persistence;
namespace VideoGameQuotes.Web.Security {
public class SessionBasedUserProvider : ICurrentUserProvider {
private readonly IUserService userService;
private readonly ISessionStore sessionStore;
private readonly HttpContextBase httpContext;
public SessionBasedUserProvider(IUserService userService, ISessionStore sessionStore, HttpContextBase httpContext) {
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)) {
return null;
}
user = userService.FindByIpAddress(ipAddress);
if (user == null) {
user = new User {
IpAddress = ipAddress,
Group = UserGroup.User
};
user = userService.Save(user);
}
sessionStore["user"] = user;
}
return user;
}
}
}
}