This repository was archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBaseCmdlet.cs
More file actions
87 lines (74 loc) · 2.92 KB
/
BaseCmdlet.cs
File metadata and controls
87 lines (74 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//-----------------------------------------------------------------------
// <copyright>
// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
//
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.IO;
using System.Management.Automation;
using System.Security.Principal;
namespace Web.Management.PHP.Powershell
{
public abstract class BaseCmdlet : PSCmdlet
{
[Parameter(ValueFromPipeline = false)]
public string SiteName { get; set; }
[Parameter(ValueFromPipeline = false)]
public string VirtualPath { get; set; }
protected abstract void DoProcessing();
protected void EnsureAdminUser()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
var sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
if (!principal.IsInRole(sidAdmin))
{
var exception = new UnauthorizedAccessException(Resources.UserIsNotAdminError);
ReportTerminatingError(exception, "UnathorizedAccess", ErrorCategory.PermissionDenied);
}
}
protected static WildcardPattern PrepareWildcardPattern(string pattern)
{
const WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.Compiled;
WildcardPattern wildcard;
if (!String.IsNullOrEmpty(pattern))
{
wildcard = new WildcardPattern(pattern, options);
}
else
{
wildcard = new WildcardPattern("*", options);
}
return wildcard;
}
protected override void ProcessRecord()
{
EnsureAdminUser();
try
{
DoProcessing();
}
catch (FileNotFoundException ex)
{
ReportTerminatingError(ex, "FileNotFound", ErrorCategory.ObjectNotFound);
}
catch (InvalidOperationException ex)
{
ReportTerminatingError(ex, "InvalidOperation", ErrorCategory.InvalidOperation);
}
}
protected void ReportNonTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
{
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
WriteError(errorRecord);
}
protected void ReportTerminatingError(Exception exception, string errorId, ErrorCategory errorCategory)
{
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, null);
ThrowTerminatingError(errorRecord);
}
}
}