Exemplo n.º 1
0
        /// <summary>
        /// Returns the <see cref="Rock.Model.UserLogin"/> of the user who is currently logged in, and updates their last activity date if userIsOnline=true
        /// </summary>
        /// <param name="userIsOnline">A <see cref="System.Boolean"/> value that returns the logged in user if <c>true</c>; otherwise can return the impersonated user</param>
        /// <returns>The current <see cref="Rock.Model.UserLogin"/></returns>
        public static UserLogin GetCurrentUser(bool userIsOnline)
        {
            var rockContext = new RockContext();

            string userName = UserLogin.GetCurrentUserName();

            if (userName != string.Empty)
            {
                if (userName.StartsWith("rckipid="))
                {
                    Rock.Model.PersonTokenService personTokenService = new Model.PersonTokenService(rockContext);
                    Rock.Model.PersonToken        personToken        = personTokenService.GetByImpersonationToken(userName.Substring(8));
                    if (personToken?.PersonAlias?.Person != null)
                    {
                        return(personToken.PersonAlias.Person.GetImpersonatedUser());
                    }
                }
                else
                {
                    var       userLoginService = new UserLoginService(rockContext);
                    UserLogin user             = userLoginService.GetByUserName(userName);

                    if (user != null && userIsOnline)
                    {
                        // Save last activity date
                        var message = new UpdateUserLastActivity.Message
                        {
                            UserId           = user.Id,
                            LastActivityDate = RockDateTime.Now,
                        };

                        if ((user.IsConfirmed ?? true) && !(user.IsLockedOut ?? false))
                        {
                            if (HttpContext.Current != null && HttpContext.Current.Session != null)
                            {
                                HttpContext.Current.Session["RockUserId"] = user.Id;
                            }

                            message.Send();
                        }
                        else
                        {
                            message.IsOnline = false;
                            message.Send();

                            Authorization.SignOut();
                            return(null);
                        }
                    }

                    return(user);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the <see cref="Rock.Model.UserLogin"/> of the user who is currently logged in, and updates their last activity date if userIsOnline=true
        /// </summary>
        /// <param name="userIsOnline">A <see cref="System.Boolean"/> value that returns the logged in user if <c>true</c>; otherwise can return the impersonated user</param>
        /// <returns>The current <see cref="Rock.Model.UserLogin"/></returns>
        public static UserLogin GetCurrentUser(bool userIsOnline)
        {
            var rockContext = new RockContext();

            string userName = UserLogin.GetCurrentUserName();

            if (userName != string.Empty)
            {
                if (userName.StartsWith("rckipid="))
                {
                    Rock.Model.PersonTokenService personTokenService = new Model.PersonTokenService(rockContext);
                    Rock.Model.PersonToken        personToken        = personTokenService.GetByImpersonationToken(userName.Substring(8));
                    if (personToken?.PersonAlias?.Person != null)
                    {
                        return(personToken.PersonAlias.Person.GetImpersonatedUser());
                    }
                }
                else
                {
                    var       userLoginService = new UserLoginService(rockContext);
                    UserLogin user             = userLoginService.GetByUserName(userName);

                    if (user != null && userIsOnline)
                    {
                        // Save last activity date
                        var transaction = new Rock.Transactions.UserLastActivityTransaction();
                        transaction.UserId           = user.Id;
                        transaction.LastActivityDate = RockDateTime.Now;

                        if ((user.IsConfirmed ?? true) && !(user.IsLockedOut ?? false))
                        {
                            if (HttpContext.Current != null && HttpContext.Current.Session != null)
                            {
                                HttpContext.Current.Session["RockUserId"] = user.Id;
                            }

                            // see if there is already a LastActivitytransaction queued for this user, and just update its LastActivityDate instead of adding another to the queue
                            var userLastActivity = Rock.Transactions.RockQueue.TransactionQueue.ToArray().OfType <Rock.Transactions.UserLastActivityTransaction>()
                                                   .Where(a => a.UserId == transaction.UserId).FirstOrDefault();

                            if (userLastActivity != null)
                            {
                                userLastActivity.LastActivityDate = transaction.LastActivityDate;
                            }
                            else
                            {
                                Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                            }
                        }
                        else
                        {
                            transaction.IsOnLine = false;
                            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);

                            Authorization.SignOut();
                            return(null);
                        }
                    }

                    return(user);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// NOTE: This does much more then is sounds like! It returns the <see cref="Rock.Model.UserLogin"/> of the user who is currently logged in,
        /// but it also updates their last activity date, and will sign them out if they are not confirmed or are locked out.
        /// </summary>
        /// <param name="userIsOnline">A <see cref="System.Boolean"/> value that returns the logged in user if <c>true</c>; otherwise can return the impersonated user</param>
        /// <returns>The current <see cref="Rock.Model.UserLogin"/></returns>
        public static UserLogin GetCurrentUser(bool userIsOnline)
        {
            var rockContext = new RockContext();

            string userName = UserLogin.GetCurrentUserName();

            if (userName.IsNullOrWhiteSpace())
            {
                return(null);
            }

            if (userName.StartsWith("rckipid="))
            {
                Rock.Model.PersonTokenService personTokenService = new Model.PersonTokenService(rockContext);
                Rock.Model.PersonToken        personToken        = personTokenService.GetByImpersonationToken(userName.Substring(8));
                if (personToken?.PersonAlias?.Person != null)
                {
                    return(personToken.PersonAlias.Person.GetImpersonatedUser());
                }
            }
            else
            {
                var       userLoginService = new UserLoginService(rockContext);
                UserLogin user             = userLoginService.GetByUserName(userName);

                if (user != null && userIsOnline)
                {
                    // Save last activity date
                    var message = new UpdateUserLastActivity.Message
                    {
                        UserId           = user.Id,
                        LastActivityDate = RockDateTime.Now,
                    };

                    if ((user.IsConfirmed ?? true) && !(user.IsLockedOut ?? false))
                    {
                        if (HttpContext.Current != null && HttpContext.Current.Session != null)
                        {
                            HttpContext.Current.Session["RockUserId"] = user.Id;
                        }

                        message.SendIfNeeded();
                    }
                    else
                    {
                        // Even though we are in the userIsOnline == true condition,
                        // The user is either not confirmed or is locked out, so we'll mark them
                        // as offline and sign them out.

                        message.IsOnline = false;
                        message.SendIfNeeded();

                        Authorization.SignOut();
                        return(null);
                    }
                }

                return(user);
            }

            return(null);
        }