/// <summary> /// Constructor; loads the currently logged-in user, if needed. /// </summary> public ApiControllerBase(IPrincipal user, Data.IUserRepository userRepository, IGlobalizationManager globalizationManager, Domain.User preLoadedUser = null) { UserRepository = userRepository; GlobalizationManager = globalizationManager; // load logged-in user, if needed if (preLoadedUser != null && preLoadedUser.ID > 0) { User = preLoadedUser; } else { User = GetAuthenticatedUser(user, UserRepository); } }
/// <summary> /// Returns the currently logged-in user corresponding to the given IPrincipal; /// returns null if user is not found or is not authenticated. /// </summary> public static Domain.User GetAuthenticatedUser(IPrincipal user, Data.IUserRepository userRepository) { Domain.User u = null; if (user != null && user.Identity != null && user.Identity.IsAuthenticated) { int userID; var username = user.Identity.Name.Split('-'); if (username.Length > 1) { u = Domain.User.GetImpersonateUser(user.Identity.Name, userRepository); u.AdminID = Int32.Parse(username[1]); } else if (int.TryParse(username[0], out userID)) { u = Domain.User.Get(userID, userRepository); } } return(u); }