Skip to content

Commit

Permalink
Merge pull request #5 from webmd-health-services/bugfix/grant-on-file…
Browse files Browse the repository at this point in the history
…-shows-warning

Granting permissions on a file always shows a warning
  • Loading branch information
splatteredbits authored Apr 22, 2024
2 parents ec82981 + eddb1a7 commit dcb2a9c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
30 changes: 15 additions & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ from Carbon to Carbon.FileSystem, do the following:
* Replace usages of the `Test-CNtfsPermission` function's `-Exact` switch to `-Strict`.
* Using the table below, replace usages of `Grant-CNtfsPermission` and `Test-CNtfsPermission` arguments in the left
column with the new arguments from the right column.
| Old Argument | New Argument(s)
|---------------------------------------------------------|---------------------------------------------------------
| `-Permission Container` | `-Permission FolderOnly`
| `-Permission SubContainers` | `-Permission SubfoldersOnly`
| `-Permission Leaves` | `-Permission FilesOnly`
| `-Permission ChildContainers` | `-Permission SubfoldersOnly -OnlyApplyToChildFilesAndFolders`
| `-Permission ChildLeaves` | `-Permission FilesOnly -OnlyApplyToChildFilesAndFolders`
| `-Permission ContainerAndSubContainers` | `-Permission FolderAndSubfolders`
| `-Permission ContainerAndLeaves` | `-Permission FolderAndFiles`
| `-Permission SubContainerAndLeaves` | `-Permission SubfoldersAndFilesOnly`
| `-Permission ContainerAndChildContainers` | `-Permission FolderAndSubfolders -OnlyApplyToChildFilesAndFolders`
| `-Permission ContainerAndChildLeaves` | `-Permission FolderAndFiles -OnlyApplyToChildFilesAndFolders`
| `-Permission ContainerAndChildContainersAndChildLeaves` | `-Permission FolderSubfoldersAndFiles -OnlyApplyToChildFilesAndFolders`
| `-Permission ContainerAndSubContainersAndLeaves` | `-Permission FolderSubfoldersAndFiles`
| `-Permission ChildContainersAndChildLeaves` | `-Permission SubfoldersAndFilesOnly -OnlyApplyToChildFilesAndFolders`
| Old Argument | New Argument(s)
|------------------------------------------------------|---------------------------------------------------------------------
| `-ApplyTo Container` | `-ApplyTo FolderOnly`
| `-ApplyTo SubContainers` | `-ApplyTo SubfoldersOnly`
| `-ApplyTo Leaves` | `-ApplyTo FilesOnly`
| `-ApplyTo ChildContainers` | `-ApplyTo SubfoldersOnly -OnlyApplyToChildFilesAndFolders`
| `-ApplyTo ChildLeaves` | `-ApplyTo FilesOnly -OnlyApplyToChildFilesAndFolders`
| `-ApplyTo ContainerAndSubContainers` | `-ApplyTo FolderAndSubfolders`
| `-ApplyTo ContainerAndLeaves` | `-ApplyTo FolderAndFiles`
| `-ApplyTo SubContainerAndLeaves` | `-ApplyTo SubfoldersAndFilesOnly`
| `-ApplyTo ContainerAndChildContainers` | `-ApplyTo FolderAndSubfolders -OnlyApplyToChildFilesAndFolders`
| `-ApplyTo ContainerAndChildLeaves` | `-ApplyTo FolderAndFiles -OnlyApplyToChildFilesAndFolders`
| `-ApplyTo ContainerAndChildContainersAndChildLeaves` | `-ApplyTo FolderSubfoldersAndFiles -OnlyApplyToChildFilesAndFolders`
| `-ApplyTo ContainerAndSubContainersAndLeaves` | `-ApplyTo FolderSubfoldersAndFiles`
| `-ApplyTo ChildContainersAndChildLeaves` | `-ApplyTo SubfoldersAndFilesOnly -OnlyApplyToChildFilesAndFolders`

### Added

Expand Down
7 changes: 5 additions & 2 deletions Carbon.FileSystem/Functions/Grant-CNtfsPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ function Grant-CNtfsPermission
Set-StrictMode -Version 'Latest'
Use-CallerPreference -Cmdlet $PSCmdlet -Session $ExecutionContext.SessionState

if (-not $ApplyTo)
if (-not $ApplyTo -and (Test-Path -Path $Path -PathType Container))
{
$ApplyTo = 'FolderSubfoldersAndFiles'
}

$PSBoundParameters['ApplyTo'] = $ApplyTo | ConvertTo-CarbonSecurityApplyTo
if ($ApplyTo)
{
$PSBoundParameters['ApplyTo'] = $ApplyTo | ConvertTo-CarbonSecurityApplyTo
}

if ($PSBoundParameters.ContainsKey('OnlyApplyToChildFilesAndFolders'))
{
Expand Down
16 changes: 10 additions & 6 deletions Tests/Grant-CNtfsPermission.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ BeforeAll {

function Invoke-GrantPermissions
{
[CmdletBinding()]
param(
$Identity,
$Permissions,
Expand Down Expand Up @@ -175,12 +176,15 @@ Describe 'Grant-CNtfsPermission' {
$file = New-TestFile
$identity = 'BUILTIN\Administrators'
$permissions = 'Read','Write'
$warnings = @()

Invoke-GrantPermissions -Identity $identity `
-Permissions $permissions `
-Path $file `
-InheritanceFlag 'None' `
-PropagationFlag 'None'
-PropagationFlag 'None' `
-WarningVariable 'warnings'
$warnings | Should -BeNullOrEmpty
}

It 'grants permissions on directories' {
Expand Down Expand Up @@ -388,16 +392,16 @@ Describe 'Grant-CNtfsPermission' {
$item = Get-Item -Path $script:path
$item.Attributes = $item.Attributes -bor [IO.FileAttributes]::Hidden

$result = Invoke-GrantPermissions -Identity $script:user -Permission Read -Path $script:path
Invoke-GrantPermissions -Identity $script:user -Permission Read -Path $script:path
$Global:Error.Count | Should -Be 0
}

It 'fails if the path does not exist' {
$result = Grant-CNtfsPermission -Identity $script:user `
-Permission Read `
-Path 'C:\I\Do\Not\Exist' `
-PassThru `
-ErrorAction SilentlyContinue
-Permission Read `
-Path 'C:\I\Do\Not\Exist' `
-PassThru `
-ErrorAction SilentlyContinue
$result | Should -BeNullOrEmpty
$Global:Error.Count | Should -BeGreaterThan 0
$Global:Error[0] | Should -Match 'that path does not exist'
Expand Down

0 comments on commit dcb2a9c

Please sign in to comment.