Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Modules/CIPPCore/Public/Authentication/Test-CIPPAccessUserRole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ function Test-CIPPAccessUserRole {
$User
)
$Roles = @()
$Table = Get-CippTable -TableName cacheAccessUserRoles
$Filter = "PartitionKey eq 'AccessUser' and RowKey eq '$($User.userDetails)' and Timestamp ge datetime'$((Get-Date).AddMinutes(-15).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ'))'"
$UserRole = Get-CIPPAzDataTableEntity @Table -Filter $Filter

try {
$Table = Get-CippTable -TableName cacheAccessUserRoles
$Filter = "PartitionKey eq 'AccessUser' and RowKey eq '$($User.userDetails)' and Timestamp ge datetime'$((Get-Date).AddMinutes(-15).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ'))'"
$UserRole = Get-CIPPAzDataTableEntity @Table -Filter $Filter
} catch {
Write-Information "Could not access cached user roles table. $($_.Exception.Message)"
$UserRole = $null
}
if ($UserRole) {
Write-Information "Found cached user role for $($User.userDetails)"
$Roles = $UserRole.Role | ConvertFrom-Json
Expand Down Expand Up @@ -59,12 +65,16 @@ function Test-CIPPAccessUserRole {
}

if (($Roles | Measure-Object).Count -gt 2) {
$UserRole = [PSCustomObject]@{
PartitionKey = 'AccessUser'
RowKey = [string]$User.userDetails
Role = [string](ConvertTo-Json -Compress -InputObject $Roles)
try {
$UserRole = [PSCustomObject]@{
PartitionKey = 'AccessUser'
RowKey = [string]$User.userDetails
Role = [string](ConvertTo-Json -Compress -InputObject $Roles)
}
Add-CIPPAzDataTableEntity @Table -Entity $UserRole -Force
} catch {
Write-Information "Could not cache user roles for $($User.userDetails). $($_.Exception.Message)"
}
Add-CIPPAzDataTableEntity @Table -Entity $UserRole -Force
}
}
$User.userRoles = $Roles
Expand Down
37 changes: 8 additions & 29 deletions Modules/CIPPCore/Public/Clear-CippDurables.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
function Clear-CippDurables {
[CmdletBinding(SupportsShouldProcess = $true)]
Param()
param()
# Collect info
$StorageContext = New-AzStorageContext -ConnectionString $env:AzureWebJobsStorage
$FunctionName = $env:WEBSITE_SITE_NAME -replace '-', ''

# Get orchestrators
$InstancesTable = Get-CippTable -TableName ('{0}Instances' -f $FunctionName)
$HistoryTable = Get-CippTable -TableName ('{0}History' -f $FunctionName)
$QueueTable = Get-CippTable -TableName 'CippQueue'
$CippQueueTasks = Get-CippTable -TableName 'CippQueueTasks'

Remove-AzDataTable @InstancesTable
Remove-AzDataTable @HistoryTable
Remove-AzDataTable @QueueTable
Remove-AzDataTable @CippQueueTasks

$Queues = Get-AzStorageQueue -Context $StorageContext -Name ('{0}*' -f $FunctionName) | Select-Object -Property Name, ApproximateMessageCount, QueueClient

Expand All @@ -19,8 +26,6 @@ function Clear-CippDurables {
}
}

Remove-AzDataTable @InstancesTable
Remove-AzDataTable @HistoryTable
$BlobContainer = '{0}-largemessages' -f $FunctionName
if (Get-AzStorageContainer -Name $BlobContainer -Context $StorageContext -ErrorAction SilentlyContinue) {
Write-Information "- Removing blob container: $BlobContainer"
Expand All @@ -29,32 +34,6 @@ function Clear-CippDurables {
}
}

$QueueTable = Get-CippTable -TableName 'CippQueue'
$CippQueue = Invoke-ListCippQueue
$QueueEntities = foreach ($Queue in $CippQueue) {
if ($Queue.Status -eq 'Running') {
$Queue.TotalTasks = $Queue.CompletedTasks
$Queue | Select-Object -Property PartitionKey, RowKey, TotalTasks
}
}
if (($QueueEntities | Measure-Object).Count -gt 0) {
if ($PSCmdlet.ShouldProcess('Queues', 'Mark Failed')) {
Update-AzDataTableEntity -Force @QueueTable -Entity $QueueEntities
}
}

$CippQueueTasks = Get-CippTable -TableName 'CippQueueTasks'
$RunningTasks = Get-CIPPAzDataTableEntity @CippQueueTasks -Filter "PartitionKey eq 'Task' and Status eq 'Running'" -Property RowKey, PartitionKey, Status
if (($RunningTasks | Measure-Object).Count -gt 0) {
if ($PSCmdlet.ShouldProcess('Tasks', 'Mark Failed')) {
$UpdatedTasks = foreach ($Task in $RunningTasks) {
$Task.Status = 'Failed'
$Task
}
Update-AzDataTableEntity -Force @CippQueueTasks -Entity $UpdatedTasks
}
}

$null = Get-CippTable -TableName ('{0}History' -f $FunctionName)
Write-Information 'Durable Orchestrators and Queues have been cleared'
return $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function Push-ExecScheduledCommand {
$item = $Item | ConvertTo-Json -Depth 100 | ConvertFrom-Json
Write-Information "We are going to be running a scheduled task: $($Item.TaskInfo | ConvertTo-Json -Depth 10)"

$script:ScheduledTaskId = $Item.TaskInfo.RowKey

$Table = Get-CippTable -tablename 'ScheduledTasks'
$task = $Item.TaskInfo
$commandParameters = $Item.Parameters | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashtable
Expand All @@ -21,10 +23,12 @@ function Push-ExecScheduledCommand {
$CurrentTask = Get-AzDataTableEntity @Table -Filter "PartitionKey eq '$($task.PartitionKey)' and RowKey eq '$($task.RowKey)'"
if (!$CurrentTask) {
Write-Information "The task $($task.Name) for tenant $($task.Tenant) does not exist in the ScheduledTasks table. Exiting."
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
return
}
if ($CurrentTask.TaskState -eq 'Completed') {
Write-Information "The task $($task.Name) for tenant $($task.Tenant) is already completed. Skipping execution."
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
return
}

Expand Down Expand Up @@ -69,6 +73,7 @@ function Push-ExecScheduledCommand {
TaskState = 'Planned'
ScheduledTime = [string]$nextRunUnixTime
}
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
return
}
}
Expand All @@ -94,6 +99,7 @@ function Push-ExecScheduledCommand {
}

Write-LogMessage -API 'Scheduler_UserTasks' -tenant $Tenant -tenantid $TenantInfo.customerId -message "Failed to execute task $($task.Name): The command $($Item.Command) does not exist." -sev Error
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
return
}

