Esempio n. 1
0
        public IHttpActionResult Login([FromBody] LoginParameters loginParameters)
        {
            var authController = new AuthController();
            var site           = MobileHelper.GetCurrentApplicationSite();

            if (site == null)
            {
                return(StatusCode(System.Net.HttpStatusCode.Unauthorized));
            }

            //
            // Chain to the existing login method for actual authorization check.
            // Throws exception if not authorized.
            //
            authController.Login(loginParameters);

            //
            // Find the user and translate to a mobile person.
            //
            var userLoginService = new UserLoginService(new Rock.Data.RockContext());
            var userLogin        = userLoginService.GetByUserName(loginParameters.Username);
            var mobilePerson     = MobileHelper.GetMobilePerson(userLogin.Person, site);

            mobilePerson.AuthToken = MobileHelper.GetAuthenticationToken(loginParameters.Username);

            return(Ok(mobilePerson));
        }
Esempio n. 2
0
        public IHttpActionResult Login([FromBody] LoginParameters loginParameters, Guid?personalDeviceGuid = null)
        {
            var site = MobileHelper.GetCurrentApplicationSite();

            if (site == null)
            {
                return(StatusCode(System.Net.HttpStatusCode.Unauthorized));
            }

            //
            // Use the existing AuthController.IsLoginValid method for actual authorization check. Throws exception if not authorized.
            //
            if (!AuthController.IsLoginValid(loginParameters, out var errorMessage, out var userName))
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(System.Net.HttpStatusCode.Unauthorized, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            //
            // Find the user and translate to a mobile person.
            //
            using (var rockContext = new Rock.Data.RockContext())
            {
                var userLoginService = new UserLoginService(rockContext);
                var userLogin        = userLoginService.GetByUserName(loginParameters.Username);

                if (personalDeviceGuid.HasValue)
                {
                    var personalDevice = new PersonalDeviceService(rockContext).Get(personalDeviceGuid.Value);

                    if (personalDevice != null && personalDevice.PersonAliasId != userLogin.Person.PrimaryAliasId)
                    {
                        personalDevice.PersonAliasId = userLogin.Person.PrimaryAliasId;
                    }
                }

                userLogin.LastLoginDateTime = RockDateTime.Now;

                rockContext.SaveChanges();

                var mobilePerson = MobileHelper.GetMobilePerson(userLogin.Person, site);
                mobilePerson.AuthToken = MobileHelper.GetAuthenticationToken(loginParameters.Username);

                return(Ok(mobilePerson));
            }
        }
Esempio n. 3
0
        public object GetLaunchPacket()
        {
            var baseUrl            = GlobalAttributesCache.Value("PublicApplicationRoot");
            var site               = MobileHelper.GetCurrentApplicationSite();
            var additionalSettings = site?.AdditionalSettings.FromJsonOrNull <AdditionalSiteSettings>();
            var person             = GetPerson();
            var deviceData         = Request.GetHeader("X-Rock-DeviceData").FromJsonOrNull <DeviceData>();

            if (additionalSettings == null || !additionalSettings.LastDeploymentDate.HasValue)
            {
                return(NotFound());
            }

            var launchPacket = new LaunchPackage
            {
                LatestVersionId     = ( int )(additionalSettings.LastDeploymentDate.Value.ToJavascriptMilliseconds() / 1000),
                IsSiteAdministrator = site.IsAuthorized(Authorization.EDIT, person)
            };

            if (deviceData.DeviceType == DeviceType.Phone)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.PhoneUpdatePackageUrl;
            }
            else if (deviceData.DeviceType == DeviceType.Tablet)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.TabletUpdatePackageUrl;
            }
            else
            {
                return(NotFound());
            }

            if (person != null)
            {
                var principal = ControllerContext.Request.GetUserPrincipal();

                launchPacket.CurrentPerson           = MobileHelper.GetMobilePerson(person, site);
                launchPacket.CurrentPerson.AuthToken = MobileHelper.GetAuthenticationToken(principal.Identity.Name);
            }

            return(launchPacket);
        }
