Repository: airavata-php-gateway Updated Branches: refs/heads/develop 7737bc57c -> 6d19ca7c9
Fixing finding Keycloak user by username 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/6d19ca7c Tree: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/tree/6d19ca7c Diff: http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/diff/6d19ca7c Branch: refs/heads/develop Commit: 6d19ca7c9d1f0a71bdfd6f434b5e36c58f86b126 Parents: 7737bc5 Author: Marcus Christie <[email protected]> Authored: Mon Jun 19 10:13:19 2017 -0400 Committer: Marcus Christie <[email protected]> Committed: Mon Jun 19 10:13:19 2017 -0400 ---------------------------------------------------------------------- app/libraries/Keycloak/API/Users.php | 13 +++++++++++++ app/libraries/Keycloak/Keycloak.php | 25 +++++++++++-------------- 2 files changed, 24 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/6d19ca7c/app/libraries/Keycloak/API/Users.php ---------------------------------------------------------------------- diff --git a/app/libraries/Keycloak/API/Users.php b/app/libraries/Keycloak/API/Users.php index d03be02..e0e112c 100644 --- a/app/libraries/Keycloak/API/Users.php +++ b/app/libraries/Keycloak/API/Users.php @@ -42,6 +42,19 @@ class Users extends BaseKeycloakAPIEndpoint { return $result; } + public function getUserByUsername($realm, $username){ + + # getUsers returns all users that have a username containing $username + # so we need to check the returned users for one that matches exactly + $users = $this->getUsers($realm, $username); + foreach ($users as $user) { + if ($user->username == $username) { + return $user; + } + } + return null; + } + /** * Search users * GET /admin/realms/{realm}/users http://git-wip-us.apache.org/repos/asf/airavata-php-gateway/blob/6d19ca7c/app/libraries/Keycloak/Keycloak.php ---------------------------------------------------------------------- diff --git a/app/libraries/Keycloak/Keycloak.php b/app/libraries/Keycloak/Keycloak.php index 7f0d511..951e14b 100644 --- a/app/libraries/Keycloak/Keycloak.php +++ b/app/libraries/Keycloak/Keycloak.php @@ -354,9 +354,8 @@ class Keycloak { * @param $username */ public function getUserProfile($username){ - $users = $this->users->getUsers($this->realm, $username); - if(count($users) > 0){ - $user = $users[0]; + $user = $this->users->getUserByUsername($this->realm, $username); + if($user != null){ $result = []; $result["email"] = $user->email; $result["firstname"] = $user->firstName; @@ -375,8 +374,8 @@ class Keycloak { */ public function usernameExists($username){ try{ - $users = $this->users->getUsers($this->realm, $username); - return $users != null && count($users) > 0; + $user = $this->users->getUserByUsername($this->realm, $username); + return $user != null; }catch (Exception $ex){ // Username does not exists return false; @@ -387,9 +386,9 @@ class Keycloak { public function isUpdatePasswordRequired($username) { try{ - $users = $this->users->getUsers($this->realm, $username); - if ($users != null && count($users) == 1) { - return in_array("UPDATE_PASSWORD", $users[0]->requiredActions); + $user = $this->users->getUserByUsername($this->realm, $username); + if ($user != null) { + return in_array("UPDATE_PASSWORD", $user->requiredActions); } else { return false; } @@ -413,13 +412,11 @@ class Keycloak { * Get the user's Keycloak user_id from their username */ private function getUserId($username) { - $users = $this->users->getUsers($this->realm, $username); - if (count($users) > 1) { - throw new Exception("More than one user has username $username"); - } else if (count($users) == 0) { - throw new Exception("No user found with username $username"); + $user = $this->users->getUserByUsername($this->realm, $username); + if ($user != null) { + return $user->id; } else { - return $users[0]->id; + throw new Exception("No user found with username $username"); } }
