Example #1
0
        private bool BeforeSavePerson(Person person, EntityInfo info)
        {
            if (info.EntityState == EntityState.Added)
            {
                person.UserId = UserId;

                // assign a new color if necessary
                if (string.IsNullOrEmpty(person.Color))
                {
                    // default to first color
                    person.Color = Colors.List[0];

                    // try to assign a color that hasn't yet been assigned
                    var colors = People.Select(p => p.Color).Distinct();
                    for (int i = 0; i < Colors.List.Count; i++)
                    {
                        if (!colors.Contains(Colors.List[i]))
                        {
                            person.Color = Colors.List[i];
                            break;
                        }
                    }
                }
                return(true);
            }
            if (info.EntityState == EntityState.Deleted)
            {
                // reassign all devices to Shared
                var shared = ValidationRepository.People.FirstOrDefault(p => p.Name == "Shared");
                if (shared != null)
                {
                    var devices = ValidationRepository.Devices.Where(d => d.PersonId == person.PersonId);
                    foreach (var d in devices)
                    {
                        d.PersonId = shared.PersonId;
                    }
                    ValidationRepository.SaveChanges();
                }
                return(true);
            }
            return(UserId == person.UserId || throwCannotSaveEntityForThisUser(person.UserId));
        }
Example #2
0
        private bool BeforeSaveDevice(Device device, EntityInfo info)
        {
            if (info.EntityState == EntityState.Deleted)
            {
                // when the client tries to delete a device, instead of deleting it we mark it with an
                // enabled status of NULL.  this is so that the next time the client Posts records, the
                // ControlMessage returned can instruct the device to dissasociate.  at that point, the
                // device is actually removed.
                var dbDevice = ValidationRepository.Devices.FirstOrDefault(d => d.DeviceId == device.DeviceId);
                if (dbDevice != null)
                {
                    dbDevice.Enabled = null;
                    ValidationRepository.SaveChanges();
                    return(false);
                }
            }

            var person = device.PersonId.HasValue ? ValidationContext.People.Find(device.PersonId) : null;

            return((null != person)
                       ? UserId == person.UserId || throwPersonDoesNotBelongToThisUser(person.UserId)
                       : UserId == device.UserId || throwCannotSaveEntityForThisUser(device.UserId));
        }