-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_create.php
More file actions
84 lines (71 loc) · 2.2 KB
/
Copy pathuser_create.php
File metadata and controls
84 lines (71 loc) · 2.2 KB
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
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
# initialize http client
use GuzzleHttp\Client;
$client = new GuzzleHttp\Client();
$forsta_api_path = "https://atlas.forsta.io/v1/";
# login as admin to retrieve jwt
# Either use auth_token OR username and password
$admin_user_auth_token = '<admin authtoken>';
$admin_username = '@<user>:<org>';
$admin_password = '<password>';
# User to create
$new_user_first_name = 'Test';
$new_user_last_name = 'User';
$new_user_tag_slug = 'test1234';
try {
$loginResponse = $client->post($forsta_api_path . 'login/', [
'json' => [
'userauthtoken' => $admin_user_auth_token
# 'fq_tag' => $admin_username,
# 'password' => $admin_password
]
]);
}
catch (Exception $e) {
exit("Failed to login: " . $e->getMessage());
}
echo("Successfully connected\n");
$loginResponseJson = json_decode($loginResponse->getBody()->getContents());
$jwt = 'JWT ' . $loginResponseJson->token;
# use admin jwt to create a new user
try {
$createUserResponse = $client->post($forsta_api_path . 'user/', [
'headers' => [
'Authorization' => $jwt
],
'json' => [
'first_name' => $new_user_first_name,
'last_name' => $new_user_last_name,
'tag_slug' => $new_user_tag_slug,
]
]);
}
catch (Exception $e) {
exit("Failed to create user: " . $e->getMessage());
}
echo("User created\n");
$createUserResponseJson = json_decode($createUserResponse->getBody()->getContents());
$newUserId = $createUserResponseJson->id;
# create a new auth token for the user we just created
$tokenDescription = 'Auth Token Description';
try{
$postAuthTokenResponse = $client->post($forsta_api_path . 'userauthtoken/', [
'headers' => [
'Authorization' => $jwt
],
'json' => [
'userid' => $newUserId,
'description' => $tokenDescription
]
]);
}
catch (Exception $e) {
exit("Failed to create user authtoken: " . $e->getMessage());
}
$createUserResponseJson = json_decode($postAuthTokenResponse->getBody()->getContents());
echo("User authtoken created. Please save the following:\n");
echo($createUserResponseJson->token . "\n");
?>