PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms 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
  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
 10using System;
 11using System.IO;
 12using System.Management.Automation;
 13using System.Security.Principal;
 14using System.Text.RegularExpressions;
 15
 16namespace Web.Management.PHP.Powershell
 17{
 18
 19    public abstract class BaseCmdlet : PSCmdlet
 20    {
 21        private string _siteName;
 22        private string _virtualPath;
 23
 24        [Parameter(ValueFromPipeline = false)]
 25        public string SiteName
 26        {
 27            set
 28            {
 29                _siteName = value;
 30            }
 31            get
 32            {
 33                return _siteName;
 34            }
 35        }
 36
 37        [Parameter(ValueFromPipeline = false)]
 38        public string VirtualPath
 39        {
 40            set
 41            {
 42                _virtualPath = value;
 43            }
 44            get
 45            {
 46                return _virtualPath;
 47            }
 48        }
 49
 50        protected abstract void DoProcessing();
 51
 52        protected void EnsureAdminUser()
 53        {
 54            WindowsIdentity identity = WindowsIdentity.GetCurrent();
 55            WindowsPrincipal principal = new WindowsPrincipal(identity);
 56            SecurityIdentifier sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
 57            if (!principal.IsInRole(sidAdmin))
 58            {
 59                UnauthorizedAccessException exception = new UnauthorizedAccessException(Resources.UserIsNotAdminError);
 60                ReportTerminatingError(exception, "UnathorizedAccess", ErrorCategory.PermissionDenied);
 61            }
 62        }
 63
 64        protected static WildcardPattern PrepareWildcardPattern(string pattern)
 65        {
 66            WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled;
 67            WildcardPattern wildcard = null;
 68
 69            if (!String.IsNullOrEmpty(pattern))
 70            {
 71                wildcard = new WildcardPattern(pattern, options);
 72            }
 73            else
 74            {
 75                wildcard = new WildcardPattern("*", options);
 76            }
 77
 78            return wildcard;
 79        }
 80
 81        protected override void ProcessRecord()
 82        {
 83            EnsureAdminUser();
 84
 85            try
 86            {
 87                DoProcessing();
 88            }
 89            catch (FileNotFoundException ex)
 90            {
 91                ReportTerminatingError(ex, "FileNotFound", ErrorCategory.ObjectNotFound);
 92            }
 93            catch (InvalidOperationException ex)
 94            {
 95                ReportTerminatingError(ex, "InvalidOperation", ErrorCategory.InvalidOperation);
 96            }
 97        }
 98
 99        protected void ReportNonTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
100        {
101            ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
102            WriteError(errorRecord);
103        }
104
105        protected void ReportTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
106        {
107            ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
108            ThrowTerminatingError(errorRecord);
109        }
110    }
111}