Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmbShare: Remove duplicate code #226

Closed
johlju opened this issue Jun 23, 2019 · 0 comments · Fixed by #283
Closed

SmbShare: Remove duplicate code #226

johlju opened this issue Jun 23, 2019 · 0 comments · Fixed by #283
Labels
enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community.

Comments

@johlju
Copy link
Member

johlju commented Jun 23, 2019

Details of the scenario you tried and the problem that is occurring

The helper function Add-SmbShareAccessPermission is using duplicated code blocks. We should make the code simpler and reduce code management by removing the duplicated code.

Duplicated code blocks:

$currentSmbShareAccessPermissions = Get-SmbShareAccess -Name $Name

    if ($PSBoundParameters.ContainsKey('ChangeAccess'))
    {
        # Get already added account names.
        $smbShareChangeAccessObjects = $currentSmbShareAccessPermissions | Where-Object -FilterScript {
            $_.AccessControlType -eq 'Allow' `
            -and $_.AccessRight -eq 'Change'
        }

        # Get a collection of just the account names.
        $changeAccessAccountNames = @($smbShareChangeAccessObjects.AccountName)

        $newAccountsToHaveChangeAccess = $ChangeAccess | Where-Object -FilterScript {
            $_ -notin $changeAccessAccountNames
        }

        $accessRight = 'Change'

        # Add new accounts that should have change permission.
        $newAccountsToHaveChangeAccess | ForEach-Object {
            Write-Verbose -Message ($script:localizedData.GrantAccess -f $accessRight, $_, $Name)

            Grant-SmbShareAccess -Name $Name -AccountName $_ -AccessRight $accessRight -Force -ErrorAction 'Stop'
        }
    }

    if ($PSBoundParameters.ContainsKey('ReadAccess'))
    {
        # Get already added account names.
        $smbShareReadAccessObjects = $currentSmbShareAccessPermissions | Where-Object -FilterScript {
            $_.AccessControlType -eq 'Allow' `
            -and $_.AccessRight -eq 'Read'
        }

        # Get a collection of just the account names.
        $readAccessAccountNames = @($smbShareReadAccessObjects.AccountName)

        $newAccountsToHaveReadAccess = $ReadAccess | Where-Object -FilterScript {
            $_ -notin $readAccessAccountNames
        }

        $accessRight = 'Read'

        # Add new accounts that should have read permission.
        $newAccountsToHaveReadAccess | ForEach-Object {
            Write-Verbose -Message ($script:localizedData.GrantAccess -f $accessRight, $_, $Name)

            Grant-SmbShareAccess -Name $Name -AccountName $_ -AccessRight $accessRight -Force -ErrorAction 'Stop'
        }
    }

    if ($PSBoundParameters.ContainsKey('FullAccess'))
    {
        # Get already added account names.
        $smbShareFullAccessObjects = $currentSmbShareAccessPermissions | Where-Object -FilterScript {
            $_.AccessControlType -eq 'Allow' `
            -and $_.AccessRight -eq 'Full'
        }

        # Get a collection of just the account names.
        $fullAccessAccountNames = @($smbShareFullAccessObjects.AccountName)

        $newAccountsToHaveFullAccess = $FullAccess | Where-Object -FilterScript {
            $_ -notin $fullAccessAccountNames
        }

        $accessRight = 'Full'

        # Add new accounts that should have full permission.
        $newAccountsToHaveFullAccess | ForEach-Object {
            Write-Verbose -Message ($script:localizedData.GrantAccess -f $accessRight, $_, $Name)

            Grant-SmbShareAccess -Name $Name -AccountName $_ -AccessRight $accessRight -Force -ErrorAction 'Stop'
        }
    }

Verbose logs showing the problem

Not applicable

Suggested solution to the issue

Add a new helper function so each code path called the helper functions with parameters instead.

The DSC configuration that is used to reproduce the issue (as detailed as possible)

# not applicable

The operating system the target node is running

n/a

Version and build of PowerShell the target node is running

n/a

Version of the DSC module that was used ('dev' if using current dev branch)

dev

@PlagueHO PlagueHO added enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community. labels Jun 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants