Example #1
0
        public UserResult GetUser(string UserId, bool fill = false)
        {
            if (string.IsNullOrWhiteSpace(UserId))
            {
                return(UserResult.AsFailed());
            }
            var idx = UserId.IndexOf(@"\");

            if (idx > -1)
            {
                UserId = UserId.Substring(idx + 1);
            }
            return(userRepository.GetUser(UserId, fill));
        }
        };  // TODO: Create Service Locator from DI container, then move services instantiation to Factory (or Builder)

        protected virtual bool InitRequestState(string userName = null)
        {
            log.Debug("Begin BaseController.InitRequestState");
            try
            {
                userName = userName ?? User.Identity.Name;

                var userResult = User != null && !string.IsNullOrWhiteSpace(userName)
                    ? ServicesProxy.UserService.GetUser(userName)
                    : UserResult.AsFailed();

                if (User == null)
                {
                    log.Debug(string.Format("User is null"));
                }
                else if (User.Identity == null)
                {
                    log.Debug(string.Format("User.Identity is null"));
                }
                else
                {
                    log.Debug(string.Format("{0} UserService.GetUser({1}): {2}", userResult.OK ? "Call" : "Fail", userName, userResult));
                }

                ServicesProxy.RequestState = new RequestState
                {
                    Principal      = User,
                    Identity       = User != null ? User.Identity : null,
                    UserDetails    = userResult.OK ? userResult.User : null,
                    MaxSearchCount = Config.MaxSearchCount,
                };

                ViewBag.RequestState = ServicesProxy.RequestState;
                if (HttpContext.Items.Contains("RequestState"))
                {
                    HttpContext.Items["RequestState"] = ServicesProxy.RequestState;
                }
                else
                {
                    HttpContext.Items.Add("RequestState", ServicesProxy.RequestState);
                }

                return(ServicesProxy.RequestState.UserDetails != null);
            }
            finally
            {
                log.Debug("End BaseController.InitRequestState");
            }
        }