Пример #1
0
        public void ControlledUpdate_EntityWithMigrationLevelAuthorization_OverrideAnyLevel()
        {
            //ARRANGE
            const string higherContext = "HigherUser";
            var          auth          = new Dictionary <string, string> {
                { "Name", higherContext }
            };
            string authXml = SerialisationUtils.ToXml(auth);
            ControlledUpdateThing thing
                = new ControlledUpdateThing
                {
                DbName = "Test Thing",
                DbPropertyAuthorization = authXml
                };
            const string newName = "New Thing";

            //ACT
            using (PebblecodeUpdateContexts.Migration(thing))
            {
                thing.Name = newName;
            }

            //ASSERT
            Assert.AreEqual(newName, thing.Name);
        }
Пример #2
0
        public void ControlledUpdate_EntityWithMigrationLevelAuthorization_KeepUpdateLevelUnchanged()
        {
            //ARRANGE
            const string previousHigherContext = "HigherUser";
            var          auth = new Dictionary <string, string> {
                { "Name", previousHigherContext }
            };
            string authXml = SerialisationUtils.ToXml(auth);
            ControlledUpdateThing thing
                = new ControlledUpdateThing
                {
                DbName = "Test Thing",
                DbPropertyAuthorization = authXml
                };
            const string newName = "New Thing";

            //ACT
            using (PebblecodeUpdateContexts.Migration(thing))
            {
                thing.Name = newName;
            }
            Dictionary <string, string> currentAuth =
                SerialisationUtils.FromXml <Dictionary <string, string> >(thing.DbPropertyAuthorization);

            //ASSERT
            Assert.IsTrue(currentAuth.ContainsKey("Name"));
            Assert.AreEqual(previousHigherContext, currentAuth["Name"]);
        }
Пример #3
0
        public void ControlledUpdate_EntityWithNoPrviousAuthorization_AllowPropertyToBeSet()
        {
            //ARRANGE
            ControlledUpdateThing thing
                = new ControlledUpdateThing
                {
                DbName = "Test Thing",
                DbPropertyAuthorization = GetBasicAuthXml()
                };
            const string newName = "New Thing";

            //ACT
            using (PebblecodeUpdateContexts.LowerUser(thing))
            {
                thing.Name = newName;
            }

            //ASSERT
            Assert.AreEqual(newName, thing.Name);
        }
        /// <summary>
        /// Save (insert/update) a ControlledUpdateThing into the store
        /// </summary>
        /// <param name="entitiesBeingHandled">Entities already being saved further up the save stack</param>
        /// <param name="controlledUpdateThings">The ControlledUpdateThings to save</param>
        /// <param name="toSave">Entity types to cascade to, if they are loaded</param>
        internal void Save(List <Entity> entitiesBeingHandled, Flags toSave, params ControlledUpdateThing[] controlledUpdateThings)
        {
            if (controlledUpdateThings == null)
            {
                throw new ArgumentNullException("controlledUpdateThings");
            }
            Log("Save", controlledUpdateThings.Select <ControlledUpdateThing, int>(entity => entity.Identity).ToArray <int>(), EntityType.None);

            // Copy the list of entities being handled, and add this new set of entities to it.
            // We're handling those now.
            List <Entity> entitiesNowBeingHandled = new List <Entity>(entitiesBeingHandled);

            entitiesNowBeingHandled.AddRange(controlledUpdateThings);

            // Loop over each entity and save it.
            foreach (ControlledUpdateThing controlledUpdateThing in controlledUpdateThings)
            {
                using (PebblecodeUpdateContexts.PebbleAdmin(controlledUpdateThing)) {
                    // Already being saved higher up the stack?
                    if (entitiesBeingHandled.ContainsEntity(controlledUpdateThing))
                    {
                        continue;
                    }

                    // Allow derived/partial class to do extra work
                    OnBeforeSaveEntity(controlledUpdateThing);

                    bool saved = false;

                    try
                    {
                        // Save the entity
                        if (controlledUpdateThing.IsNew)
                        {
                            this.Mapper.Insert("InsertControlledUpdateThing", controlledUpdateThing);
                            saved = true;
                        }
                        else if (controlledUpdateThing.IsChanged)
                        {
                            if (this.Mapper.Update("UpdateControlledUpdateThing", controlledUpdateThing) != 1)
                            {
                                ThrowControlledUpdateThingEntityException(controlledUpdateThing.Identity);
                            }
                            saved = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw EntityLogger.WriteUnexpectedException(
                                  ex,
                                  "Failed to insert/update Entity",
                                  Category.EntityFramework,
                                  controlledUpdateThing);
                    }

                    // Post save protocol
                    if (saved)
                    {
                        // Allow derived/partial class to do extra work
                        OnAfterSaveEntity(controlledUpdateThing);
                        try
                        {
                            VersionedEntityInfo versionInfo = this.Mapper.QueryForObject <VersionedEntityInfo>(
                                "SelectControlledUpdateThingVersionInfo",
                                controlledUpdateThing.Identity);
                            controlledUpdateThing.Reset(versionInfo);
                        }
                        catch (Exception ex)
                        {
                            throw EntityLogger.WriteUnexpectedException(
                                      ex,
                                      "Failed to reset version information",
                                      Category.EntityFramework,
                                      controlledUpdateThing);
                        }

                        //The insert/update will have resulted in a new database_update row, inform interested parties
                        RaiseModelChanged();
                    }
                }
            }
        }