Skip to content

Commit 63e5a0c

Browse files
kmazariegosKarla Mazariegos
andauthored
Redesign Audit Logs PHP Example Application (#26)
* Redesign Audit Logs PHP Example Application * fixing issues post review * fixing issues for audit logs --------- Co-authored-by: Karla Mazariegos <[email protected]>
1 parent 4b6318d commit 63e5a0c

File tree

15 files changed

+962
-669
lines changed

15 files changed

+962
-669
lines changed

php-audit-logs-example/.DS_Store

6 KB
Binary file not shown.

php-audit-logs-example/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ Composer - [Link](https://getcomposer.org/)
1818
composer i
1919
```
2020

21-
2. Create a new file called `.env` and enter your API Key and Client ID from the WorkOS Dashboard.
21+
2. Create a new file called `.env` and enter your API Key and Client ID from the WorkOS Dashboard. Add your system username from your computer to generate the path to the downloads folder in line 189 in router.php. Note: do not add '' to the username in your .env file.
2222

2323
```
24-
WORKOS_API_KEY="your_api_key"
25-
WORKOS_CLIENT_ID="your_client_id"
24+
WORKOS_API_KEY='your_api_key'
25+
WORKOS_CLIENT_ID='your_client_id'
26+
PATH_USERNAME=username
2627
```
2728

2829
## Running the app
@@ -50,7 +51,7 @@ Action title: "user.connection_deleted" | Target type: "team"
5051

5152
5. Once you enter the Organization ID and submit it, you will be brought to the page where you'll be able to send the audit log events that were just configured. You'll also notice that the action of setting the Organization triggered an Audit Log already. Click the buttons to send the respective events.
5253

53-
6. To obtain a CSV of the Audit Log events that were sent for the last 30 days, click the "Export Events" button. This will bring you to a new page where you can download the events. Downloading the events is a 2 step process. First you need to create the report by clicking the "Generate CSV" button. Then click the "Access CSV" button to download a CSV of the Audit Log events for the selected Organization for the past 30 days.
54+
6. To obtain a CSV of the Audit Log events that were sent for the last 30 days, click the "Export Events" tab. Downloading the events is a 2 step process. First you need to create the report by clicking the "Generate CSV" button. Then click the "Access CSV" button to download a CSV of the Audit Log events for the selected Organization for the past 30 days.
5455

5556
## Need help?
5657

php-audit-logs-example/auditLogEvents.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

php-audit-logs-example/router.php

Lines changed: 86 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

3-
require __DIR__ . "/vendor/autoload.php";
4-
include './auditLogEvents.php';
3+
require __DIR__ . "/vendor/autoload.php";
54

65
use Twig\Environment;
76
use Twig\Loader\FilesystemLoader;
@@ -11,7 +10,8 @@
1110

1211
//Set API Key, ClientID, and Connection
1312
$WORKOS_API_KEY = $_ENV['WORKOS_API_KEY'];
14-
$WORKOS_CLIENT_ID = $_ENV['WORKOS_CLIENT_ID'];
13+
$WORKOS_CLIENT_ID = $_ENV['WORKOS_CLIENT_ID'];
14+
$PATH_USERNAME = $_ENV['PATH_USERNAME'];
1515

1616
// Setup html templating library
1717
$loader = new FilesystemLoader(__DIR__ . '/templates');
@@ -50,6 +50,16 @@ function objectToArray($d)
5050
}
5151
}
5252

53+
// Convenient function for redirecting to URL
54+
function Redirect($url, $permanent = false)
55+
{
56+
if (headers_sent() === false) {
57+
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
58+
}
59+
60+
exit();
61+
}
62+
5363
//Routing
5464
switch (strtok($_SERVER["REQUEST_URI"], "?")) {
5565
case (preg_match("/\.css$/", $_SERVER["REQUEST_URI"]) ? true : false):
@@ -70,42 +80,69 @@ function objectToArray($d)
7080
}
7181
return httpNotFound();
7282

73-
74-
//set_org route is what will set organization
83+
//List Organizations
7584
case ("/"):
7685
case ("/login"):
77-
case ("/logout"):
78-
echo $twig->render("login.html.twig");
86+
session_start();
87+
$before = $_GET['before'] ?? "";
88+
$after = $_GET['after'] ?? "";
89+
$listOrganizations = new WorkOS\Organizations();
90+
[$before, $after, $currentPage] = $listOrganizations->listOrganizations(
91+
limit: 5,
92+
before: $before,
93+
after: $after,
94+
order: null
95+
);
96+
$organizations = $currentPage;
97+
echo $twig->render("login.html.twig", ['organizations' => $organizations, 'after' => $after, 'before' => $before]);
7998
return true;
8099

100+
//set_org
81101
case ("/set_org"):
82-
$organizationId = $_POST["org"] ?? "";
102+
session_start();
103+
$organizationId = $_GET["id"] ?? "";
83104
$organization = (new \WorkOS\Organizations()) -> getOrganization($organizationId);
84105
$orgPayloadArray = objectToArray($organization);
85106
$orgPayloadArrayRawData = $orgPayloadArray['raw'];
86107
$finalOrgId = $orgPayloadArrayRawData["id"] ?? "";
87108
$orgName = $orgPayloadArrayRawData["name"] ?? "";
88-
session_start();
89109
$_SESSION['id'] = $finalOrgId;
90110
$_SESSION['name'] = $orgName;
91-
echo $twig->render("send_events.html.twig", ['org_id' => $_SESSION['id'], 'org_name' => $orgName]);
111+
$rangeEnd = (new \DateTime('now',new \DateTimeZone("UTC")))->format(\DateTime::ATOM);
112+
$rangeStart = (new \DateTime('-1 month',new \DateTimeZone("UTC")))->format(\DateTime::ATOM);
113+
echo $twig->render("send_events.html.twig", ['org_id' => $_SESSION['id'], 'org_name' => $orgName, 'rangeStart' => $rangeStart, 'rangeEnd' => $rangeEnd]);
92114
return true;
93115

94116
//send_event
95117
case ("/send_event"):
96118
session_start();
97-
$payload = file_get_contents("php://input");
98-
$eventId = $payload[6];
99-
$event;
100-
if($eventId === '0'){
101-
$event = $user_signed_in;
102-
} else if($eventId === '1'){
103-
$event = $user_logged_out;
104-
} else if($eventId === '2'){
105-
$event = $user_organization_deleted;
106-
} else if($eventId === '3'){
107-
$event = $user_connection_deleted;
108-
}
119+
$action = $_POST['event-action'];
120+
$version = $_POST['event-version'];
121+
$actorName = $_POST['actor-name'];
122+
$actorType = $_POST['actor-type'];
123+
$targetName = $_POST['target-name'];
124+
$targetType = $_POST['target-type'];
125+
$event = [
126+
"action" => $action,
127+
"occurred_at" => date("c"),
128+
"version" => (int)$version,
129+
"actor" => [
130+
"id" => "user_01GBNJC3MX9ZZJW1FSTF4C5938",
131+
"name" => $actorName,
132+
"type" => $actorType,
133+
],
134+
"targets" => [
135+
[
136+
"id" => "team_01GBNJD4MKHVKJGEWK42JNMBGS",
137+
"name" => $targetName,
138+
"type" => $targetType,
139+
],
140+
],
141+
"context" => [
142+
"location" => "123.123.123.123",
143+
"user_agent" => "Chrome/104.0.0.0",
144+
],
145+
];
109146

110147
$orgId = $_SESSION['id'];
111148
$orgName = $_SESSION['name'];
@@ -118,15 +155,6 @@ function objectToArray($d)
118155
echo $twig->render("send_events.html.twig", ['org_id' => $_SESSION['id'], 'org_name' => $_SESSION['name']]);
119156
return true;
120157

121-
//export_events
122-
case ("/export_events"):
123-
session_start();
124-
$payload = file_get_contents("php://input");
125-
$orgId = $_SESSION['id'];
126-
$orgName = $_SESSION['name'];
127-
echo $twig->render("export_events.html.twig", ['org_id' => $orgId, 'org_name' => $orgName]);
128-
return true;
129-
130158
//generate_csv
131159
case ("/get_events"):
132160
session_start();
@@ -158,13 +186,38 @@ function objectToArray($d)
158186
$orgPayloadArrayRawData = $orgPayloadArray['raw'];
159187
$url = $orgPayloadArrayRawData["url"] ?? "";
160188
$source = file_get_contents($url);
161-
file_put_contents('/[YOUR PATH HERE]/auditlogs.csv', $source);
189+
file_put_contents("/Users/$PATH_USERNAME/Downloads/auditlogs.csv", $source);
162190
}
191+
echo $twig->render("send_events.html.twig", ['org_id' => $orgId, 'org_name' => $orgName, 'rangeStart' => $dateNow, 'rangeEnd' => $dateMonth]);
192+
return true;
163193

164-
echo $twig->render("export_events.html.twig", ['org_id' => $orgId, 'org_name' => $orgName]);
194+
//events
195+
case ("/events"):
196+
session_start();
197+
$intent = $_GET['intent'];
198+
$orgId = $_SESSION['id'];
199+
$linkPayloadObject = (new \WorkOS\Portal())->generateLink(
200+
organization: $orgId,
201+
intent: $intent
202+
);
203+
$linkPayloadArray = objectToArray($linkPayloadObject);
204+
$linkPayloadArrayRawData = $linkPayloadArray['raw'];
205+
$finalLink = $linkPayloadArrayRawData['link'];
206+
Redirect($finalLink, false);
207+
echo $twig->render("send_events.html.twig", ['org_id' => $orgId]);
165208
return true;
166209

167210

211+
//change_org
212+
case ("/logout"):
213+
session_start();
214+
$_SESSION['organizations'] = null;
215+
$_SESSION['before'] = null;
216+
$_SESSION['after'] = null;
217+
Redirect('/', false);
218+
echo $twig->render("login.html.twig");
219+
return true;
220+
168221

169222

170223
default:

0 commit comments

Comments
 (0)