vgquotes/Src/VideoGameQuotes.Web/Security/UserProvider.cs
2011-02-10 04:39:59 +00:00

46 lines
1.1 KiB
C#

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