PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/project/WebDashboard/Plugins/ServerReport/ServerUserListServerPlugin.cs

https://github.com/aravindmc/CruiseControl.NET
C# | 185 lines | 148 code | 14 blank | 23 comment | 7 complexity | 7894a08636e3c62f9fd728d9c79ef915 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using Exortech.NetReflector;
  6. using ThoughtWorks.CruiseControl.Core.Reporting.Dashboard.Navigation;
  7. using ThoughtWorks.CruiseControl.Remote.Security;
  8. using ThoughtWorks.CruiseControl.WebDashboard.Dashboard;
  9. using ThoughtWorks.CruiseControl.WebDashboard.IO;
  10. using ThoughtWorks.CruiseControl.WebDashboard.MVC;
  11. using ThoughtWorks.CruiseControl.WebDashboard.MVC.Cruise;
  12. using ThoughtWorks.CruiseControl.WebDashboard.MVC.View;
  13. using ThoughtWorks.CruiseControl.WebDashboard.ServerConnection;
  14. namespace ThoughtWorks.CruiseControl.WebDashboard.Plugins.ServerReport
  15. {
  16. /// <title>User List Server Plugin</title>
  17. /// <version>1.5</version>
  18. /// <summary>
  19. /// Displays all the users in the system, plus the security rights they have on the server.
  20. /// </summary>
  21. /// <example>
  22. /// <code title="Minimalist Example">
  23. /// &lt;serverUserListServerPlugin /&gt;
  24. /// </code>
  25. /// <code title="Full Example">
  26. /// &lt;serverUserListServerPlugin resetPassword="anewpassword" /&gt;
  27. /// </code>
  28. /// </example>
  29. /// <remarks>
  30. /// <para type="tip">
  31. /// This can be installed using the "User List" package.
  32. /// </para>
  33. /// </remarks>
  34. [ReflectorType("serverUserListServerPlugin")]
  35. public class ServerUserListServerPlugin : ICruiseAction, IPlugin
  36. {
  37. #region Private consts
  38. private const string ActionName = "ViewServerUserList";
  39. private const string DiagnosticsActionName = "ViewUserSecurityDiagnostics";
  40. #endregion
  41. #region Private fields
  42. private readonly IFarmService farmService;
  43. private readonly IVelocityViewGenerator viewGenerator;
  44. private readonly ISessionRetriever sessionRetriever;
  45. private readonly IUrlBuilder urlBuilder;
  46. private string resetPassword = "password";
  47. #endregion
  48. #region Constructors
  49. public ServerUserListServerPlugin(IFarmService farmService,
  50. IVelocityViewGenerator viewGenerator,
  51. ISessionRetriever sessionRetriever,
  52. IUrlBuilder urlBuilder)
  53. {
  54. this.farmService = farmService;
  55. this.viewGenerator = viewGenerator;
  56. this.sessionRetriever = sessionRetriever;
  57. this.urlBuilder = urlBuilder;
  58. }
  59. #endregion
  60. #region Public properties
  61. public string LinkDescription
  62. {
  63. get { return "View User List"; }
  64. }
  65. public INamedAction[] NamedActions
  66. {
  67. get
  68. {
  69. return new INamedAction[] { new ImmutableNamedAction(ActionName, this),
  70. new ImmutableNamedActionWithoutSiteTemplate(DiagnosticsActionName, this)
  71. };
  72. }
  73. }
  74. /// <summary>
  75. /// The password to use when reseting the password.
  76. /// </summary>
  77. /// <version>1.5</version>
  78. /// <default>None</default>
  79. [ReflectorProperty("resetPassword", Required = false)]
  80. public string ResetPassword
  81. {
  82. get { return resetPassword; }
  83. set { resetPassword = value; }
  84. }
  85. #endregion
  86. #region Public methods
  87. public IResponse Execute(ICruiseRequest request)
  88. {
  89. string userName = HttpContext.Current.Request.Form["user"];
  90. string action = HttpContext.Current.Request.Form["action"];
  91. if (string.IsNullOrEmpty(userName))
  92. {
  93. return GenerateUserList(request, string.Empty, string.Empty);
  94. }
  95. else
  96. {
  97. if (action == "Reset password")
  98. {
  99. string message = string.Empty;
  100. string errorMsg = string.Empty;
  101. try
  102. {
  103. farmService.ResetPassword(request.ServerName, sessionRetriever.SessionToken, userName, resetPassword);
  104. message = "The password has been reset";
  105. }
  106. catch (Exception error)
  107. {
  108. errorMsg = error.Message;
  109. }
  110. return GenerateUserList(request, message, errorMsg);
  111. }
  112. else
  113. {
  114. return GenerateUserDiagnostics(request, userName);
  115. }
  116. }
  117. }
  118. #endregion
  119. #region Private methdods
  120. private IResponse GenerateUserDiagnostics(ICruiseRequest request, string userName)
  121. {
  122. Hashtable velocityContext = new Hashtable();
  123. velocityContext["userName"] = userName;
  124. velocityContext["message"] = string.Empty;
  125. string sessionToken = request.RetrieveSessionToken(sessionRetriever);
  126. if (!string.IsNullOrEmpty(request.ProjectName))
  127. {
  128. velocityContext["projectName"] = request.ProjectName;
  129. velocityContext["diagnostics"] = farmService.DiagnoseSecurityPermissions(request.ProjectSpecifier, sessionToken, userName);
  130. }
  131. else
  132. {
  133. velocityContext["diagnostics"] = farmService.DiagnoseSecurityPermissions(request.ServerSpecifier, sessionToken, userName);
  134. }
  135. return viewGenerator.GenerateView(@"UserDiagnostics.vm", velocityContext);
  136. }
  137. private IResponse GenerateUserList(ICruiseRequest request, string message, string error)
  138. {
  139. Hashtable velocityContext = new Hashtable();
  140. velocityContext["message"] = message;
  141. velocityContext["error"] = error;
  142. var links = new List<IAbsoluteLink>();
  143. links.Add(new ServerLink(request.UrlBuilder, request.ServerSpecifier, "User List", ActionName));
  144. ProjectStatusListAndExceptions projects = farmService.GetProjectStatusListAndCaptureExceptions(request.ServerSpecifier, request.RetrieveSessionToken());
  145. foreach (ProjectStatusOnServer projectStatusOnServer in projects.StatusAndServerList)
  146. {
  147. DefaultProjectSpecifier projectSpecifier = new DefaultProjectSpecifier(projectStatusOnServer.ServerSpecifier, projectStatusOnServer.ProjectStatus.Name);
  148. links.Add(new ProjectLink(request.UrlBuilder, projectSpecifier, projectSpecifier.ProjectName, ServerUserListServerPlugin.ActionName));
  149. }
  150. velocityContext["projectLinks"] = links;
  151. string sessionToken = request.RetrieveSessionToken(sessionRetriever);
  152. List<UserDetails> allUsers = farmService.ListAllUsers(request.ServerSpecifier, sessionToken);
  153. foreach (UserDetails user in allUsers)
  154. {
  155. if (user.DisplayName == null) user.DisplayName = string.Empty;
  156. }
  157. velocityContext["users"] = allUsers;
  158. if (!string.IsNullOrEmpty(request.ProjectName))
  159. {
  160. velocityContext["currentProject"] = request.ProjectName;
  161. velocityContext["diagnosticsCall"] = new ProjectLink(request.UrlBuilder, request.ProjectSpecifier, string.Empty, DiagnosticsActionName);
  162. }
  163. else
  164. {
  165. velocityContext["diagnosticsCall"] = new ServerLink(request.UrlBuilder, request.ServerSpecifier, string.Empty, DiagnosticsActionName);
  166. }
  167. return viewGenerator.GenerateView(@"UserList.vm", velocityContext);
  168. }
  169. #endregion
  170. }
  171. }