Expand Down Expand Up @@ -330,4 +336,5 @@ function Push-ExecScheduledCommand {
if ($TaskType -ne 'Alert') {
Write-LogMessage -API 'Scheduler_UserTasks' -tenant $Tenant -tenantid $TenantInfo.customerId -message "Successfully executed task: $($task.Name)" -sev Info
}
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ function Push-CIPPStandard {
Write-Warning "Error running standard $($Standard) for tenant $($Tenant) - $($_.Exception.Message)"
Write-Information $_.InvocationInfo.PositionMessage
throw $_.Exception.Message
} finally {
Remove-Variable -Name StandardInfo -Scope Script -ErrorAction SilentlyContinue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function Invoke-ExecEditTemplate {
$Template = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'IntuneTemplate' and RowKey eq '$GUID'"
$OriginalJSON = $Template.JSON

$TemplateData = $Template.JSON | ConvertFrom-Json
$TemplateType = $TemplateData.Type

if ($Template.SHA) {
$NewGuid = [guid]::NewGuid().ToString()
} else {
Expand All @@ -36,7 +39,7 @@ function Invoke-ExecEditTemplate {
RawJson = $RawJSON
DisplayName = $Request.Body.displayName
Description = $Request.Body.description
templateType = $Template.Type
templateType = $TemplateType
Package = $Template.Package
Headers = $Request.Headers
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function Invoke-ExecApiClient {
if (!$Client) {
$Results = @{
resultText = 'API client not found'
severity = 'error'
state = 'error'
}
} else {
$ApiConfig = New-CIPPAPIConfig -ResetSecret -AppId $Request.Body.ClientId -Headers $Request.Headers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Invoke-ExecUpdateRefreshToken {
function Invoke-ExecUpdateRefreshToken {
<#
.FUNCTIONALITY
Entrypoint,AnyTenant
Expand Down Expand Up @@ -49,16 +49,21 @@ Function Invoke-ExecUpdateRefreshToken {
$TenantName = $request.body.tenantId
}
$Results = @{
'message' = "Successfully updated the credentials for $($TenantName). You may continue to the next step, or add additional tenants if required."
'severity' = 'success'
'resultText' = "Successfully updated the credentials for $($TenantName). You may continue to the next step, or add additional tenants if required."
'state' = 'success'
}
} catch {
$Results = [pscustomobject]@{'Results' = "Failed. $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.message)"; severity = 'failed' }
}
$Results = [pscustomobject]@{
'Results' = @{
resultText = "Failed. $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.message)"
state = 'failed'
}
}

return ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $Results
})
return ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $Results
})

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function Invoke-ExecCopyForSent {
function Invoke-ExecCopyForSent {
<#
.FUNCTIONALITY
Entrypoint
Expand All @@ -13,13 +13,21 @@ Function Invoke-ExecCopyForSent {


# Interact with query parameters or the body of the request.
$TenantFilter = $Request.Query.TenantFilter ?? $Request.Body.TenantFilter
$TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
$UserID = $Request.Query.ID ?? $Request.Body.ID
$MessageCopyForSentAsEnabled = $Request.Query.MessageCopyForSentAsEnabled ?? $Request.Body.MessageCopyForSentAsEnabled
$MessageCopyForSentAsEnabled = [System.Convert]::ToBoolean($MessageCopyForSentAsEnabled)
$MessageCopyState = $Request.Query.messageCopyState ?? $Request.Body.messageCopyState
$MessageCopyState = [System.Convert]::ToBoolean($MessageCopyState)

Try {
$Result = Set-CIPPMessageCopy -userid $UserID -tenantFilter $TenantFilter -APIName $APIName -Headers $Headers -MessageCopyForSentAsEnabled $MessageCopyForSentAsEnabled
try {
$params = @{
UserId = $UserID
TenantFilter = $TenantFilter
APIName = $APIName
Headers = $Headers
MessageCopyForSentAsEnabled = $MessageCopyState
MessageCopyForSendOnBehalfEnabled = $MessageCopyState
}
$Result = Set-CIPPMessageCopy @params
$StatusCode = [HttpStatusCode]::OK
} catch {
$Result = "$($_.Exception.Message)"
Expand Down
Loading