Esempio n. 4
0
        public IHttpActionResult Login([FromBody] LoginParameters loginParameters, Guid?personalDeviceGuid = null)
        {
            var authController = new AuthController();
            var site           = MobileHelper.GetCurrentApplicationSite();

            if (site == null)
            {
                return(StatusCode(System.Net.HttpStatusCode.Unauthorized));
            }

            //
            // Chain to the existing login method for actual authorization check.
            // Throws exception if not authorized.
            //
            authController.Login(loginParameters);

            //
            // Find the user and translate to a mobile person.
            //
            using (var rockContext = new Rock.Data.RockContext())
            {
                var userLoginService = new UserLoginService(rockContext);
                var userLogin        = userLoginService.GetByUserName(loginParameters.Username);

                if (personalDeviceGuid.HasValue)
                {
                    var personalDevice = new PersonalDeviceService(rockContext).Get(personalDeviceGuid.Value);

                    if (personalDevice != null && personalDevice.PersonAliasId != userLogin.Person.PrimaryAliasId)
                    {
                        personalDevice.PersonAliasId = userLogin.Person.PrimaryAliasId;
                        rockContext.SaveChanges();
                    }
                }

                var mobilePerson = MobileHelper.GetMobilePerson(userLogin.Person, site);
                mobilePerson.AuthToken = MobileHelper.GetAuthenticationToken(loginParameters.Username);

                return(Ok(mobilePerson));
            }
        }
Esempio n. 5
0
        public IHttpActionResult GetLaunchPacket(string deviceIdentifier = null)
        {
            var site = MobileHelper.GetCurrentApplicationSite();
            var additionalSettings = site?.AdditionalSettings.FromJsonOrNull <AdditionalSiteSettings>();
            var rockContext        = new Rock.Data.RockContext();
            var person             = GetPerson(rockContext);
            var deviceData         = Request.GetHeader("X-Rock-DeviceData").FromJsonOrNull <DeviceData>();

            if (additionalSettings == null || !additionalSettings.LastDeploymentDate.HasValue)
            {
                return(NotFound());
            }

            var launchPacket = new LaunchPacket
            {
                LatestVersionId     = ( int )(additionalSettings.LastDeploymentDate.Value.ToJavascriptMilliseconds() / 1000),
                IsSiteAdministrator = site.IsAuthorized(Authorization.EDIT, person)
            };

            if (deviceData.DeviceType == DeviceType.Phone)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.PhoneUpdatePackageUrl;
            }
            else if (deviceData.DeviceType == DeviceType.Tablet)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.TabletUpdatePackageUrl;
            }
            else
            {
                return(NotFound());
            }

            if (person != null)
            {
                var principal = ControllerContext.Request.GetUserPrincipal();

                launchPacket.CurrentPerson           = MobileHelper.GetMobilePerson(person, site);
                launchPacket.CurrentPerson.AuthToken = MobileHelper.GetAuthenticationToken(principal.Identity.Name);
            }

            //
            // Get or create the personal device.
            //
            if (deviceIdentifier.IsNotNullOrWhiteSpace())
            {
                var mobileDeviceTypeValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSONAL_DEVICE_TYPE_MOBILE).Id;
                var personalDeviceService   = new PersonalDeviceService(rockContext);
                var personalDevice          = personalDeviceService.Queryable()
                                              .AsNoTracking()
                                              .Where(a => a.DeviceUniqueIdentifier == deviceIdentifier && a.PersonalDeviceTypeValueId == mobileDeviceTypeValueId)
                                              .FirstOrDefault();

                if (personalDevice == null)
                {
                    personalDevice = new PersonalDevice
                    {
                        DeviceUniqueIdentifier    = deviceIdentifier,
                        PersonalDeviceTypeValueId = mobileDeviceTypeValueId,
                        PlatformValueId           = deviceData.DevicePlatform.GetDevicePlatformValueId(),
                        PersonAliasId             = person?.PrimaryAliasId,
                        NotificationsEnabled      = true
                    };

                    personalDeviceService.Add(personalDevice);
                    rockContext.SaveChanges();
                }

                launchPacket.PersonalDeviceGuid = personalDevice.Guid;
            }

            return(Ok(launchPacket));
        }
