/source/app/NOS.Registration/AutoRegistrationPlugin.cs

http://github.com/agross/netopenspace · C# · 231 lines · 199 code · 32 blank · 0 comment · 10 complexity · 6488116207c69bccce6a4ce57559606c MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using NOS.Registration.EntryPositioning;
  4. using ScrewTurn.Wiki.PluginFramework;
  5. namespace NOS.Registration
  6. {
  7. public class AutoRegistrationPlugin : IFormatterProviderV30
  8. {
  9. readonly IPluginConfiguration _configuration;
  10. readonly IEntryFormatter _entryFormatter;
  11. readonly IFileReader _fileReader;
  12. readonly ILogger _logger;
  13. readonly INotificationSender _notificationSender;
  14. readonly IPageFormatter _pageFormatter;
  15. readonly IPageRepository _pageRepository;
  16. readonly IRegistrationRepository _registrationRepository;
  17. readonly ISettings _settings;
  18. readonly ISynchronizer _synchronizer;
  19. IHostV30 _host;
  20. public AutoRegistrationPlugin()
  21. : this(new CrossContextSynchronizer(),
  22. new RegistrationRepository(),
  23. new PageRepository(),
  24. new PageFormatter(new DefaultLogger(),
  25. new DefaultOpinionEvaluator(
  26. new IHasOpinionAboutEntryPosition[]
  27. {
  28. new AtListEnd(),
  29. new AtListEndWhenSponsoring(),
  30. new AtWaitingListEndWhenHardLimitReached()
  31. })),
  32. new NVelocityEntryFormatter(),
  33. new EmailNotificationSender(),
  34. new DefaultLogger(),
  35. new DefaultPluginConfiguration(),
  36. new DefaultFileReader(),
  37. new WikiSettings())
  38. {
  39. }
  40. public AutoRegistrationPlugin(ISynchronizer synchronizer,
  41. IRegistrationRepository registrationRepository,
  42. IPageRepository pageRepository,
  43. IPageFormatter pageFormatter,
  44. IEntryFormatter entryFormatter,
  45. INotificationSender notificationSender,
  46. ILogger logger,
  47. IPluginConfiguration configuration,
  48. IFileReader fileReader,
  49. ISettings settings)
  50. {
  51. _synchronizer = synchronizer;
  52. _fileReader = fileReader;
  53. _settings = settings;
  54. _registrationRepository = registrationRepository;
  55. _pageRepository = pageRepository;
  56. _pageFormatter = pageFormatter;
  57. _entryFormatter = entryFormatter;
  58. _notificationSender = notificationSender;
  59. _logger = logger;
  60. _configuration = configuration;
  61. }
  62. #region IFormatterProvider Members
  63. public void Init(IHostV30 host, string config)
  64. {
  65. _host = host;
  66. if (Configure(config))
  67. {
  68. _host.UserAccountActivity += Host_UserAccountActivity;
  69. _notificationSender.Configure(_host, _settings);
  70. _logger.Info(String.Format("Waiting list is enabled after {0} attendees with a hard limit of {1}.",
  71. _configuration.MaximumAttendees,
  72. _configuration.HardLimit),
  73. "SYSTEM");
  74. }
  75. else
  76. {
  77. _logger.Error("The auto registration plugin will be disabled.", "SYSTEM");
  78. }
  79. }
  80. public void Shutdown()
  81. {
  82. }
  83. public ComponentInformation Information
  84. {
  85. get { return new ComponentInformation(GetType().Name, "Alexander Groß", "1.1", "http://therightstuff.de", null); }
  86. }
  87. public string ConfigHelpHtml
  88. {
  89. get { return String.Empty; }
  90. }
  91. public string Format(string raw, ContextInformation context, FormattingPhase phase)
  92. {
  93. return raw;
  94. }
  95. public string PrepareTitle(string title, ContextInformation context)
  96. {
  97. return title;
  98. }
  99. public bool PerformPhase1
  100. {
  101. get { return false; }
  102. }
  103. public bool PerformPhase2
  104. {
  105. get { return false; }
  106. }
  107. public bool PerformPhase3
  108. {
  109. get { return false; }
  110. }
  111. public int ExecutionPriority
  112. {
  113. get { return 100; }
  114. }
  115. #endregion
  116. bool Configure(string config)
  117. {
  118. var errors = _configuration.Parse(config ?? String.Empty, _pageRepository);
  119. errors.Each(x => _logger.Error(x, "SYSTEM"));
  120. return !errors.Any();
  121. }
  122. void Host_UserAccountActivity(object sender, UserAccountActivityEventArgs e)
  123. {
  124. if (e.Activity != UserAccountActivity.AccountActivated)
  125. {
  126. return;
  127. }
  128. _synchronizer.Lock(() =>
  129. {
  130. var failed = false;
  131. User user = null;
  132. try
  133. {
  134. user = _registrationRepository.FindByUserName(e.User.Username);
  135. if (user == null)
  136. {
  137. return;
  138. }
  139. var pageInfo = _pageRepository.FindPage(_configuration.PageName);
  140. if (pageInfo == null)
  141. {
  142. _logger.Error(String.Format("The attendee page '{0}' does not exist.", _configuration.PageName), "SYSTEM");
  143. throw new Exception("Attendee page does not exist.");
  144. }
  145. PageContent pageContent;
  146. try
  147. {
  148. pageContent = _host.GetPageContent(pageInfo);
  149. }
  150. catch (Exception ex)
  151. {
  152. _logger.Error(String.Format("The attendee page's content ('{0}') could not be loaded: {1}", _configuration.PageName, ex),
  153. "SYSTEM");
  154. throw;
  155. }
  156. try
  157. {
  158. string entry = _entryFormatter.FormatUserEntry(user, _settings, _configuration.EntryTemplate);
  159. string newContent = _pageFormatter.AddEntry(pageContent.Content, entry, user, _configuration);
  160. _pageRepository.Save(pageInfo, pageContent.Title, user.UserName, _configuration.Comment, newContent);
  161. _logger.Info("User entry written successfully", user.UserName);
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.Error(String.Format("Could not add the user's entry to the attendee list: {0}", ex), "SYSTEM");
  166. throw;
  167. }
  168. }
  169. catch
  170. {
  171. failed = true;
  172. }
  173. finally
  174. {
  175. if (user != null)
  176. {
  177. string message = LoadEmailTemplate(failed);
  178. message = FillTemplate(message, user);
  179. _notificationSender.SendMessage(e.User.Email, _configuration.Comment, message);
  180. _notificationSender.SendMessage(_settings.ContactEmail, _configuration.Comment, message);
  181. }
  182. }
  183. });
  184. }
  185. string LoadEmailTemplate(bool failed)
  186. {
  187. string file = typeof(AutoRegistrationPlugin).FullName + ".SuccessMessage";
  188. if (failed)
  189. {
  190. file = typeof(AutoRegistrationPlugin).FullName + ".FailureMessage";
  191. }
  192. return _fileReader.Read(file);
  193. }
  194. string FillTemplate(string template, User user)
  195. {
  196. return _entryFormatter.FormatUserEntry(user, _settings, template);
  197. }
  198. }
  199. }