コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RockRequestContext"/> class.
        /// </summary>
        /// <param name="httpContextAccessor">The HTTP context accessor.</param>
        /// <param name="rockContextFactory">The rock context factory.</param>
        /// <param name="rockPage">The rock page.</param>
        /// <param name="currentUser">The current user.</param>
        /// <param name="currentPerson">The current person.</param>
        public RockRequestContext(IHttpContextAccessor httpContextAccessor, IRockContextFactory rockContextFactory, IRockPage rockPage, UserLogin currentUser, Person currentPerson)
        {
            HttpContext         = httpContextAccessor?.HttpContext;
            _rockContextFactory = rockContextFactory ?? new DefaultRockContextFactory();
            _rockContext        = GetRockContext();

            //
            // Set the current user either to the explicit user or the one found
            // in the HttpContext.
            //
            if (currentUser != null)
            {
                CurrentUser = currentUser;
            }
            else
            {
                _currentUser = new Lazy <UserLogin>(() =>
                {
                    UserLogin user  = null;
                    string username = HttpContext?.User?.Identity?.Name;

                    if (!string.IsNullOrEmpty(username) && !username.StartsWith("rckipid="))
                    {
                        user = new UserLoginService(_rockContext).GetByUserName(username);
                    }

                    return(user);
                });
            }

            //
            // Set the current person either to the explicit person or the one found
            // in the HttpContext.
            //
            if (currentPerson != null)
            {
                CurrentPerson = currentPerson;
            }
            else
            {
                _currentPerson = new Lazy <Person>(() =>
                {
                    string username = HttpContext?.User?.Identity?.Name;
                    string rckipid  = null;

                    if (!string.IsNullOrEmpty(username) && username.StartsWith("rckipid="))
                    {
                        rckipid = username.Substring(8);
                    }
                    else if (HttpContext?.Request?.Query?.ContainsKey("rckipid") ?? false)
                    {
                        rckipid = HttpContext.Request.Query["rckipid"];
                    }

                    if (rckipid != null)
                    {
                        var impersonatedPerson = new PersonService(_rockContext).GetByImpersonationToken(rckipid, false, rockPage?.PageId);

                        if (impersonatedPerson != null)
                        {
                            return(impersonatedPerson);
                        }
                    }

                    return(CurrentUser?.Person);
                });
            }
        }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RockRequestContext"/> class.
 /// </summary>
 /// <param name="httpContextAccessor">The HTTP context accessor.</param>
 /// <param name="rockContextFactory">The rock context factory.</param>
 public RockRequestContext(IHttpContextAccessor httpContextAccessor, IRockContextFactory rockContextFactory)
     : this(httpContextAccessor, rockContextFactory, null, null, null)
 {
 }