Skip to content

Commit 110f048

Browse files
authored
Merge pull request KelvinTegelaar#1614 from Zacgoose/teams-phone
FEAT: Teams phone DID removal endpoint
2 parents c42c185 + 3f207b3 commit 110f048

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

CIPP-Permissions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@
425425
"Name": "UserAuthenticationMethod.ReadWrite",
426426
"Description": "Allows the app to read and write your authentication methods, including phone numbers and Authenticator app settings.This does not allow the app to see secret information like your passwords, or to sign-in or otherwise use your authentication methods."
427427
},
428+
{
429+
"Id": "424b07a8-1209-4d17-9fe4-9018a93a1024",
430+
"Name": "TeamsTelephoneNumber.ReadWrite.All",
431+
"Description": "Allows the app to read and modify your tenant's acquired telephone number details on behalf of the signed-in admin user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc."
432+
},
428433
{
429434
"Id": "b7887744-6746-4312-813d-72daeaee7e2d",
430435
"Name": "UserAuthenticationMethod.ReadWrite.All",
@@ -697,6 +702,11 @@
697702
"Name": "User.ReadWrite.All",
698703
"Description": "Allows the app to read and update user profiles without a signed in user."
699704
},
705+
{
706+
"Id": "0a42382f-155c-4eb1-9bdc-21548ccaa387",
707+
"Name": "TeamsTelephoneNumber.ReadWrite.All",
708+
"Description": "Allows the app to read your tenant's acquired telephone number details, without a signed-in user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc."
709+
},
700710
{
701711
"Id": "50483e42-d915-4231-9639-7fdb7fd190e5",
702712
"Name": "UserAuthenticationMethod.ReadWrite.All",

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Users/Invoke-CIPPOffboardingJob.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ function Invoke-CIPPOffboardingJob {
111111
$_.Exception.Message
112112
}
113113
}
114+
{ $_.RemoveTeamsPhoneDID } {
115+
try {
116+
Remove-CIPPUserTeamsPhoneDIDs -userid $userid -username $username -tenantFilter $TenantFilter -Headers $Headers -APIName $APIName
117+
} catch {
118+
$_.Exception.Message
119+
}
120+
}
114121
{ $_.RemoveLicenses -eq $true } {
115122
Remove-CIPPLicense -userid $userid -username $Username -tenantFilter $TenantFilter -Headers $Headers -APIName $APIName -Schedule
116123
}

