Last active
March 15, 2017 21:25
-
-
Save pitbulk/048316796c44aceccb0df6aca3be7c2f to your computer and use it in GitHub Desktop.
Onelogin - Roles API
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 | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/api/1/roles"; | |
$access_token = "<access_token>"; | |
// Parameters | |
$query_parameters = array( | |
// -- Search -- | |
'id' => null, | |
'name' => null, | |
// -- Pagination -- | |
// 'after_cursor' => '', | |
// 'before_cursor' => '', | |
// -- Limit -- | |
'limit' => 50, // Max limit (default value) | |
// -- Sort -- | |
// 'sort' => '+id', | |
// -- Fields (return only those fields) -- | |
// 'fields' => 'id, name', | |
); | |
$query = http_build_query(array_filter($query_parameters)); | |
$authorization = "bearer:$access_token"; | |
if (!empty($query)) { | |
$url .= '?' . $query; | |
} | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
$header_opts = array( | |
'Authorization:'.$authorization | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts); | |
$result = curl_exec($ch); | |
if ($result !== false) { | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$body = substr($result, $header_size); | |
$result_data = json_decode($body); | |
if ($result_data->status->error == true) { | |
$errorMsg = $result_data->status->code. ", ".$result_data->status->type; | |
if ($result_data->status->message instanceof stdClass) { | |
$errorMsg .= " || " . $result_data->status->message->description; | |
} else { | |
$errorMsg .= " || " . $result_data->status->message; | |
} | |
throw new Exception($errorMsg); | |
} else if (empty($result_data->data)) { | |
// No result | |
$roles = array(); | |
} else { | |
$roles = $result_data->data; | |
$before_cursor = $result_data->pagination->before_cursor; | |
$after_cursor = $result_data->pagination->after_cursor; | |
// Now if $after_cursor is not empty, I can pass it as | |
// a parameter of $query_parameters and execute again | |
// the curl to retrieve all roles. | |
} | |
} else { | |
throw new Exception(curl_error($ch), curl_errno($ch)); | |
} | |
curl_close($ch); | |
// Let's review the parameters used on the different examples listed at | |
// https://developers.onelogin.com/api-docs/1/roles/get-roles | |
// Return the role whose name value equals employee | |
/* | |
$query_parameters = array( | |
'name' => 'employee' | |
); | |
*/ | |
// Include wildcards. For example, you can return all roles whose name values end with -us: | |
/* | |
$query_parameters = array( | |
'name' => '*-us' | |
); | |
*/ | |
// Return a limited number of roles: | |
/* | |
$query_parameters = array( | |
'limit' => 10 | |
); | |
*/ | |
//Return roles sorted by id. Use + to sort in ascending order or - to sort in descending order: | |
/* | |
$query_parameters = array( | |
'sort' => '+id' | |
); | |
*/ | |
// Return roles displaying only selected fields per role: | |
/* | |
$query_parameters = array( | |
'fields' => 'id' | |
); | |
*/ | |
// Combine use of multiple query parameters using an &: | |
/* | |
$query_parameters = array( | |
'name' ==> '*-us', | |
'sort' => '+id', | |
'fields' => 'id' | |
); | |
*/ |
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 | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/api/1/roles"; | |
$access_token = "<access_token>"; | |
// Parameters | |
$id = "<role_id>"; | |
$authorization = "bearer:$access_token"; | |
if (empty($id)) { | |
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call"); | |
} | |
$url .= "/".$id; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
$header_opts = array( | |
'Authorization:'.$authorization | |
); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts); | |
$result = curl_exec($ch); | |
if ($result !== false) { | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$body = substr($result, $header_size); | |
$result_data = json_decode($body); | |
if ($result_data->status->error == true) { | |
$errorMsg = $result_data->status->code. ", ".$result_data->status->type; | |
$errorMsg .= " || ". $result_data->status->message; | |
throw new Exception($errorMsg); | |
} else { | |
$role = $result_data->data; | |
} | |
} else { | |
throw new Exception(curl_error($ch), curl_errno($ch)); | |
} | |
curl_close($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment