Ejemplo n.º 1
0
        protected User GetCurrentUser()
        {
            if (_cachedUser != null)
            {
                return(_cachedUser);
            }

            User user = null;

            if (_httpContext.IsReady())
            {
                if (_webHelper.IsSearchEngine(_httpContext.Request))
                {
                    user = _userService.GetUserBySystemName(UserNames.SearchEngine);
                }

                if (user == null || !user.IsPublished || user.IsDeleted)
                {
                    user = _authenticationService.GetAuthenticatedUser();
                }

                if (user == null || !user.IsPublished || user.IsDeleted)
                {
                    var userCookie = GetUserCookie();
                    if (userCookie != null && !string.IsNullOrEmpty(userCookie.Value))
                    {
                        Guid guid;
                        if (Guid.TryParse(userCookie.Value, out guid))
                        {
                            var userByCookie = _userService.GetUserByGuid(guid);
                            if (userByCookie != null && !userByCookie.IsRegistered() && !userByCookie.IsSearchEngineAccount())
                            {
                                user = userByCookie;
                            }
                        }
                    }
                }

                if (user == null || !user.IsPublished || user.IsDeleted)
                {
                    user = _userService.GetOrInsertGuestUser();
                }

                SetUserCookie(user.Guid);
            }

            if (user != null && user.IsPublished && !user.IsDeleted)
            {
                var update = false;
                if (user.LastActivityDateUtc.AddMinutes(1.0) < DateTime.UtcNow)
                {
                    user.LastActivityDateUtc = DateTime.UtcNow;
                    update = true;
                }

                var currentIpAddress = _webHelper.GetIpAddress();
                if (!string.IsNullOrEmpty(currentIpAddress))
                {
                    if (!currentIpAddress.Equals(user.LastIpAddress))
                    {
                        user.LastIpAddress = currentIpAddress;
                        update             = true;
                    }
                }

                if (update)
                {
                    _userService.UpdateUser(user);
                }

                _cachedUser = user;
            }

            return(_cachedUser);
        }