Esempio n. 6
0
        public object UpdateProfile(MobilePerson profile)
        {
            var user = UserLoginService.GetCurrentUser(false);

            if (user == null)
            {
                return(ActionStatusCode(System.Net.HttpStatusCode.Unauthorized));
            }

            var personId    = user.PersonId.Value;
            var rockContext = new Data.RockContext();

            var personService      = new PersonService(rockContext);
            var phoneNumberService = new PhoneNumberService(rockContext);
            var person             = personService.Get(personId);

            person.NickName  = person.NickName == person.FirstName ? profile.FirstName : person.NickName;
            person.FirstName = profile.FirstName;
            person.LastName  = profile.LastName;

            var gender = (Model.Gender)profile.Gender;

            if (GenderVisibility != VisibilityTriState.Hidden)
            {
                person.Gender = gender;
            }

            if (GetAttributeValue(AttributeKeys.BirthDateShow).AsBoolean())
            {
                person.SetBirthDate(profile.BirthDate?.Date);
            }

            if (GetAttributeValue(AttributeKeys.CampusShow).AsBoolean())
            {
                person.PrimaryFamily.CampusId = profile.CampusGuid.HasValue ? CampusCache.Get(profile.CampusGuid.Value)?.Id : null;
            }

            if (GetAttributeValue(AttributeKeys.EmailShow).AsBoolean())
            {
                person.Email = profile.Email;
            }

            if (GetAttributeValue(AttributeKeys.MobilePhoneShow).AsBoolean())
            {
                int phoneNumberTypeId = DefinedValueCache.Get(SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE).Id;

                var phoneNumber = person.PhoneNumbers.FirstOrDefault(n => n.NumberTypeValueId == phoneNumberTypeId);
                if (phoneNumber == null)
                {
                    phoneNumber = new PhoneNumber {
                        NumberTypeValueId = phoneNumberTypeId
                    };
                    person.PhoneNumbers.Add(phoneNumber);
                }

                // TODO: What to do with country code?
                phoneNumber.CountryCode = PhoneNumber.CleanNumber("+1");
                phoneNumber.Number      = PhoneNumber.CleanNumber(profile.MobilePhone);

                if (string.IsNullOrWhiteSpace(phoneNumber.Number))
                {
                    person.PhoneNumbers.Remove(phoneNumber);
                    phoneNumberService.Delete(phoneNumber);
                }
            }

            if (GetAttributeValue(AttributeKeys.AddressShow).AsBoolean())
            {
                var addressTypeGuid = SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid();

                var groupLocationService = new GroupLocationService(rockContext);

                var dvHomeAddressType = DefinedValueCache.Get(addressTypeGuid);
                var familyAddress     = groupLocationService.Queryable().Where(l => l.GroupId == person.PrimaryFamily.Id && l.GroupLocationTypeValueId == dvHomeAddressType.Id).FirstOrDefault();

                if (familyAddress != null && string.IsNullOrWhiteSpace(profile.HomeAddress.Street1))
                {
                    // delete the current address
                    groupLocationService.Delete(familyAddress);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(profile.HomeAddress.Street1))
                    {
                        if (familyAddress == null)
                        {
                            familyAddress = new GroupLocation();
                            groupLocationService.Add(familyAddress);
                            familyAddress.GroupLocationTypeValueId = dvHomeAddressType.Id;
                            familyAddress.GroupId           = person.PrimaryFamily.Id;
                            familyAddress.IsMailingLocation = true;
                            familyAddress.IsMappedLocation  = true;
                        }
                        else if (familyAddress.Location.Street1 != profile.HomeAddress.Street1)
                        {
                            // user clicked move so create a previous address
                            var previousAddress = new GroupLocation();
                            groupLocationService.Add(previousAddress);

                            var previousAddressValue = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_PREVIOUS.AsGuid());
                            if (previousAddressValue != null)
                            {
                                previousAddress.GroupLocationTypeValueId = previousAddressValue.Id;
                                previousAddress.GroupId = person.PrimaryFamily.Id;

                                Location previousAddressLocation = new Location
                                {
                                    Street1    = familyAddress.Location.Street1,
                                    Street2    = familyAddress.Location.Street2,
                                    City       = familyAddress.Location.City,
                                    State      = familyAddress.Location.State,
                                    PostalCode = familyAddress.Location.PostalCode,
                                    Country    = familyAddress.Location.Country
                                };

                                previousAddress.Location = previousAddressLocation;
                            }
                        }

                        // TODO: ???
                        // familyAddress.IsMailingLocation = cbIsMailingAddress.Checked;
                        // familyAddress.IsMappedLocation = cbIsPhysicalAddress.Checked;
                        familyAddress.Location = new LocationService(rockContext).Get(
                            profile.HomeAddress.Street1,
                            string.Empty,
                            profile.HomeAddress.City,
                            profile.HomeAddress.State,
                            profile.HomeAddress.PostalCode,
                            profile.HomeAddress.Country,
                            person.PrimaryFamily,
                            true);

                        // since there can only be one mapped location, set the other locations to not mapped
                        if (familyAddress.IsMappedLocation)
                        {
                            var groupLocations = groupLocationService.Queryable()
                                                 .Where(l => l.GroupId == person.PrimaryFamily.Id && l.Id != familyAddress.Id).ToList();

                            foreach (var groupLocation in groupLocations)
                            {
                                groupLocation.IsMappedLocation = false;
                            }
                        }

                        rockContext.SaveChanges();
                    }
                }
            }

            rockContext.SaveChanges();

            var mobilePerson = MobileHelper.GetMobilePerson(person, MobileHelper.GetCurrentApplicationSite());

            mobilePerson.AuthToken = MobileHelper.GetAuthenticationToken(user.UserName);

            return(ActionOk(mobilePerson));
        }
