AIRAVATA-2342 Keycloak: search users
Project: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/repo Commit: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/commit/994483d0 Tree: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/tree/994483d0 Diff: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/diff/994483d0 Branch: refs/heads/develop Commit: 994483d0c48d0272eda12d8f2824bc7f342e181f Parents: 64362fe Author: Marcus Christie <[email protected]> Authored: Tue Mar 28 17:15:54 2017 -0400 Committer: Marcus Christie <[email protected]> Committed: Tue Mar 28 17:15:54 2017 -0400 ---------------------------------------------------------------------- app/controllers/AdminController.php | 2 +- app/libraries/Keycloak/API/Users.php | 30 ++++++++++++++++++++++++++++++ app/libraries/Keycloak/Keycloak.php | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/994483d0/app/controllers/AdminController.php ---------------------------------------------------------------------- diff --git a/app/controllers/AdminController.php b/app/controllers/AdminController.php index f52de91..d6a19e0 100644 --- a/app/controllers/AdminController.php +++ b/app/controllers/AdminController.php @@ -85,7 +85,7 @@ class AdminController extends BaseController { public function searchUsersView(){ if(Input::has("search_val")) { - $users = WSIS::searchUsers(Input::get("search_val")); + $users = Keycloak::searchUsers(Input::get("search_val")); } else $users = Keycloak::listUsers(); http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/994483d0/app/libraries/Keycloak/API/Users.php ---------------------------------------------------------------------- diff --git a/app/libraries/Keycloak/API/Users.php b/app/libraries/Keycloak/API/Users.php index c1c8aca..089ad3a 100644 --- a/app/libraries/Keycloak/API/Users.php +++ b/app/libraries/Keycloak/API/Users.php @@ -55,6 +55,36 @@ class Users { } /** + * Search users + * GET /admin/realms/{realm}/users + * NOTE: the search is a substring search across users' usernames, first and + * last names, and email address + * Returns Array of UserRepresentation + */ + public function searchUsers($realm, $keyword){ + + // get access token for admin API + $access_token = $this->getAPIAccessToken(); + $url = $this->base_endpoint_url . '/admin/realms/' . rawurlencode($realm) . '/users?search=' . rawurlencode($keyword); + // Log::debug("getUsers url", array($url)); + $r = curl_init($url); + curl_setopt($r, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($r, CURLOPT_ENCODING, 1); + curl_setopt($r, CURLOPT_SSL_VERIFYPEER, $this->verify_peer); + curl_setopt($r, CURLOPT_HTTPHEADER, array( + "Authorization: Bearer " . $access_token + )); + + $response = curl_exec($r); + if ($response == false) { + die("curl_exec() failed. Error: " . curl_error($r)); + } + $result = json_decode($response); + // Log::debug("getUsers result", array($result)); + return $result; + } + + /** * Get representation of a user * GET /admin/realms/{realm}/users/{id} * Returns a UserRepresentation http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/994483d0/app/libraries/Keycloak/Keycloak.php ---------------------------------------------------------------------- diff --git a/app/libraries/Keycloak/Keycloak.php b/app/libraries/Keycloak/Keycloak.php index 8c9c1a1..1f876b0 100644 --- a/app/libraries/Keycloak/Keycloak.php +++ b/app/libraries/Keycloak/Keycloak.php @@ -191,6 +191,22 @@ class Keycloak { } /** + * Function to search users + * NOTE: Keycloak uses the keyword to search in the username, first and last + * name and email address + * @param $keyword + * @return Array of usernames + */ + public function searchUsers($phrase){ + $users = $this->users->searchUsers($this->realm, $phrase); + $usernames = []; + foreach ($users as $user) { + $usernames[] = $user->username; + } + return $usernames; + } + + /** * Function to get the list of all existing roles * For Keycloak this is a list of "Realm roles" *
