Пример #1
0
        /// <summary>
        /// Try to link the device to a person and return the primary alias ID if successful
        /// </summary>
        /// <returns>true if device successfully linked to a person</returns>
        protected int?LinkDeviceToPerson()
        {
            // At this point the user is not logged in and not found by looking up the device
            // So lets try to find the user using entered info and then link them to the device.
            PersonService personService     = new PersonService(new RockContext());
            int           mobilePhoneTypeId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE).Id;
            Person        person            = null;
            string        mobilePhoneNumber = string.Empty;

            // Looking for a match using the first name, last name, and mobile number or email address information
            if (tbFirstName.Visible && tbLastName.Visible && (tbMobilePhone.Visible || tbEmail.Visible))
            {
                mobilePhoneNumber = tbMobilePhone.Text.RemoveAllNonAlphaNumericCharacters();

                var personQuery = new PersonService.PersonMatchQuery(tbFirstName.Text, tbLastName.Text, tbEmail.Text, mobilePhoneNumber);
                person = personService.FindPerson(personQuery, true);

                if (person.IsNotNull())
                {
                    RockPage.LinkPersonAliasToDevice(person.PrimaryAlias.Id, hfMacAddress.Value);
                    return(person.PrimaryAliasId);
                }
                else
                {
                    // If no known person record then create one since we have the minimum info required
                    person = CreateAndSaveNewPerson();

                    // Link new device to person alias
                    RockPage.LinkPersonAliasToDevice(person.PrimaryAlias.Id, hfMacAddress.Value);
                    return(person.PrimaryAlias.Id);
                }
            }

            // Just match off phone number if no other fields are showing.
            if (tbMobilePhone.Visible && !tbFirstName.Visible && !tbLastName.Visible && !tbEmail.Visible)
            {
                mobilePhoneNumber = tbMobilePhone.Text.RemoveAllNonAlphaNumericCharacters();
                person            = personService.Queryable().Where(p => p.PhoneNumbers.Where(n => n.NumberTypeValueId == mobilePhoneTypeId).FirstOrDefault().Number == mobilePhoneNumber).FirstOrDefault();
                if (person != null)
                {
                    RockPage.LinkPersonAliasToDevice(person.PrimaryAlias.Id, hfMacAddress.Value);
                    return(person.PrimaryAliasId);
                }
            }

            // Unable to find an existing user and we don't have the minimium info to create one.
            // We'll let Rock.Page and the cookie created in OnLoad() link the device to a user when they are logged in.
            // This will not work if this page is loaded into a Captive Network Assistant page as the cookie will not persist.
            return(null);
        }
        private void RedirectToUrl(string url)
        {
            var    mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, CurrentPerson);
            string resolvedUrl = url.ResolveMergeFields(mergeFields);

            // create or get device
            if (hfMacAddress.Value.IsNotNullOrWhiteSpace())
            {
                string macAddress = hfMacAddress.Value;
                PersonalDeviceService personalDeviceService = new PersonalDeviceService(new RockContext());
                PersonalDevice        personalDevice        = null;

                bool isAnExistingDevice = DoesPersonalDeviceExist(macAddress);
                if (isAnExistingDevice)
                {
                    personalDevice = VerifyDeviceInfo(macAddress);
                }
                else
                {
                    personalDevice = CreateDevice(macAddress);
                    CreateDeviceCookie(macAddress);
                }

                // Time to link this device to the person.
                if (personalDevice.PersonAliasId != CurrentPersonAliasId && CurrentPersonAliasId.HasValue)
                {
                    RockPage.LinkPersonAliasToDevice(CurrentPersonAliasId.Value, macAddress);
                }

                // Create/Modify the user in Front Porch
                if (CurrentPersonAliasId.HasValue)
                {
                    ModifyFrontPorchUser(CurrentPersonAliasId.Value);
                }
            }

            if (IsUserAuthorized(Rock.Security.Authorization.ADMINISTRATE))
            {
                nbAlert.Text = string.Format("If you did not have Administrate permissions on this block, you would have been redirected to here: <a href='{0}'>{0}</a>.", Page.ResolveUrl(resolvedUrl));
            }
            else
            {
                Response.Redirect(resolvedUrl, false);
                Context.ApplicationInstance.CompleteRequest();
                return;
            }
        }
