Esempio n. 1
0
        /// <summary>Gets the command used to publish an item.</summary>
        /// <param name="context">The command context used to determine which command to return.</param>
        /// <returns>A command that when executed will publish an item.</returns>
		public virtual CompositeCommand GetPublishCommand(CommandContext context)
        {
			var item = context.Content;

            if (!item.IsPage)
                throw new ArgumentException("Publish requires item to be a page");

            if (item is IActiveContent)
				return Compose("Publish active content", Authorize(Permission.Publish), validate, updateObject, moveToPosition, saveActiveContent, updateReferences);
                
            // Editing
			if (!item.VersionOf.HasValue)
			{
                return Compose("Publish 1", Authorize(Permission.Publish), validate, 
                    (item.ID == 0) ? (CommandBase<CommandContext>) null : makeVersion, 
                    updateObject, // UPDATE
                    publishedState, moveToPosition, save, updateReferences);
			}

			// has been published before
			if (item.State == ContentState.Unpublished)
				 return Compose("Re-Publish", Authorize(Permission.Publish), validate, 
                     replaceMaster, useMaster, // REPLACE & USE
                     publishedState, moveToPosition, save, updateReferences);

			// has never been published before (remove old version)
			return Compose("Publish 2", Authorize(Permission.Publish), validate, 
                    updateObject, replaceMaster, delete, useMaster, // TODO check
                    publishedState, moveToPosition, save, updateReferences);
		}
Esempio n. 2
0
 public bool UpdateObject(N2.Edit.Workflow.CommandContext value)
 {
     try
     {
         BinderContext = value;
         EnsureChildControls();
         if (ZoneName != null && ZoneName != value.Content.ZoneName)
         {
             value.Content.ZoneName = ZoneName;
         }
         foreach (string key in AddedEditors.Keys)
         {
             BinderContext.GetDefinedDetails().Add(key);
         }
         var modifiedDetails = EditAdapter.UpdateItem(value.Definition, value.Content, AddedEditors, Page.User);
         if (modifiedDetails.Length == 0)
         {
             return(false);
         }
         foreach (string detailName in modifiedDetails)
         {
             BinderContext.GetUpdatedDetails().Add(detailName);
         }
         BinderContext.RegisterItemToSave(value.Content);
         return(true);
     }
     finally
     {
         BinderContext = null;
     }
 }
Esempio n. 3
0
        /// <summary>Executes the supplied command</summary>
        /// <param name="command">The command to execute.</param>
        /// <param name="context">The context passed to the command</param>
        public virtual void Execute(CommandBase<CommandContext> command, CommandContext context)
        {
            var args = new CommandProcessEventArgs { Command = command, Context = context };
            if (CommandExecuting != null)
                CommandExecuting.Invoke(this, args);

            logger.Info(args.Command.Name + " processing " + args.Context);
            using (var tx = persister.Repository.BeginTransaction())
            {
                try
                {
                    args.Command.Process(args.Context);
                    tx.Commit();

                    if (CommandExecuted != null)
                        CommandExecuted.Invoke(this, args);
                }
                catch (StopExecutionException)
                {
                    tx.Rollback();
                }
                catch (Exception ex)
                {
                    tx.Rollback();
                    logger.Error(ex);
                    throw;
                }
                finally
                {
                    logger.Info(" -> " + args.Context);
                }
            }
        }
 /// <summary>Executes the supplied command</summary>
 /// <param name="command">The command to execute.</param>
 /// <param name="context">The context passed to the command</param>
 public virtual void Execute(CommandBase<CommandContext> command, CommandContext context)
 {
     logger.Info(command.Name + " processing " + context);
     using (var tx = persister.Repository.BeginTransaction())
     {
         try
         {
             command.Process(context);
             tx.Commit();
         }
         catch (StopExecutionException)
         {
             tx.Rollback();
         }
         catch (Exception ex)
         {
             tx.Rollback();
             logger.Error(ex);
             throw;
         }
         finally
         {
             logger.Info(" -> " + context);
         }
     }
 }
