-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
207 lines (187 loc) · 9.5 KB
/
create.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#################################################
# HelloID-Conn-Prov-Target-NewBlack-Create
# PowerShell V2
#################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Resolve-NewBlackError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
if (-not [string]::IsNullOrEmpty($ErrorObject.ErrorDetails.Message)) {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
} elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
if ($null -ne $ErrorObject.Exception.Response) {
$streamReaderResponse = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
if (-not [string]::IsNullOrEmpty($streamReaderResponse)) {
$httpErrorObj.ErrorDetails = $streamReaderResponse
}
}
}
try {
$errorDetailsObject = ($httpErrorObj.ErrorDetails | ConvertFrom-Json)
$httpErrorObj.FriendlyMessage = $errorDetailsObject.Error.Message
} catch {
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
}
Write-Output $httpErrorObj
}
}
#endregion
try {
# Initial Assignments
$outputContext.AccountReference = 'Currently not available'
$headers = @{
'EVA-User-Agent' = 'HelloID/1.0.0'
'Content-Type' = 'application/json'
Authorization = "Bearer $($actionContext.Configuration.ApiKey)"
}
# Validate correlation configuration
if ($actionContext.CorrelationConfiguration.Enabled) {
$correlationField = $actionContext.CorrelationConfiguration.AccountField
$correlationValue = $actionContext.CorrelationConfiguration.PersonFieldValue
if ([string]::IsNullOrEmpty($($correlationField))) {
throw 'Correlation is enabled but not configured correctly'
}
if ([string]::IsNullOrEmpty($($correlationValue))) {
throw 'Correlation is enabled but [accountFieldValue] is empty. Please make sure it is correctly mapped'
}
# Determine if a user needs to be [created] or [correlated]
$splatSearchUser = @{
Uri = "$($actionContext.Configuration.BaseUrl)/message/SearchUsers"
Method = 'POST'
Headers = $headers
Body = @{
$correlationField = $correlationValue
IncludeCustomers = $false
IncludeEmployees = $true
} | ConvertTo-Json
}
$responseSearchUser = (Invoke-WebRequest @splatSearchUser)
$correlatedAccount = ([Text.Encoding]::UTF8.GetString([Text.Encoding]::GetEncoding(28591).GetBytes(($responseSearchUser.Content))) | ConvertFrom-Json).Result.Page
}
$actionList = [System.Collections.Generic.List[Object]]::new()
if ($correlatedAccount.Count -eq 1) {
$actionList.Add('CorrelateAccount')
if (-not ($correlatedAccount.BackendRelationID -eq $actionContext.Data.BackendRelationID)) {
$actionList.Add('UpdateBackendRelationID')
}
} elseif ($correlatedAccount.Count -gt 1) {
throw "Multiple Accounts [$($correlatedAccount.Count)] found with Correlation Value [$correlationField : $correlationValue]"
} else {
# Check that the email is not already in use to prevent updating an existing account
if (-not $null -eq $actionContext.Data.EmailAddress) {
$bodyJson = @{
EmailAddress = $actionContext.Data.EmailAddress
IncludeCustomers = $false
IncludeEmployees = $true
} | ConvertTo-Json
$splatSearchUser = @{
Uri = "$($actionContext.Configuration.BaseUrl)/message/SearchUsers"
Method = 'POST'
Headers = $headers
Body = ([System.Text.Encoding]::UTF8.GetBytes($bodyJson))
}
$responseSearchUser = (Invoke-WebRequest @splatSearchUser)
if (-not ($null -eq $responseSearchUser.Content)) {
$AccountWithSameEmail = ([Text.Encoding]::UTF8.GetString([Text.Encoding]::GetEncoding(28591).GetBytes(($responseSearchUser.Content))) | ConvertFrom-Json).Result.Page
}
if ($AccountWithSameEmail.Count -ge 1) {
throw "There already is an account with email address [$($AccountWithSameEmail[0].EmailAddress)]: Account ID [$($AccountWithSameEmail[0].ID)] EmployeeNumber [$($AccountWithSameEmail[0].EmployeeNumber)]"
}
}
$actionList.Add('CreateAccount')
$actionList.Add('UpdateBackendRelationID')
}
# Process
foreach ($action in $actionList) {
switch ($action) {
'CreateAccount' {
$splatCreateParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/message/CreateEmployeeUser"
Method = 'POST'
Body = ([System.Text.Encoding]::UTF8.GetBytes(( $actionContext.Data | ConvertTo-Json)))
Headers = $headers
}
if (-not($actionContext.DryRun -eq $true)) {
Write-Information 'Creating and correlating NewBlack account'
$createdAccountResult = Invoke-RestMethod @splatCreateParams
if ($createdAccountResult.Result -eq 2) {
Write-Warning "Warning: Intended to create account for employee number $($actionContext.Data.EmployeeNumber), but an existing account with user ID $($createdAccountResult.UserId) was updated instead."
}
$outputContext.AccountReference = $createdAccountResult.UserID
} else {
Write-Information '[DryRun] Create and correlate NewBlack account, will be executed during enforcement'
}
$outputContext.Data = $actionContext.Data
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Create account was successful. AccountReference is: [$($outputContext.AccountReference)]"
IsError = $false
})
break
}
'CorrelateAccount' {
Write-Information 'Correlating NewBlack account'
$outputContext.Data = $correlatedAccount
$outputContext.AccountReference = $correlatedAccount.Id
$outputContext.AccountCorrelated = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Correlated account: [$($outputContext.AccountReference)] on field: [$($correlationField)] with value: [$($correlationValue)]"
IsError = $false
})
break
}
'UpdateBackendRelationID' {
Write-Information 'Update BackendRelationID NewBlack account'
$splatUpdateParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/message/UpdateUser"
Method = 'POST'
Body = @{
ID = $outputContext.AccountReference
BackendRelationID = $actionContext.Data.BackendRelationID
} | ConvertTo-Json
Headers = $headers
}
if (-not($actionContext.DryRun -eq $true)) {
$null = Invoke-RestMethod @splatUpdateParams
} else {
Write-Information '[DryRun] Update BackendRelationID NewBlack account, will be executed during enforcement'
}
$outputContext.Data | Add-Member -MemberType NoteProperty -Name BackendRelationID -Value $actionContext.Data.BackendRelationID -Force
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Update BackendRelationID [$($actionContext.Data.BackendRelationID)] NewBlack account"
IsError = $false
})
break
}
}
}
$outputContext.Success = $true
} catch {
$outputContext.Success = $false
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-NewBlackError -ErrorObject $ex
$auditMessage = "Could not create or correlate NewBlack account. Error: $($errorObj.FriendlyMessage)"
Write-Warning "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
} else {
$auditMessage = "Could not create or correlate NewBlack account. Error: $($ex.Exception.Message)"
Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}