7

I'm creating a Context Menu script to allow deletion of languages on my content tree. When the user elects to run this script, I want to prompt them for their password as a confirmation mechanism, similar to the "elevate" mechanic. Is this possible, and how would it be achieved? These users are not administrators, just general content authors.

Here's the gist of my script:

$location = get-location

$languages = Get-ChildItem "master:\sitecore\system\Languages"
$currentLanguage = [Sitecore.Context]::Language.Name

$langOptions = @{};

foreach ($lang in $languages) {
    $langOptions[$lang.Name] = $lang.Name
}

$result = Read-Variable -Parameters `
    @{ Name = "language"; Value=$currentLanguage; Title="Language to Remove"; Options=$langOptions; },
    @{ Name = "includeSubitems"; Value=$false; Title="Include Subitems"; Columns = 4;} `
    -Description "Select the language to remove" `
    -Title "Remove Language" -Width 650 -Height 360 -OkButtonName "Proceed" -CancelButtonName "Cancel" -ShowHints

if($result -ne "ok") {
    Exit
}

$items = @()

$items += Get-Item $location

# add optional subitems
if ($includeSubitems) {
    $items += Get-ChildItem $location -Recurse
}

$proceed = Show-Confirm -Title "Are you sure?"

# Prompt user here for their password to verify execution

if ($proceed -ne 'yes') {
    Write-Host "Canceling"
    Exit
}

$items | Remove-ItemLanguage -Language $language

2 Answers 2

7

In SPE there is a special password field.

enter image description here

You can find this example here: /sitecore/system/Modules/PowerShell/Script Library/Getting Started/User Interaction/Read-Variable - Sample

After you get credentials from the user then you can validate whether credentials are valid using ValidateUser method from Sitecore.Security.Authentication.AuthenticationHelper (Remember about domain name)

$obj = New-Object -TypeName Sitecore.Security.Authentication.AuthenticationHelper
$valid = $obj.ValidateUser("sitecore\admin","b")
5

Another way is with the Read-Host commandlet. If you ask it to return a SecureString, it will show a password field (as a modal dialog in Sitecore Powershell). You then have to convert the so-called secure string back into a normal string to validate credentials:

(Update: use NetworkCredential class to get clear password.)

$username = [Sitecore.Context]::User.Name
$securePass = Read-Host -Prompt 'Please enter your password' -AsSecureString
$password = (New-Object System.Net.NetworkCredential "", $securePass).Password
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

One issue with this method is you can't control the dialog's title. But it's a quick fix.

Update: yet another (IMO better) way using Read-Variable, which sets a regular string and provides more control:

$username = [Sitecore.Context]::User.Name
$password = '' # Clear password var so there's no initial value
Read-Variable -Parameters @{Name="password"; Title="Password"; Editor="Password"} `
    -Description 'Please enter your password to confirm' `
    -Title 'Confirmation' | Out-Null
$isValid = [System.Web.Security.Membership]::ValidateUser($username, $password)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.