diff --git a/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs b/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs index 8f32a906a..c9c4959e7 100644 --- a/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs +++ b/src/Orchard.Tests.Modules/CodeGeneration/Commands/CodeGenerationCommandsTests.cs @@ -58,16 +58,14 @@ namespace Orchard.Tests.Modules.CodeGeneration.Commands { } [Test] + [ExpectedException(typeof(OrchardException))] public void CreateDataMigrationTestNonExistentFeature() { CodeGenerationCommands codeGenerationCommands = new CodeGenerationCommands(_extensionManager, _schemaCommandGenerator); TextWriter textWriterOutput = new StringWriter(); codeGenerationCommands.Context = new CommandContext { Output = textWriterOutput }; - bool result = codeGenerationCommands.CreateDataMigration("feature"); - - Assert.That(result, Is.False); - Assert.That(textWriterOutput.ToString(), Is.EqualTo("Creating Data Migration for feature\r\nCreating data migration failed: target Feature feature could not be found.\r\n")); + codeGenerationCommands.CreateDataMigration("feature"); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Scheduling/OwnedScheduledTaskHandler.cs b/src/Orchard.Web/Core/Common/Scheduling/OwnedScheduledTaskHandler.cs new file mode 100644 index 000000000..5577ac963 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Scheduling/OwnedScheduledTaskHandler.cs @@ -0,0 +1,30 @@ +using System; +using Orchard.ContentManagement; +using Orchard.Core.Common.Models; +using Orchard.Security; +using Orchard.Tasks.Scheduling; + +namespace Orchard.Core.Common.Scheduling { + public abstract class OwnedScheduledTaskHandler : IScheduledTaskHandler { + private readonly IOrchardServices _orchardServices; + + protected OwnedScheduledTaskHandler(IOrchardServices orchardServices) { + _orchardServices = orchardServices; + } + + protected void SetCurrentUser(ContentItem contentItem) { + IUser owner = null; + var commonPart = contentItem.As(); + if (commonPart != null) { + owner = commonPart.Owner; + } + if (owner == null) { + var superUser = _orchardServices.WorkContext.CurrentSite.SuperUser; + owner = _orchardServices.WorkContext.Resolve().GetUser(superUser); + } + _orchardServices.WorkContext.Resolve().SetAuthenticatedUserForRequest(owner); + } + + public abstract void Process(ScheduledTaskContext context); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index e63beeb36..c06905952 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -227,6 +227,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.ArchiveLater/Handlers/UnpublishingTaskHandler.cs b/src/Orchard.Web/Modules/Orchard.ArchiveLater/Handlers/UnpublishingTaskHandler.cs index 48c2dfd45..28e955f6d 100644 --- a/src/Orchard.Web/Modules/Orchard.ArchiveLater/Handlers/UnpublishingTaskHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.ArchiveLater/Handlers/UnpublishingTaskHandler.cs @@ -1,27 +1,29 @@ using JetBrains.Annotations; using Orchard.ContentManagement; +using Orchard.Core.Common.Scheduling; using Orchard.Logging; using Orchard.Tasks.Scheduling; namespace Orchard.ArchiveLater.Handlers { [UsedImplicitly] - public class UnpublishingTaskHandler : IScheduledTaskHandler { + public class UnpublishingTaskHandler : OwnedScheduledTaskHandler { private readonly IContentManager _contentManager; - public UnpublishingTaskHandler(IContentManager contentManager) { + public UnpublishingTaskHandler(IContentManager contentManager, IOrchardServices orchardServices) : base(orchardServices) { _contentManager = contentManager; Logger = NullLogger.Instance; } public ILogger Logger { get; set; } - public void Process(ScheduledTaskContext context) { + public override void Process(ScheduledTaskContext context) { if (context.Task.TaskType == "Unpublish") { Logger.Information("Unpublishing item #{0} version {1} scheduled at {2} utc", context.Task.ContentItem.Id, context.Task.ContentItem.Version, context.Task.ScheduledUtc); + SetCurrentUser(context.Task.ContentItem); _contentManager.Unpublish(context.Task.ContentItem); } } diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Commands/BlogCommands.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Commands/BlogCommands.cs index 0940393d8..8164f42c9 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Commands/BlogCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Commands/BlogCommands.cs @@ -53,12 +53,11 @@ namespace Orchard.Blogs.Commands { var owner = _membershipService.GetUser(Owner); if ( owner == null ) { - Context.Output.WriteLine(); - return T("Invalid username: {0}", Owner).Text; + throw new OrchardException(T("Invalid username: {0}", Owner)); } if(!IsSlugValid(Slug)) { - return "Invalid Slug provided. Blog creation failed."; + throw new OrchardException(T("Invalid Slug provided. Blog creation failed.")); } var blog = _contentManager.New("Blog"); @@ -83,8 +82,7 @@ namespace Orchard.Blogs.Commands { var owner = _membershipService.GetUser(Owner); if(owner == null) { - Context.Output.WriteLine(); - return T("Invalid username: {0}", Owner).Text; + throw new OrchardException(T("Invalid username: {0}", Owner)); } XDocument doc; @@ -95,14 +93,13 @@ namespace Orchard.Blogs.Commands { Context.Output.WriteLine("Found {0} items", doc.Descendants("item").Count()); } catch ( Exception ex ) { - Context.Output.WriteLine(T("An error occured while loading the file: " + ex.Message)); - return "Import terminated."; + throw new OrchardException(T("An error occured while loading the file: {0}", ex.Message)); } var blog = _blogService.Get(Slug); if ( blog == null ) { - return "Blog not found at specified slug: " + Slug; + throw new OrchardException(T("Blog not found at specified slug: {0}", Slug)); } foreach ( var item in doc.Descendants("item") ) { @@ -122,7 +119,6 @@ namespace Orchard.Blogs.Commands { } } - return "Import feed completed."; } diff --git a/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs b/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs index 2a43f4c05..b0c561845 100644 --- a/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.CodeGeneration/Commands/CodeGenerationCommands.cs @@ -48,15 +48,14 @@ namespace Orchard.CodeGeneration.Commands { [CommandHelp("codegen datamigration \r\n\t" + "Create a new Data Migration class")] [CommandName("codegen datamigration")] - public bool CreateDataMigration(string featureName) { + public void CreateDataMigration(string featureName) { Context.Output.WriteLine(T("Creating Data Migration for {0}", featureName)); ExtensionDescriptor extensionDescriptor = _extensionManager.AvailableExtensions().FirstOrDefault(extension => DefaultExtensionTypes.IsModule(extension.ExtensionType) && extension.Features.Any(feature => String.Equals(feature.Id, featureName, StringComparison.OrdinalIgnoreCase))); if (extensionDescriptor == null) { - Context.Output.WriteLine(T("Creating data migration failed: target Feature {0} could not be found.", featureName)); - return false; + throw new OrchardException(T("Creating data migration failed: target Feature {0} could not be found.", featureName)); } string dataMigrationFolderPath = HostingEnvironment.MapPath("~/Modules/" + extensionDescriptor.Id + "/"); @@ -69,8 +68,7 @@ namespace Orchard.CodeGeneration.Commands { } if (File.Exists(dataMigrationFilePath)) { - Context.Output.WriteLine(T("Data migration already exists in target Module {0}.", extensionDescriptor.Id)); - return false; + throw new OrchardException(T("Data migration already exists in target Module {0}.", extensionDescriptor.Id)); } List commands = _schemaCommandGenerator.GetCreateFeatureCommands(featureName, false).ToList(); @@ -104,25 +102,20 @@ namespace Orchard.CodeGeneration.Commands { File.WriteAllText(moduleCsProjPath, projectFileText); TouchSolution(Context.Output); Context.Output.WriteLine(T("Data migration created successfully in Module {0}", extensionDescriptor.Id)); - - return true; } [CommandHelp("codegen module [/IncludeInSolution:true|false]\r\n\t" + "Create a new Orchard module")] [CommandName("codegen module")] [OrchardSwitches("IncludeInSolution")] - public bool CreateModule(string moduleName) { + public void CreateModule(string moduleName) { Context.Output.WriteLine(T("Creating Module {0}", moduleName)); if ( _extensionManager.AvailableExtensions().Any(extension => String.Equals(moduleName, extension.Name, StringComparison.OrdinalIgnoreCase)) ) { - Context.Output.WriteLine(T("Creating Module {0} failed: a module of the same name already exists", moduleName)); - return false; + throw new OrchardException(T("Creating Module {0} failed: a module of the same name already exists", moduleName)); } IntegrateModule(moduleName); Context.Output.WriteLine(T("Module {0} created successfully", moduleName)); - - return true; } [CommandName("codegen theme")] @@ -131,20 +124,18 @@ namespace Orchard.CodeGeneration.Commands { public void CreateTheme(string themeName) { Context.Output.WriteLine(T("Creating Theme {0}", themeName)); if (_extensionManager.AvailableExtensions().Any(extension => String.Equals(themeName, extension.Id, StringComparison.OrdinalIgnoreCase))) { - Context.Output.WriteLine(T("Creating Theme {0} failed: an extention of the same name already exists", themeName)); + throw new OrchardException(T("Creating Theme {0} failed: an extention of the same name already exists", themeName)); } - else { - if (!string.IsNullOrEmpty(BasedOn)) { - if (!_extensionManager.AvailableExtensions().Any(extension => - string.Equals(extension.ExtensionType, DefaultExtensionTypes.Theme, StringComparison.OrdinalIgnoreCase) && - string.Equals(BasedOn, extension.Id, StringComparison.OrdinalIgnoreCase))) { - Context.Output.WriteLine(T("Creating Theme {0} failed: base theme named {1} was not found.", themeName, BasedOn)); - return; - } + + if (!string.IsNullOrEmpty(BasedOn)) { + if (!_extensionManager.AvailableExtensions().Any(extension => + string.Equals(extension.ExtensionType, DefaultExtensionTypes.Theme, StringComparison.OrdinalIgnoreCase) && + string.Equals(BasedOn, extension.Id, StringComparison.OrdinalIgnoreCase))) { + throw new OrchardException(T("Creating Theme {0} failed: base theme named {1} was not found.", themeName, BasedOn)); } - IntegrateTheme(themeName, BasedOn); - Context.Output.WriteLine(T("Theme {0} created successfully", themeName)); } + IntegrateTheme(themeName, BasedOn); + Context.Output.WriteLine(T("Theme {0} created successfully", themeName)); } [CommandHelp("codegen controller \r\n\t" + "Create a new Orchard controller in a module")] @@ -156,8 +147,7 @@ namespace Orchard.CodeGeneration.Commands { string.Equals(moduleName, extension.Name, StringComparison.OrdinalIgnoreCase)); if (extensionDescriptor == null) { - Context.Output.WriteLine(T("Creating Controller {0} failed: target Module {1} could not be found.", controllerName, moduleName)); - return; + throw new OrchardException(T("Creating Controller {0} failed: target Module {1} could not be found.", controllerName, moduleName)); } string moduleControllersPath = HostingEnvironment.MapPath("~/Modules/" + extensionDescriptor.Id + "/Controllers/"); @@ -169,8 +159,7 @@ namespace Orchard.CodeGeneration.Commands { Directory.CreateDirectory(moduleControllersPath); } if (File.Exists(controllerPath)) { - Context.Output.WriteLine(T("Controller {0} already exists in target Module {1}.", controllerName, moduleName)); - return; + throw new OrchardException(T("Controller {0} already exists in target Module {1}.", controllerName, moduleName)); } string controllerText = File.ReadAllText(templatesPath + "Controller.txt"); diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Permissions.cs b/src/Orchard.Web/Modules/Orchard.Comments/Permissions.cs index 67500ac71..6368d4d34 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/Permissions.cs +++ b/src/Orchard.Web/Modules/Orchard.Comments/Permissions.cs @@ -26,13 +26,17 @@ namespace Orchard.Comments { Name = "Anonymous", Permissions = new[] {AddComment} }, + new PermissionStereotype { + Name = "Authenticated", + Permissions = new[] {AddComment} + }, new PermissionStereotype { Name = "Editor", Permissions = new[] {AddComment} }, new PermissionStereotype { Name = "Moderator", - Permissions = new[] {AddComment} + Permissions = new[] {ManageComments, AddComment} }, new PermissionStereotype { Name = "Author", diff --git a/src/Orchard.Web/Modules/Orchard.Experimental/Commands/ProfilingCommands.cs b/src/Orchard.Web/Modules/Orchard.Experimental/Commands/ProfilingCommands.cs index f2729e26f..abd61336d 100644 --- a/src/Orchard.Web/Modules/Orchard.Experimental/Commands/ProfilingCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.Experimental/Commands/ProfilingCommands.cs @@ -69,7 +69,7 @@ namespace Orchard.Experimental.Commands { for (int i = 0; i < 1000; i++) { var user = _membershipService.CreateUser(new CreateUserParams("user" + i, "1234567", "user" + i + "@orchardproject.net", null, null, true)); if (user == null) - return "The authentication provider returned an error"; + throw new OrchardException(T("The authentication provider returned an error")); } return "Success"; diff --git a/src/Orchard.Web/Modules/Orchard.Indexing/Commands/IndexingCommands.cs b/src/Orchard.Web/Modules/Orchard.Indexing/Commands/IndexingCommands.cs index d3295b516..e87d5e983 100644 --- a/src/Orchard.Web/Modules/Orchard.Indexing/Commands/IndexingCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.Indexing/Commands/IndexingCommands.cs @@ -38,7 +38,7 @@ namespace Orchard.Indexing.Commands { [OrchardSwitches("IndexName")] public string Update() { if ( !_indexManager.HasIndexProvider() ) { - return "No index available"; + throw new OrchardException(T("No index available")); } var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName; @@ -54,7 +54,7 @@ namespace Orchard.Indexing.Commands { [OrchardSwitches("IndexName")] public string Rebuild() { if ( !_indexManager.HasIndexProvider() ) { - return "No index available"; + throw new OrchardException(T("No index available")); } var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName; @@ -71,7 +71,7 @@ namespace Orchard.Indexing.Commands { [OrchardSwitches("Query,IndexName")] public string Search() { if ( !_indexManager.HasIndexProvider() ) { - return "No index available"; + throw new OrchardException(T("No index available")); } var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName; var searchBuilder = _indexManager.GetSearchIndexProvider().CreateSearchBuilder(indexName); @@ -99,7 +99,7 @@ namespace Orchard.Indexing.Commands { [OrchardSwitches("IndexName")] public string Stats() { if ( !_indexManager.HasIndexProvider() ) { - return "No index available"; + throw new OrchardException(T("No index available")); } var indexName = String.IsNullOrWhiteSpace(IndexName) ? SearchIndexName : IndexName; Context.Output.WriteLine("Number of indexed documents: {0}", _indexManager.GetSearchIndexProvider().NumDocs(indexName)); @@ -112,7 +112,7 @@ namespace Orchard.Indexing.Commands { public string Refresh() { int contentItemId; if ( !int.TryParse(ContentItemId, out contentItemId) ) { - return "Invalid content item id. Not an integer."; + throw new OrchardException(T("Invalid content item id. Not an integer.")); } var contentItem = _contentManager.Get(contentItemId); @@ -127,7 +127,7 @@ namespace Orchard.Indexing.Commands { public string Delete() { int contenItemId; if(!int.TryParse(ContentItemId, out contenItemId)) { - return "Invalid content item id. Not an integer."; + throw new OrchardException(T("Invalid content item id. Not an integer.")); } var contentItem = _contentManager.Get(contenItemId); diff --git a/src/Orchard.Web/Modules/Orchard.Media/Views/Admin/Index.cshtml b/src/Orchard.Web/Modules/Orchard.Media/Views/Admin/Index.cshtml index 564bd58ff..3342410bb 100644 --- a/src/Orchard.Web/Modules/Orchard.Media/Views/Admin/Index.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Media/Views/Admin/Index.cshtml @@ -3,7 +3,7 @@

@Html.TitleForPage(T("Manage Media Folders").ToString())

@Html.ValidationSummary() - + @using (Html.BeginFormAntiForgeryPost()) {