Esempio n. 7
0
        public IHttpActionResult GetLaunchPacket(string deviceIdentifier = null, bool?notificationsEnabled = null)
        {
            var site = MobileHelper.GetCurrentApplicationSite();
            var additionalSettings = site?.AdditionalSettings.FromJsonOrNull <AdditionalSiteSettings>();
            var rockContext        = new Rock.Data.RockContext();
            var person             = GetPerson(rockContext);
            var deviceData         = Request.GetHeader("X-Rock-DeviceData").FromJsonOrNull <DeviceData>();

            if (additionalSettings == null || !additionalSettings.LastDeploymentDate.HasValue)
            {
                return(NotFound());
            }

            // Ensure the user login is still active, otherwise log them out.
            var principal = ControllerContext.Request.GetUserPrincipal();

            if (person != null && !principal.Identity.Name.StartsWith("rckipid="))
            {
                var userLogin = new UserLoginService(rockContext).GetByUserName(principal.Identity.Name);

                if (userLogin?.IsConfirmed != true || userLogin?.IsLockedOut == true)
                {
                    person = null;
                }
            }

            var launchPacket = new LaunchPacket
            {
                RockVersion         = Rock.VersionInfo.VersionInfo.GetRockProductVersionNumber(),
                LatestVersionId     = additionalSettings.LastDeploymentVersionId ?? ( int )(additionalSettings.LastDeploymentDate.Value.ToJavascriptMilliseconds() / 1000),
                IsSiteAdministrator = site.IsAuthorized(Rock.Security.Authorization.EDIT, person)
            };

            if (deviceData.DeviceType == DeviceType.Phone)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.PhoneUpdatePackageUrl;
            }
            else if (deviceData.DeviceType == DeviceType.Tablet)
            {
                launchPacket.LatestVersionSettingsUrl = additionalSettings.TabletUpdatePackageUrl;
            }
            else
            {
                return(NotFound());
            }

            if (person != null)
            {
                //var principal = ControllerContext.Request.GetUserPrincipal();

                launchPacket.CurrentPerson           = MobileHelper.GetMobilePerson(person, site);
                launchPacket.CurrentPerson.AuthToken = MobileHelper.GetAuthenticationToken(principal.Identity.Name);

                UserLoginService.UpdateLastLogin(principal.Identity.Name);
            }

            //
            // Get or create the personal device.
            //
            if (deviceIdentifier.IsNotNullOrWhiteSpace())
            {
                var mobileDeviceTypeValueId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSONAL_DEVICE_TYPE_MOBILE).Id;
                var personalDeviceService   = new PersonalDeviceService(rockContext);
                var personalDevice          = personalDeviceService.Queryable()
                                              .Where(a => a.DeviceUniqueIdentifier == deviceIdentifier && a.PersonalDeviceTypeValueId == mobileDeviceTypeValueId && a.SiteId == site.Id)
                                              .FirstOrDefault();

                if (personalDevice == null)
                {
                    personalDevice = new PersonalDevice
                    {
                        DeviceUniqueIdentifier    = deviceIdentifier,
                        PersonalDeviceTypeValueId = mobileDeviceTypeValueId,
                        SiteId               = site.Id,
                        PlatformValueId      = deviceData.DevicePlatform.GetDevicePlatformValueId(),
                        PersonAliasId        = person?.PrimaryAliasId,
                        NotificationsEnabled = true,
                        Manufacturer         = deviceData.Manufacturer,
                        Model            = deviceData.Model,
                        Name             = deviceData.Name,
                        LastSeenDateTime = RockDateTime.Now
                    };

                    personalDeviceService.Add(personalDevice);
                    rockContext.SaveChanges();
                }
                else
                {
                    // A change is determined as one of the following:
                    // 1) A change in Name, Manufacturer, Model, or NotificationsEnabled.
                    // 2) Device not being active.
                    // 3) Not seen in 24 hours.
                    // 4) Signed in with a different person.
                    var hasDeviceChanged = !personalDevice.IsActive ||
                                           personalDevice.Name != deviceData.Name ||
                                           personalDevice.Manufacturer != deviceData.Manufacturer ||
                                           personalDevice.Model != deviceData.Model ||
                                           personalDevice.NotificationsEnabled != (notificationsEnabled ?? true) ||
                                           !personalDevice.LastSeenDateTime.HasValue ||
                                           personalDevice.LastSeenDateTime.Value.AddDays(1) < RockDateTime.Now ||
                                           (person != null && personalDevice.PersonAliasId != person.PrimaryAliasId);

                    if (hasDeviceChanged)
                    {
                        personalDevice.IsActive         = true;
                        personalDevice.Manufacturer     = deviceData.Manufacturer;
                        personalDevice.Model            = deviceData.Model;
                        personalDevice.Name             = deviceData.Name;
                        personalDevice.LastSeenDateTime = RockDateTime.Now;

                        if (notificationsEnabled.HasValue)
                        {
                            personalDevice.NotificationsEnabled = notificationsEnabled.Value;
                        }

                        // Update the person tied to the device, but never blank it out.
                        if (person != null && personalDevice.PersonAliasId != person.PrimaryAliasId)
                        {
                            personalDevice.PersonAliasId = person.PrimaryAliasId;
                        }

                        rockContext.SaveChanges();
                    }
                }

                launchPacket.PersonalDeviceGuid = personalDevice.Guid;
            }

            return(Ok(launchPacket));
        }