Skip to content

Commit 1b7dc89

Browse files
authored
Merge pull request #539 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 440aae9 + 202554e commit 1b7dc89

File tree

2 files changed

+114
-9
lines changed

2 files changed

+114
-9
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function Invoke-ExecSetUserPhoto {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Identity.User.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$Headers = $Request.Headers
13+
$tenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
14+
$userId = $Request.Query.userId ?? $Request.Body.userId
15+
$action = $Request.Query.action ?? $Request.Body.action
16+
$photoData = $Request.Body.photoData
17+
18+
$Results = [System.Collections.Generic.List[object]]::new()
19+
20+
try {
21+
if ([string]::IsNullOrWhiteSpace($userId)) {
22+
throw 'User ID is required'
23+
}
24+
25+
if ($action -eq 'remove') {
26+
# Remove the user's profile picture
27+
try {
28+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/v1.0/users/$userId/photo/`$value" -tenantid $tenantFilter -type DELETE -NoAuthCheck $true
29+
$Results.Add('Successfully removed user profile picture.')
30+
Write-LogMessage -API $APIName -tenant $tenantFilter -headers $Headers -message "Removed profile picture for user $userId" -Sev Info
31+
} catch {
32+
# Check if the error is because there's no photo
33+
if ($_.Exception.Message -like '*does not exist*' -or $_.Exception.Message -like '*ResourceNotFound*') {
34+
$Results.Add('User does not have a profile picture to remove.')
35+
Write-LogMessage -API $APIName -tenant $tenantFilter -headers $Headers -message "No profile picture found for user $userId" -Sev Info
36+
} else {
37+
throw $_
38+
}
39+
}
40+
} elseif ($action -eq 'set') {
41+
# Set the user's profile picture
42+
if ([string]::IsNullOrWhiteSpace($photoData)) {
43+
throw 'Photo data is required when setting a profile picture'
44+
}
45+
46+
# Convert base64 string to byte array
47+
# The photoData should be in format: data:image/jpeg;base64,/9j/4AAQSkZJRg...
48+
# We need to strip the data URL prefix if present
49+
$base64Data = $photoData
50+
if ($photoData -match '^data:image/[^;]+;base64,(.+)$') {
51+
$base64Data = $Matches[1]
52+
}
53+
54+
try {
55+
$photoBytes = [Convert]::FromBase64String($base64Data)
56+
} catch {
57+
throw "Invalid base64 photo data: $($_.Exception.Message)"
58+
}
59+
60+
# Validate image size (Microsoft Graph has a 4MB limit)
61+
$maxSizeBytes = 4 * 1024 * 1024 # 4MB
62+
if ($photoBytes.Length -gt $maxSizeBytes) {
63+
throw "Photo size exceeds 4MB limit. Current size: $([math]::Round($photoBytes.Length / 1MB, 2))MB"
64+
}
65+
66+
# Upload the photo using Graph API
67+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/v1.0/users/$userId/photo/`$value" -tenantid $tenantFilter -type PATCH -body $photoBytes -ContentType 'image/jpeg' -NoAuthCheck $true
68+
69+
$Results.Add('Successfully set user profile picture.')
70+
Write-LogMessage -API $APIName -tenant $tenantFilter -headers $Headers -message "Set profile picture for user $userId" -Sev Info
71+
} else {
72+
throw "Invalid action. Must be 'set' or 'remove'"
73+
}
74+
75+
return ([HttpResponseContext]@{
76+
StatusCode = [HttpStatusCode]::OK
77+
Body = @{
78+
'Results' = @($Results)
79+
}
80+
})
81+
} catch {
82+
$ErrorMessage = Get-CippException -Exception $_
83+
Write-LogMessage -API $APIName -tenant $tenantFilter -headers $Headers -message "Failed to $action user profile picture. Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage
84+
return ([HttpResponseContext]@{
85+
StatusCode = [HttpStatusCode]::BadRequest
86+
Body = @{
87+
'Results' = @("Failed to $action user profile picture: $($ErrorMessage.NormalizedError)")
88+
}
89+
})
90+
}
91+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/GDAP/Invoke-ExecGDAPRoleTemplate.ps1

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Invoke-ExecGDAPRoleTemplate {
1+
function Invoke-ExecGDAPRoleTemplate {
22
<#
33
.FUNCTIONALITY
44
Entrypoint,AnyTenant
@@ -44,19 +44,33 @@ Function Invoke-ExecGDAPRoleTemplate {
4444
}
4545
}
4646
'Edit' {
47-
$RowKey = $Request.Body.TemplateId
48-
$Template = $Templates | Where-Object -Property RowKey -EQ $RowKey
47+
# Use OriginalTemplateId if provided (for rename), otherwise use TemplateId
48+
$OriginalRowKey = $Request.Body.OriginalTemplateId ?? $Request.Body.TemplateId
49+
$NewRowKey = $Request.Body.TemplateId
50+
$Template = $Templates | Where-Object -Property RowKey -EQ $OriginalRowKey
4951
if ($Template) {
5052
$RoleMappings = $Request.Body.RoleMappings
51-
Add-CIPPGDAPRoleTemplate -TemplateId $RowKey -RoleMappings $RoleMappings -Overwrite
52-
Write-LogMessage -headers $Headers -API $APIName -message "Updated role mappings for GDAP template '$RowKey'" -Sev 'Info'
53-
$Body = @{
54-
Results = "Updated role mappings for template $RowKey"
53+
54+
# If the template ID is being changed, delete the old one and create a new one
55+
if ($OriginalRowKey -ne $NewRowKey) {
56+
Remove-AzDataTableEntity -Force @Table -Entity $Template
57+
Add-CIPPGDAPRoleTemplate -TemplateId $NewRowKey -RoleMappings $RoleMappings -Overwrite
58+
Write-LogMessage -headers $Headers -API $APIName -message "Renamed GDAP template from '$OriginalRowKey' to '$NewRowKey' and updated role mappings" -Sev 'Info'
59+
$Body = @{
60+
Results = "Renamed template from $OriginalRowKey to $NewRowKey and updated role mappings"
61+
}
62+
} else {
63+
# Just update the existing template
64+
Add-CIPPGDAPRoleTemplate -TemplateId $NewRowKey -RoleMappings $RoleMappings -Overwrite
65+
Write-LogMessage -headers $Headers -API $APIName -message "Updated role mappings for GDAP template '$NewRowKey'" -Sev 'Info'
66+
$Body = @{
67+
Results = "Updated role mappings for template $NewRowKey"
68+
}
5569
}
5670
} else {
57-
Write-LogMessage -headers $Headers -API $APIName -message "GDAP role template '$RowKey' not found for editing" -Sev 'Warning'
71+
Write-LogMessage -headers $Headers -API $APIName -message "GDAP role template '$OriginalRowKey' not found for editing" -Sev 'Warning'
5872
$Body = @{
59-
Results = "Template $RowKey not found"
73+
Results = "Template $OriginalRowKey not found"
6074
}
6175
}
6276
}

0 commit comments

Comments
 (0)