PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/stable-1.2.0/Powershell/BaseCmdlet.cs

#
C# | 111 lines | 89 code | 14 blank | 8 comment | 2 complexity | 5ea84bf41035ed20a797648c2b9b172b MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright>
  3. // Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4. //
  5. // This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7. // </copyright>
  8. //-----------------------------------------------------------------------
  9. using System;
  10. using System.IO;
  11. using System.Management.Automation;
  12. using System.Security.Principal;
  13. using System.Text.RegularExpressions;
  14. namespace Web.Management.PHP.Powershell
  15. {
  16. public abstract class BaseCmdlet : PSCmdlet
  17. {
  18. private string _siteName;
  19. private string _virtualPath;
  20. [Parameter(ValueFromPipeline = false)]
  21. public string SiteName
  22. {
  23. set
  24. {
  25. _siteName = value;
  26. }
  27. get
  28. {
  29. return _siteName;
  30. }
  31. }
  32. [Parameter(ValueFromPipeline = false)]
  33. public string VirtualPath
  34. {
  35. set
  36. {
  37. _virtualPath = value;
  38. }
  39. get
  40. {
  41. return _virtualPath;
  42. }
  43. }
  44. protected abstract void DoProcessing();
  45. protected void EnsureAdminUser()
  46. {
  47. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  48. WindowsPrincipal principal = new WindowsPrincipal(identity);
  49. SecurityIdentifier sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
  50. if (!principal.IsInRole(sidAdmin))
  51. {
  52. UnauthorizedAccessException exception = new UnauthorizedAccessException(Resources.UserIsNotAdminError);
  53. ReportTerminatingError(exception, "UnathorizedAccess", ErrorCategory.PermissionDenied);
  54. }
  55. }
  56. protected static WildcardPattern PrepareWildcardPattern(string pattern)
  57. {
  58. WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled;
  59. WildcardPattern wildcard = null;
  60. if (!String.IsNullOrEmpty(pattern))
  61. {
  62. wildcard = new WildcardPattern(pattern, options);
  63. }
  64. else
  65. {
  66. wildcard = new WildcardPattern("*", options);
  67. }
  68. return wildcard;
  69. }
  70. protected override void ProcessRecord()
  71. {
  72. EnsureAdminUser();
  73. try
  74. {
  75. DoProcessing();
  76. }
  77. catch (FileNotFoundException ex)
  78. {
  79. ReportTerminatingError(ex, "FileNotFound", ErrorCategory.ObjectNotFound);
  80. }
  81. catch (InvalidOperationException ex)
  82. {
  83. ReportTerminatingError(ex, "InvalidOperation", ErrorCategory.InvalidOperation);
  84. }
  85. }
  86. protected void ReportNonTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
  87. {
  88. ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
  89. WriteError(errorRecord);
  90. }
  91. protected void ReportTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
  92. {
  93. ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
  94. ThrowTerminatingError(errorRecord);
  95. }
  96. }
  97. }