Last active
September 21, 2022 09:10
-
-
Save cyanide-burnout/293ae0276ff0dfcc27db73b5133853d2 to your computer and use it in GitHub Desktop.
Check user membership in Azure AD (direct membership)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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