Modules/CIPPCore/Public/PermissionsTranslator.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5353,5 +5353,23 @@
53535353
"userConsentDescription": "Access Microsoft Teams and Skype for Business data as the signed in user",
53545354
"userConsentDisplayName": "Access Microsoft Teams and Skype for Business data based on the user's role membership",
53555355
"value": "OnPremDirectorySynchronization.ReadWrite.All"
5356+
},
5357+
{
5358+
"description": "Read and Modify Tenant-Acquired Telephone Number Details",
5359+
"displayName": "Read and Modify Tenant-Acquired Telephone Number Details",
5360+
"id": "424b07a8-1209-4d17-9fe4-9018a93a1024",
5361+
"Origin": "Delegated",
5362+
"userConsentDescription": "Allows the app to read and modify your tenant's acquired telephone number details on behalf of the signed-in admin user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc.",
5363+
"userConsentDisplayName": "Allows the app to read and modify your tenant's acquired telephone number details on behalf of the signed-in admin user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc.",
5364+
"value": "TeamsTelephoneNumber.ReadWrite.All"
5365+
},
5366+
{
5367+
"description": "Read and Modify Tenant-Acquired Telephone Number Details",
5368+
"displayName": "Read and Modify Tenant-Acquired Telephone Number Details",
5369+
"id": "0a42382f-155c-4eb1-9bdc-21548ccaa387",
5370+
"Origin": "Application",
5371+
"userConsentDescription": "Allows the app to read your tenant's acquired telephone number details, without a signed-in user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc.",
5372+
"userConsentDisplayName": "Allows the app to read your tenant's acquired telephone number details, without a signed-in user. Acquired telephone numbers may include attributes related to assigned object, emergency location, network site, etc.",
5373+
"value": "TeamsTelephoneNumber.ReadWrite.All"
53565374
}
53575375
]
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using namespace System.Net
2+
using namespace System.Collections.Generic
3+
4+
function Remove-CIPPUserTeamsPhoneDIDs {
5+
[CmdletBinding()]
6+
param (
7+
$Headers,
8+
[parameter(Mandatory = $true)]
9+
[string]$UserID,
10+
[string]$Username,
11+
$APIName = 'Remove User Teams Phone DIDs',
12+
[parameter(Mandatory = $true)]
13+
$TenantFilter
14+
)
15+
16+
try {
17+
18+
# Set Username to UserID if not provided
19+
if ([string]::IsNullOrEmpty($Username)) {
20+
$Username = $UserID
21+
}
22+
23+
# Initialize collections for results
24+
$Results = [List[string]]::new()
25+
$SuccessCount = 0
26+
$ErrorCount = 0
27+
28+
# Get all tenant DIDs
29+
$TeamsPhoneDIDs = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/admin/teams/telephoneNumberManagement/numberAssignments" -tenant $TenantFilter
30+
31+
if (-not $TeamsPhoneDIDs -or $TeamsPhoneDIDs.Count -eq 0) {
32+
$Result = "No Teams Phone DIDs found in tenant"
33+
$Results.Add($Result)
34+
return $Results.ToArray()
35+
}
36+
37+
# Filter DIDs assigned to the specific user
38+
$UserDIDs = $TeamsPhoneDIDs | Where-Object { $_.assignmentTargetId -eq $UserID -and $_.assignmentStatus -ne 'unassigned' }
39+
40+
if (-not $UserDIDs -or $UserDIDs.Count -eq 0) {
41+
$Result = "No Teams Phone DIDs found assigned to user: '$Username' - '$UserID'"
42+
$Results.Add($Result)
43+
return $Results.ToArray()
44+
}
45+
46+
# Prepare bulk requests for all DIDs
47+
$RemoveRequests = foreach ($DID in $UserDIDs) {
48+
@{
49+
id = $DID.telephoneNumber
50+
method = 'POST'
51+
url = "admin/teams/telephoneNumberManagement/numberAssignments/unassignNumber"
52+
body = @{
53+
telephoneNumber = $DID.telephoneNumber
54+
numberType = $DID.numberType
55+
}
56+
}
57+
}
58+
59+
# Execute bulk request
60+
$RemoveResults = New-GraphBulkRequest -tenantid $TenantFilter -requests @($RemoveRequests)
61+
62+
# Process results
63+
$RemoveResults | ForEach-Object {
64+
$PhoneNumber = $_.id
65+
66+
if ($_.status -eq 204) {
67+
$SuccessResult = "Successfully removed Teams Phone DID: '$PhoneNumber' from: '$Username' - '$UserID'"
68+
Write-LogMessage -headers $Headers -API $APIName -message $SuccessResult -Sev 'Info' -tenant $TenantFilter
69+
$Results.Add($SuccessResult)
70+
$SuccessCount++
71+
} else {
72+
$ErrorMessage = if ($_.body.error.message) {
73+
$_.body.error.message
74+
} else {
75+
"HTTP Status: $($_.status)"
76+
}
77+
78+
$ErrorResult = "Failed to remove Teams Phone DID: '$PhoneNumber' from: '$Username' - '$UserID'. Error: $ErrorMessage"
79+
Write-LogMessage -headers $Headers -API $APIName -message $ErrorResult -Sev 'Error' -tenant $TenantFilter
80+
$Results.Add($ErrorResult)
81+
$ErrorCount++
82+
}
83+
}
84+
85+
# Add summary result
86+
$SummaryResult = "Completed processing $($UserDIDs.Count) DIDs for user '$Username': $SuccessCount successful, $ErrorCount failed"
87+
Write-LogMessage -headers $Headers -API $APIName -message $SummaryResult -Sev 'Info' -tenant $TenantFilter
88+
$Results.Add($SummaryResult)
89+
90+
return $Results.ToArray()
91+
92+
} catch {
93+
$ErrorMessage = Get-CippException -Exception $_
94+
$Result = "Failed to process Teams Phone DIDs removal for: '$Username' - '$UserID'. Error: $($ErrorMessage.NormalizedError)"
95+
Write-LogMessage -headers $Headers -API $APIName -message $Result -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
96+
throw $Result
97+
}
98+
}

Modules/CIPPCore/Public/SAMManifest.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@
570570
{
571571
"id": "b7887744-6746-4312-813d-72daeaee7e2d",
572572
"type": "Scope"
573+
},
574+
{
575+
"id": "424b07a8-1209-4d17-9fe4-9018a93a1024",
576+
"type": "Scope"
577+
},
578+
{
579+
"id": "0a42382f-155c-4eb1-9bdc-21548ccaa387",
580+
"type": "Role"
573581
}
574582
]
575583
},
@@ -643,4 +651,4 @@
643651
]
644652
}
645653
]
646-
}
654+
}

0 commit comments

Comments
 (0)