Пример #3
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            nbAlert.Visible = false;

            // Go through the UA ignore list and don't load anything we don't care about or want.
            foreach (string userAgentToIgnore in _userAgentsToIgnore)
            {
                if (Request.UserAgent == null || Request.UserAgent.StartsWith(userAgentToIgnore))
                {
                    return;
                }
            }

            if (!IsPostBack)
            {
                string macAddress = RockPage.PageParameter(GetAttributeValue("MacAddressParam"));
                if (string.IsNullOrWhiteSpace(macAddress) || !macAddress.IsValidMacAddress())
                {
                    nbAlert.Text    = "Missing or invalid MAC Address";
                    nbAlert.Visible = true;
                    ShowControls(false);
                    return;
                }

                string releaseLink = GetAttributeValue("ReleaseLink");
                if (string.IsNullOrWhiteSpace(releaseLink) || !releaseLink.IsValidUrl())
                {
                    nbAlert.Text    = "Missing or invalid Release Link";
                    nbAlert.Visible = true;
                    ShowControls(false);
                    return;
                }

                // Save the supplied MAC address to the page removing any non-Alphanumeric characters
                macAddress         = macAddress.RemoveAllNonAlphaNumericCharacters();
                hfMacAddress.Value = macAddress;

                // create or get device
                PersonalDeviceService personalDeviceService = new PersonalDeviceService(new RockContext());
                PersonalDevice        personalDevice        = null;

                if (DoesPersonalDeviceExist(macAddress))
                {
                    personalDevice = VerifyDeviceInfo(macAddress);
                }
                else
                {
                    personalDevice = CreateDevice(macAddress);
                }

                // We are going to create this everytime they hit the captive portal page. Otherwise if the device is saved but not linked to an actual user (not the fake one created here),
                // and then deleted by the user/browser/software, then they'll never get the cookie again and won't automatically get linked by RockPage.
                CreateDeviceCookie(macAddress);

                // See if user is logged in and link the alias to the device.
                if (CurrentPerson != null)
                {
                    Prefill(CurrentPerson);
                    RockPage.LinkPersonAliasToDevice(( int )CurrentPersonAliasId, macAddress);
                }

                // Direct connect if no controls are visible
                if (!ShowControls())
                {
                    // Nothing to show means nothing to enter. Redirect user back to FP with the primary alias ID and query string
                    if (IsUserAuthorized(Rock.Security.Authorization.ADMINISTRATE))
                    {
                        nbAlert.Text = string.Format("If you did not have Administrative permissions on this block you would have been redirected to: <a href='{0}'>{0}</a>.", CreateRedirectUrl(null));
                    }
                    else
                    {
                        Response.Redirect(CreateRedirectUrl(null));
                        return;
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Handles the Click event of the btnConnect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnConnect_Click(object sender, EventArgs e)
        {
            if (cbAcceptTAC.Visible && cbAcceptTAC.Checked == false)
            {
                nbAlert.Text    = string.Format("You must check \"{0}\" to continue.", GetAttributeValue("AcceptanceLabel"));
                nbAlert.Visible = true;
                return;
            }

            // We know there is a device with the stored MAC
            // If we have an alias ID then we have all data needed and can redirect the user to frontporch
            if (hfPersonAliasId.Value != string.Empty)
            {
                UpdatePersonInfo();
                Response.Redirect(CreateRedirectUrl(int.Parse(hfPersonAliasId.Value)));
                return;
            }

            PersonService personService     = new PersonService(new RockContext());
            int           mobilePhoneTypeId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE).Id;

            // Use the entered info to try and find an existing user
            string mobilePhoneNumber = string.Concat(tbMobilePhone.Text.Where(c => char.IsLetterOrDigit(c)));
            Person person            = personService.GetByMatch(tbFirstName.Text, tbLastName.Text, dppDOB.SelectedDate, tbEmail.Text, mobilePhoneNumber, null, null).FirstOrDefault();

            /*Person person = personService
             *  .Queryable()
             *  .Where( p => p.FirstName == tbFirstName.Text )
             *  .Where( p => p.LastName == tbLastName.Text )
             *  .Where( p => p.Email == tbEmail.Text )
             *  .Where( p => p.PhoneNumbers.Where( n => n.NumberTypeValueId == mobilePhoneTypeId ).FirstOrDefault().Number == mobilePhoneNumber )
             *  .FirstOrDefault();*/

            // If no known person record then create one
            if (person == null)
            {
                person = new Person {
                    FirstName    = tbFirstName.Text,
                    LastName     = tbLastName.Text,
                    Email        = tbEmail.Text,
                    PhoneNumbers = new List <PhoneNumber>()
                    {
                        new PhoneNumber {
                            IsSystem = false, Number = string.Concat(tbMobilePhone.Text.Where(c => char.IsLetterOrDigit(c))), NumberTypeValueId = mobilePhoneTypeId
                        }
                    }
                };
                if (dppDOB.SelectedDate.HasValue)
                {
                    person.BirthDay   = dppDOB.SelectedDate.Value.Day;
                    person.BirthMonth = dppDOB.SelectedDate.Value.Month;
                    person.BirthYear  = dppDOB.SelectedDate.Value.Year;
                }

                PersonService.SaveNewPerson(person, new RockContext());
            }

            // Link new device to person alias
            RockPage.LinkPersonAliasToDevice(person.PrimaryAlias.Id, hfMacAddress.Value);

            // Send them back to Front Porch
            Response.Redirect(CreateRedirectUrl(person.PrimaryAlias.Id));
            return;
        }
Пример #5
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            nbAlert.Visible = false;

            // Go through the UA ignore list and don't load anything we don't care about or want.
            foreach (string userAgentToIgnore in _userAgentsToIgnore)
            {
                if (Request.UserAgent.StartsWith(userAgentToIgnore))
                {
                    return;
                }
            }

            if (!IsPostBack)
            {
                string macAddress = RockPage.PageParameter(GetAttributeValue("MacAddressParam"));
                Regex  regex      = new Regex("^([0-9a-fA-F]{2}(?:[:-]?[0-9a-fA-F]{2}){5})$");
                if (string.IsNullOrWhiteSpace(macAddress) || !regex.IsMatch(macAddress))
                {
                    nbAlert.Text    = "Missing or invalid MAC Address";
                    nbAlert.Visible = true;
                    ShowControls(false);
                    return;
                }

                string releaseLink = GetAttributeValue("ReleaseLink");

                Uri     uriResult;
                Boolean validUrl = Uri.TryCreate(releaseLink, UriKind.Absolute, out uriResult) &&
                                   (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
                if (string.IsNullOrWhiteSpace(releaseLink) || !validUrl)
                {
                    nbAlert.Text    = "Missing or invalid Release Link";
                    nbAlert.Visible = true;
                    ShowControls(false);
                    return;
                }

                // Save the supplied MAC address to the page removing any non-Alphanumeric characters
                macAddress         = string.Concat(macAddress.Where(c => char.IsLetterOrDigit(c)));
                hfMacAddress.Value = macAddress;

                RockContext rockContext = new RockContext();

                // create or get device
                PersonalDeviceService personalDeviceService = new PersonalDeviceService(rockContext);
                PersonalDevice        personalDevice        = null;

                bool isAnExistingDevice = DoesPersonalDeviceExist(macAddress);
                if (isAnExistingDevice)
                {
                    personalDevice = VerifyDeviceInfo(macAddress);
                }
                else
                {
                    personalDevice = CreateDevice(macAddress);
                    CreateDeviceCookie(macAddress);
                }

                // See if user is logged and link the alias to the device.
                if (CurrentPerson != null)
                {
                    Prefill(CurrentPerson);
                    RockPage.LinkPersonAliasToDevice(( int )CurrentPersonAliasId, macAddress);
                    hfPersonAliasId.Value = CurrentPersonAliasId.ToString();
                }
                else if (isAnExistingDevice)
                {
                    // if the user is not logged in but we have the device lets try to get a person
                    if (personalDevice.PersonAliasId != null)
                    {
                        // Get the person
                        PersonService personService = new PersonService(rockContext);
                        Person        person        = personService.Get(personalDevice.PersonAlias.PersonId);

                        if (person != null)
                        {
                            Prefill(person);
                            RockPage.LinkPersonAliasToDevice(( int )personalDevice.PersonAliasId, macAddress);
                            hfPersonAliasId.Value = personalDevice.PersonAliasId.ToString();
                        }
                    }
                }

                // Direct connect if no controls are visible
                if (!ShowControls())
                {
                    // Nothing to show means nothing to enter. Redirect user back to FP with the primary alias ID and query string
                    if (IsUserAuthorized(Rock.Security.Authorization.ADMINISTRATE))
                    {
                        nbAlert.Text = string.Format("If you did not have Administrative permissions on this block you would have been redirected to: <a href='{0}'>{0}</a>.", CreateRedirectUrl(null));
                    }
                    else
                    {
                        Response.Redirect(CreateRedirectUrl(null));
                        return;
                    }
                }
            }
        }