Esempio n. 5
0
        /// <summary>Gets the command used to publish an item.</summary>
        /// <param name="context">The command context used to determine which command to return.</param>
        /// <returns>A command that when executed will publish an item.</returns>
        public virtual CompositeCommand GetPublishCommand(CommandContext context)
        {
            var item = context.Content;

            if (item is IActiveContent)
                return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, moveToPosition, saveActiveContent, updateReferences);
                
            // Editing
            if (!item.VersionOf.HasValue)
            {
                if(item.ID == 0)
                    return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, publishedState, moveToPosition, save, updateReferences);

                return Compose("Publish", Authorize(Permission.Publish), validate, MakeVersionIfPublished(item), updateObject, publishedState, moveToPosition, ensurePublishedDate, save, updateReferences);
            }

            // has been published before
            if (item.State == ContentState.Unpublished)
                return Compose("Re-Publish", Authorize(Permission.Publish), validate, replaceMaster, useMaster, publishedState, moveToPosition, ensurePublishedDate, save, updateReferences);

            // has never been published before (remove old version)
            return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, replaceMaster, delete, useMaster, publishedState, moveToPosition, ensurePublishedDate, save, updateReferences);
            
            throw new NotSupportedException();
        }
        public void CreatesVersion_OfVersionableItem()
        {
            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            dispatcher.Execute(CreateCommand(context), context);

            Assert.That(repository.database.Values.Count(v => v.VersionOf.Value == item), Is.GreaterThan(0), "Expected version to be created");
        }
        public void DoesntCreateVersion_OfNonVersionableItem()
        {
            var unversionable = new UnversionableStatefulItem();
            var context = new CommandContext(definitions.GetDefinition(unversionable.GetContentType()), unversionable, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);
            dispatcher.Execute(CreateCommand(context), context);

            dispatcher.Execute(CreateCommand(context), context);
            Assert.That(repository.database.Values.Count(v => v.VersionOf.Value == unversionable), Is.EqualTo(0), "Expected no version to be created");
        }
        public void DoesntMakeVersion_OfUnsavedItem()
        {
            var context = new CommandContext(definitions.GetDefinition(typeof(StatefulItem)), new StatefulItem(), Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(repository.database.Values.Count(v => v.VersionOf.Value == item), Is.EqualTo(0));
        }
        public void DoesntMakeVersion_OfUnsavedItem()
        {
            var context = new CommandContext(definitions.GetDefinition(typeof(StatefulPage)), new StatefulPage(), Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            versions.Repository.Repository.Count().ShouldBe(0);
        }
        public void MakesVersion_OfCurrent()
        {
			var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

			versions.Repository.Repository.Count().ShouldBe(1);
        }
        public void MakesVersion_OfCurrent()
        {
            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(repository.database.Values.Count(v => v.VersionOf == item), Is.EqualTo(1));
        }
Esempio n. 12
0
        public void Clears_PublishedDate()
        {
            var item = new StatefulItem();
            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(item.Published, Is.Null);
        }
Esempio n. 13
0
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ContentItem previewedItem = Selection.SelectedItem;

            var context = new CommandContext(Engine.Definitions.GetDefinition(previewedItem), previewedItem, Interfaces.Viewing, Page.User);
            Engine.Resolve<CommandDispatcher>().Publish(context);

            Response.Redirect(context.Content.Url);
        }
        public void PreviouslyPublishedVersion_CausesNewVersion_FromView()
        {
            var version = MakeVersion(item);

			var context = new CommandContext(definitions.GetDefinition(version.GetContentType()), version, Interfaces.Viewing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(versions.GetVersionsOf(item).Count, Is.EqualTo(3));
        }
        public void Version_MakesVersion_OfCurrentMaster()
        {
            var version = MakeVersion(item);
            var context = new CommandContext(version, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(repository.database.Values.Count(v => v.VersionOf == item), Is.EqualTo(1));
            Assert.That(item.Title, Is.EqualTo("version"));
        }
        public void Version_Replaces_MasterVersion()
        {
            var version = MakeVersion(item);
            version.Name = "tha masta";
			var context = new CommandContext(definitions.GetDefinition(version.GetContentType()), version, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(context.Content.Name, Is.EqualTo("tha masta"));
            Assert.That(context.Content.VersionOf.HasValue, Is.False);
        }
        public void CanMoveItem_ToBefore_Item()
        {
            var child2 = CreateOneItem<StatefulItem>(0, "child2", item);
            var context = new CommandContext(definitions.GetDefinition(child2.GetContentType()), child2, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);
            context.Parameters["MoveBefore"] = child.Path;
            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(item.Children.Count, Is.EqualTo(2));
            Assert.That(item.Children[0], Is.EqualTo(child2));
            Assert.That(item.Children[1], Is.EqualTo(child));
        }
Esempio n. 18
0
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ContentItem previewedItem = Selection.SelectedItem;

            var context = new CommandContext(Engine.Definitions.GetDefinition(previewedItem.GetContentType()), previewedItem, Interfaces.Viewing, Page.User);
            var command = Engine.Resolve<ICommandFactory>().GetPublishCommand(context);
            Engine.Resolve<CommandDispatcher>().Execute(command, context);

            Response.Redirect(context.Content.Url);
        }
Esempio n. 19
0
 public void UpdateInterface(N2.Edit.Workflow.CommandContext value)
 {
     try
     {
         BinderContext = value;
         EnsureChildControls();
         Engine.EditManager.UpdateEditors(GetDefinition(), value.Content, AddedEditors, Page.User);
     }
     finally
     {
         BinderContext = null;
     }
 }
Esempio n. 20
0
        public void IsCheckedForSecurity(string userInterface, bool useVersion)
        {
            DynamicPermissionMap.SetRoles(item, Permission.Read, "None");
            if (useVersion)
                item = MakeVersion(item);
            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, userInterface, CreatePrincipal("someone"), new NullBinder<CommandContext>(), new NullValidator<CommandContext>());

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            Assert.That(context.ValidationErrors.Count, Is.EqualTo(1));
            Assert.That(context.ValidationErrors.First().Name, Is.EqualTo("Unauthorized"));
        }
Esempio n. 21
0
        /// <summary>Gets the command used to publish an item.</summary>
        /// <param name="context">The command context used to determine which command to return.</param>
        /// <returns>A command that when executed will publish an item.</returns>
        public virtual CompositeCommand GetPublishCommand(CommandContext context)
        {
            if (context.Interface == Interfaces.Editing)
            {
                if (context.Content is IActiveContent)
                    return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, moveToPosition, saveActiveContent);

                // Editing
                if (!context.Content.VersionOf.HasValue)
                {
                    if(context.Content.ID == 0)
                        return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);

                    if (context.Content.State == ContentState.Draft && context.Content.Published.HasValue == false)
                        return Compose("Publish", Authorize(Permission.Publish), validate, makeVersion, updateObject, incrementVersionIndex, publishedState, moveToPosition, publishedDate, save);

                    return Compose("Publish", Authorize(Permission.Publish), validate, makeVersion, updateObject, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);
                }

                // has been published before
                if (context.Content.State == ContentState.Unpublished)
                    return Compose("Publish", Authorize(Permission.Publish), validate, updateObject,/* makeVersionOfMaster,*/ replaceMaster, useMaster, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);

                // has never been published before (remove old version)
                return Compose("Publish", Authorize(Permission.Publish), validate, updateObject,/* makeVersionOfMaster,*/ replaceMaster, delete, useMaster, publishedState, moveToPosition/*, publishedDate*/, save);
            }
            else if (context.Interface == Interfaces.Viewing)
            {
                // Viewing
                if (!context.Content.VersionOf.HasValue)
                {
                    if (context.Content.ID == 0)
                        return Compose("Publish", Authorize(Permission.Publish), validate, updateObject, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);

                    if (context.Content.State == ContentState.Draft && context.Content.Published.HasValue == false)
                        return Compose("Publish", Authorize(Permission.Publish), validate, makeVersion, updateObject, incrementVersionIndex, publishedState, moveToPosition, publishedDate, save);

                    return Compose("Publish", Authorize(Permission.Publish), validate, makeVersion, updateObject, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);
                }

                if (context.Content.State == ContentState.Unpublished)
                    return Compose("Re-Publish", Authorize(Permission.Publish),/* makeVersionOfMaster,*/ replaceMaster, useMaster, incrementVersionIndex, publishedState, moveToPosition/*, publishedDate*/, save);

                if (context.Content.State == ContentState.Draft)
                    return Compose("Re-Publish", Authorize(Permission.Publish),/* makeVersionOfMaster,*/ replaceMaster, useMaster, incrementVersionIndex, publishedState, moveToPosition, publishedDate, save);

                return Compose("Publish", Authorize(Permission.Publish),/* makeVersionOfMaster,*/ replaceMaster, delete, useMaster, publishedState, moveToPosition/*, publishedDate*/, save);
            }

            throw new NotSupportedException();
        }
        public void Version_MakesVersion_OfCurrentMaster()
        {
            var version = MakeVersion(item);
			version.State = ContentState.Draft;
			version.Title = "version";

			var context = new CommandContext(definitions.GetDefinition(version.GetContentType()), version, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

			versions.Repository.GetVersions(item).Count().ShouldBe(1);
            Assert.That(item.Title, Is.EqualTo("version"));
        }
        public void IsNotValidated_WhenInterface_IsViewing()
        {
            item = MakeVersion(item);

            var validator = MockRepository.GenerateStub<IValidator<CommandContext>>();
            mocks.ReplayAll();

            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, Interfaces.Viewing, CreatePrincipal("admin"), nullBinder, validator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            validator.AssertWasNotCalled(b => b.Validate(context));
        }
Esempio n. 24
0
        public void IsValidated_WhenInterface_IsEditing(string userInterface, bool useVersion)
        {
            if (useVersion)
                item = MakeVersion(item);

            var validator = mocks.Stub<IValidator<CommandContext>>();
            mocks.ReplayAll();

            var context = new CommandContext(definitions.GetDefinition(item.GetContentType()), item, userInterface, CreatePrincipal("admin"), nullBinder, validator);

            var command = CreateCommand(context);
            dispatcher.Execute(command, context);

            validator.AssertWasCalled(b => b.Validate(context));
        }
		public void UnsavedPart_IsAppendedLast_ToParent()
		{
			var page = CreatePageWithPart();

			var part2 = new StatefulPart();
			part2.Title = "New part 2";
			part2.Parent = page;
			part2.ZoneName = "TheZone";
			var context = new CommandContext(definitions.GetDefinition(page.GetContentType()), part2, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);
			var command = CreateCommand(context);
			dispatcher.Execute(command, context);

			var pageVersion = versions.GetVersion(page, context.Content.VersionIndex);
			var partVersion = pageVersion.Children[0];
			var part2Version = pageVersion.Children[1];
			partVersion.SortOrder.ShouldBeLessThan(part2Version.SortOrder);
		}
Esempio n. 26
0
        public void NewChild_IsSaved()
        {
            var item       = new DecoratedItem();
            var definition = new DefinitionMap().GetOrCreateDefinition(item);
            var editable   = (EditableItemAttribute)definition.Properties["TheItem"].Editable;

            var page            = new Page();
            var enclosingEditor = new ItemEditor();
            var editor          = AddEditorAndInit(item, editable, page, enclosingEditor);

            editable.UpdateEditor(item, editor);

            var ctx = new N2.Edit.Workflow.CommandContext(definition, item, Interfaces.Editing, engine.RequestContext.User);

            enclosingEditor.UpdateObject(ctx);

            ctx.GetItemsToSave().ShouldContain(item.TheItem);
        }
		public void UnsavedPart_CanBeInserted_BeforeSortOrder()
		{
			var page = CreatePageWithPart();

			var part2 = new StatefulPart();
			part2.Title = "New part";
			part2.Name = "NewPart";
			part2.Parent = page;
			part2.ZoneName = "TheZone";
			var context = new CommandContext(definitions.GetDefinition(page.GetContentType()), part2, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);
			context.Parameters["MoveBeforeSortOrder"] = "0";
			var command = CreateCommand(context);
			dispatcher.Execute(command, context);

			var pageVersion = versions.GetVersion(page, context.Content.VersionIndex);
			var partVersion = pageVersion.Children["ThePart"];
			var part2Version = pageVersion.Children["NewPart"];
			part2Version.SortOrder.ShouldBeLessThan(partVersion.SortOrder);
		}
		public void UnsavedPart_IsSavedOnNewPageVersion()
		{
			var page = new StatefulPage();
			page.Title = "The page";
			persister.Save(page);
			
			var part = new StatefulPart();
			part.Title = "New part";
			part.Parent = page;
			part.ZoneName = "TheZone";

			var context = new CommandContext(definitions.GetDefinition(page.GetContentType()), part, Interfaces.Editing, CreatePrincipal("admin"), nullBinder, nullValidator);

			var command = CreateCommand(context);
			dispatcher.Execute(command, context);

			var pageVersions = versions.GetVersionsOf(page);
			pageVersions.Count.ShouldBeGreaterThan(0);
			pageVersions.First().State.ShouldBe(ContentState.Draft);
			pageVersions.First().Children.Single().Title.ShouldBe("New part");
		}
Esempio n. 29
0
		private void HandleResult(CommandContext ctx, params string[] redirectSequence)
		{
			if (ctx.Errors.Count > 0)
			{
				string message = string.Empty;
				foreach (var ex in ctx.Errors)
				{
					Engine.Resolve<IErrorNotifier>().Notify(ex);
					message += ex.Message + "<br/>";
				}
				FailValidation(message);
			}
			else if (ctx.ValidationErrors.Count == 0)
			{
				string redirectUrl = redirectSequence.FirstOrDefault(u => !string.IsNullOrEmpty(u));

				if (ctx.RedirectUrl != null)
					Response.Redirect(ctx.RedirectUrl.ToUrl().AppendQuery("returnUrl", redirectUrl, unlessNull: true));

				Refresh(ctx.Content, ToolbarArea.Navigation);
				Refresh(ctx.Content, redirectUrl ?? Engine.GetContentAdapter<NodeAdapter>(ctx.Content).GetPreviewUrl(ctx.Content));
			}
		}
Esempio n. 30
0
		private void ApplySortInfo(CommandContext ctx)
		{
			ctx.Parameters["MoveBefore"] = Request["before"];
			ctx.Parameters["MoveBeforeVersionKey"] = Request["beforeVersionKey"];
			ctx.Parameters["BelowVersionKey"] = Request["belowVersionKey"];
			ctx.Parameters["MoveAfter"] = Request["after"];
			ctx.Parameters["MoveBeforeSortOrder"] = Request["beforeSortOrder"];
		}
Esempio n. 31
0
 /// <summary>Saves the data specified by the provided context.</summary>
 /// <param name="context">Contains data and information used to for saving an item.</param>
 public virtual void Save(CommandContext context)
 {
     Execute(commandFactory.GetSaveCommand(context), context);
 }
Esempio n. 32
0
 /// <summary>Publishes the data specified by the provided context.</summary>
 /// <param name="context">Contains data and information used to for publishing an item.</param>
 public virtual void Publish(CommandContext context)
 {
     Execute(commandFactory.GetPublishCommand(context), context);
 }
Esempio n. 33
0
 private void HandleResult(CommandContext ctx, params string[] redirectSequence)
 {
     if (ctx.Errors.Count > 0)
     {
         string message = string.Empty;
         foreach (var ex in ctx.Errors)
         {
             Engine.Resolve<IErrorNotifier>().Notify(ex);
             message += ex.Message + "<br/>";
         }
         FailValidation(message);
     }
     else if(ctx.ValidationErrors.Count == 0)
     {
         foreach (string redirectUrl in redirectSequence)
         {
             if (!string.IsNullOrEmpty(redirectUrl))
             {
                 Refresh(ctx.Content, redirectUrl);
                 return;
             }
         }
     }
 }