Skip to content

Instantly share code, notes, and snippets.

@cyanide-burnout
Last active September 21, 2022 09:10
Show Gist options
  • Save cyanide-burnout/293ae0276ff0dfcc27db73b5133853d2 to your computer and use it in GitHub Desktop.
Save cyanide-burnout/293ae0276ff0dfcc27db73b5133853d2 to your computer and use it in GitHub Desktop.
Check user membership in Azure AD (direct membership)
<?php
// https://learn.microsoft.com/en-us/graph/api/user-list-memberof?view=graph-rest-1.0&tabs=http
if (array_key_exists("access_token", $_SESSION))
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $_SESSION["access_token"]));
curl_setopt($handle, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/memberOf");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$data = json_decode($response, true);
curl_close($handle);
if (array_key_exists("value", $data))
{
$_SESSION["membership"] = array("roles" => array(), "groups" => array());
foreach ($data["value"] as $object)
{
if ($object["@odata.type"] == "#microsoft.graph.directoryRole") $_SESSION["membership"]["roles"][] = $object["id"];
if ($object["@odata.type"] == "#microsoft.graph.group") $_SESSION["membership"]["groups"][] = $object["id"];
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment