/src/Orchard.Web/Modules/Orchard.PublishLater/Services/XmlRpcHandler.cs

http://vietnamicare.codeplex.com · C# · 170 lines · 145 code · 25 blank · 0 comment · 31 complexity · 98e158cf35833ad90d3a503be4c8b171 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Linq;
  4. using Orchard.ContentManagement;
  5. using Orchard.Core.Contents;
  6. using Orchard.Core.XmlRpc;
  7. using Orchard.Core.XmlRpc.Models;
  8. using Orchard.Localization;
  9. using Orchard.PublishLater.Models;
  10. using Orchard.Security;
  11. using Orchard.Tasks.Scheduling;
  12. namespace Orchard.PublishLater.Services {
  13. public class XmlRpcHandler : IXmlRpcHandler {
  14. private readonly IContentManager _contentManager;
  15. private readonly IPublishingTaskManager _publishingTaskManager;
  16. private readonly IMembershipService _membershipService;
  17. private readonly IAuthorizationService _authorizationService;
  18. public XmlRpcHandler(IContentManager contentManager,
  19. IPublishingTaskManager publishingTaskManager,
  20. IMembershipService membershipService,
  21. IAuthorizationService authorizationService) {
  22. _contentManager = contentManager;
  23. _publishingTaskManager = publishingTaskManager;
  24. _membershipService = membershipService;
  25. _authorizationService = authorizationService;
  26. T = NullLocalizer.Instance;
  27. }
  28. public Localizer T { get; set; }
  29. public void SetCapabilities(XElement options) {
  30. const string manifestUri = "http://schemas.microsoft.com/wlw/manifest/weblog";
  31. options.SetElementValue(XName.Get("supportsCustomDate", manifestUri), "Yes");
  32. }
  33. public void Process(XmlRpcContext context) {
  34. switch (context.Request.MethodName) {
  35. case "metaWeblog.newPost":
  36. MetaWeblogSetCustomPublishedDate(
  37. GetId(context.Response),
  38. Convert.ToString(context.Request.Params[0].Value),
  39. Convert.ToString(context.Request.Params[1].Value),
  40. Convert.ToString(context.Request.Params[2].Value),
  41. (XRpcStruct)context.Request.Params[3].Value,
  42. Convert.ToBoolean(context.Request.Params[4].Value),
  43. context._drivers);
  44. break;
  45. case "metaWeblog.editPost":
  46. MetaWeblogSetCustomPublishedDate(
  47. GetId(context.Response),
  48. Convert.ToString(context.Request.Params[0].Value),
  49. Convert.ToString(context.Request.Params[1].Value),
  50. Convert.ToString(context.Request.Params[2].Value),
  51. (XRpcStruct)context.Request.Params[3].Value,
  52. Convert.ToBoolean(context.Request.Params[4].Value),
  53. context._drivers);
  54. break;
  55. case "metaWeblog.getPost":
  56. MetaWeblogGetCustomPublishedDate(
  57. GetPost(context.Response),
  58. Convert.ToInt32(context.Request.Params[0].Value),
  59. Convert.ToString(context.Request.Params[1].Value),
  60. Convert.ToString(context.Request.Params[2].Value),
  61. context._drivers);
  62. break;
  63. case "metaWeblog.getRecentPosts":
  64. MetaWeblogGetCustomPublishedDate(
  65. GetPost(context.Response),
  66. Convert.ToInt32(context.Request.Params[0].Value),
  67. Convert.ToString(context.Request.Params[1].Value),
  68. Convert.ToString(context.Request.Params[2].Value),
  69. context._drivers);
  70. break;
  71. }
  72. }
  73. private void MetaWeblogGetCustomPublishedDate(XRpcStruct postStruct, int itemId, string userName, string password, ICollection<IXmlRpcDriver> drivers) {
  74. if (itemId < 1)
  75. return;
  76. var driver = new XmlRpcDriver(item => {
  77. var post = item as XRpcStruct;
  78. if (post == null)
  79. return;
  80. var postId = post.Optional<int>("postid");
  81. var contentItem = _contentManager.Get(postId, VersionOptions.Latest);
  82. if (contentItem == null)
  83. return;
  84. var publishedUtc = contentItem.As<PublishLaterPart>().ScheduledPublishUtc.Value;
  85. if (publishedUtc == null || publishedUtc <= DateTime.UtcNow)
  86. return;
  87. post.Set("dateCreated", publishedUtc);
  88. post.Set("date_created_gmt", publishedUtc);
  89. });
  90. if (postStruct != null)
  91. driver.Process(postStruct);
  92. else
  93. drivers.Add(driver);
  94. }
  95. private void MetaWeblogSetCustomPublishedDate(int contentItemId, string appKey, string userName, string password, XRpcStruct content, bool publish, ICollection<IXmlRpcDriver> drivers) {
  96. var user = ValidateUser(userName, password);
  97. if (user == null)
  98. return;
  99. var publishedUtc = content.Optional<DateTime?>("dateCreated");
  100. if (publishedUtc == null || publishedUtc <= DateTime.UtcNow) // only post-dating/scheduling of content with the PublishLaterPart
  101. return;
  102. var driver = new XmlRpcDriver(item => {
  103. if (!(item is int))
  104. return;
  105. var id = (int)item;
  106. var contentItem = _contentManager.Get(id, VersionOptions.Latest);
  107. if (contentItem == null || !contentItem.Is<PublishLaterPart>())
  108. return;
  109. _authorizationService.CheckAccess(Permissions.PublishContent, user, null);
  110. contentItem.As<PublishLaterPart>().ScheduledPublishUtc.Value = publishedUtc;
  111. _publishingTaskManager.Publish(contentItem, (DateTime)publishedUtc);
  112. });
  113. if (contentItemId > 0)
  114. driver.Process(contentItemId);
  115. else
  116. drivers.Add(driver);
  117. }
  118. private static XRpcStruct GetPost(XRpcMethodResponse response) {
  119. return response != null && response.Params.Count == 1 && response.Params[0].Value is XRpcStruct
  120. ? response.Params[0].Value as XRpcStruct
  121. : null;
  122. }
  123. private static int GetId(XRpcMethodResponse response) {
  124. return response != null && response.Params.Count == 1 && response.Params[0].Value is int
  125. ? Convert.ToInt32(response.Params[0].Value)
  126. : 0;
  127. }
  128. private IUser ValidateUser(string userName, string password) {
  129. IUser user = _membershipService.ValidateUser(userName, password);
  130. if (user == null) {
  131. throw new OrchardCoreException(T("The username or e-mail or password provided is incorrect."));
  132. }
  133. return user;
  134. }
  135. public class XmlRpcDriver : IXmlRpcDriver {
  136. private readonly Action<object> _process;
  137. public XmlRpcDriver(Action<object> process) {
  138. _process = process;
  139. }
  140. public void Process(object item) {
  141. _process(item);
  142. }
  143. }
  144. }
  145. }