This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CyUsers.ps1
186 lines (159 loc) · 4.86 KB
/
CyUsers.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
<#
.SYNOPSIS
Gets a list of users in the console
#>
function Get-CyUserList {
[CmdletBinding(DefaultParameterSetName="All")]
Param (
[parameter(Mandatory=$false)]
[CylanceAPIHandle]$API = $GlobalCyAPIHandle
)
Read-CyData -API $API -Uri "$($API.BaseUrl)/users/v2"
}
<#
.SYNOPSIS
Retrieves the details of a user object.
.PARAMETER User
A user object (retrieved e.g. via Get-CyUserList)
.PARAMETER UserId
A user ID or email to retrieve the user detail for
#>
function Get-CyUserDetail {
Param (
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[CylanceAPIHandle]$API = $GlobalCyAPIHandle,
[Parameter(ParameterSetName="ByUser", Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[object[]]$User,
[Parameter(Mandatory=$true,ParameterSetName="ByUserIdOrEmail")]
[object]$UserId
)
Process {
switch ($PSCmdlet.ParameterSetName) {
"ByUserIdOrEmail" {
$url = "$($API.BaseUrl)/users/v2/$($UserId)"
}
"ByUser" {
$url = "$($API.BaseUrl)/users/v2/$($User.id)"
}
}
# Read-CyData -API $API -Uri $url
Invoke-CyRestMethod -API $API -Method GET -Uri $url | Convert-CyObject
}
}
<#
.SYNOPSIS
Resolves a human-readable role identifier to the fixed 'guid' assigned to the role.
#>
function RoleToGuid() {
Param(
[Parameter(Mandatory=$true)]
[string]$Role,
[Parameter(Mandatory=$false)]
[ValidateSet("User", "Zone")]
[string]$Type = "User"
)
switch ($Type) {
"User" {
switch ($Role) {
"User" {
"00000000-0000-0000-0000-000000000001"
}
"Administrator" {
"00000000-0000-0000-0000-000000000002"
}
"ZoneManager" {
"00000000-0000-0000-0000-000000000003"
}
}
}
"Zone" {
switch ($Role) {
"ZoneManager" {
"00000000-0000-0000-0000-000000000001"
}
"User" {
"00000000-0000-0000-0000-000000000002"
}
}
}
}
}
<#
.SYNOPSIS
Creates a new user account
.PARAMETER UserId
The user's email address
.PARAMETER FirstName
The user's first name
.PARAMETER LastName
The user's first name
.PARAMETER Role
The user's role
.PARAMETER ZoneRights
If the user's role is "zone manager", an array of hashtables with pairs of "zone_id" (zone's ID) and "role" ("ZoneManager", "User").
#>
function New-CyUser {
Param (
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[CylanceAPIHandle]$API = $GlobalCyAPIHandle,
[string]$UserId,
[Parameter(Mandatory=$true)]
[string]$FirstName,
[Parameter(Mandatory=$true)]
[string]$LastName,
[Parameter(Mandatory=$true)]
[ValidateSet ("Administrator","ZoneManager","User")]
[string]$Role,
[Parameter(Mandatory=$false)]
[Hashtable[]]$ZoneRights
)
Process {
$EffectiveRole = RoleToGuid -Role $Role
$updateMap = @{
email = $UserId
user_role = $EffectiveRole
first_name = $FirstName
last_name = $LastName
}
if ($EffectiveRole -eq "00000000-0000-0000-0000-000000000003") {
$EffectiveZoneRights = @()
foreach ($ZoneRight in $ZoneRights) {
Write-Host "Zone: $($ZoneRight.zone_id) = $($ZoneRight.role)"
$EffectiveZoneRights += @{
id = $ZoneRight.zone_id
role_type = RoleToGuid -Role $ZoneRight.role -Type Zone
}
}
$updateMap.zones = $EffectiveZoneRights
}
$json = ConvertTo-Json $updateMap
Invoke-CyRestMethod -API $API -Method POST -Uri "$($API.BaseUrl)/users/v2" -Body $json -ContentType "application/json; charset=utf-8" | Convert-CyObject
}
}
<#
.SYNOPSIS
Sends an invite to a user
.PARAMETER UserId
The user's email address
#>
function Invoke-CySendUserInvite {
Param (
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[CylanceAPIHandle]$API = $GlobalCyAPIHandle,
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]$UserId
)
Process {
if ($null -ne $UserId.email) {
$id = $UserId.email
}
else
{
$id = $UserId
}
Invoke-CyRestMethod -API $API -Method POST -Uri "$($API.BaseUrl)/users/v2/$($id)/invite" -Body "" -ContentType "application/json; charset=utf-8" | Convert-CyObject
}
}