Skip to content

Commit

Permalink
User update from resource owner details is configurable - refs BT#16734
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Ducoulombier committed Feb 28, 2020
1 parent 7394a58 commit 502f890
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
14 changes: 14 additions & 0 deletions plugin/oauth2/config.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Adjust user account values
*
* @param array $response the Resource Owner Details
* @param Chamilo\UserBundle\Entity\User $user the user
* with firstname, lastname, status, username and email already updated
* but unsaved
*/
function oauth2_update_user_from_resource_owner_details(array $response, Chamilo\UserBundle\Entity\User $user)
{
$user->setStatus(STUDENT);
$user->setPhone($response['data'][0]['telephone']);
}
75 changes: 44 additions & 31 deletions plugin/oauth2/src/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,48 @@ private function getValuesByKey(array $data, $key, $default = [])
return $values;
}

private function updateUser($userId, $response)
{
/**
* @var $user Chamilo\UserBundle\Entity\User
*/
$user = UserManager::getRepository()->find($userId);
$user->setFirstname(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_FIRSTNAME
), $user->getFirstname())
);
$user->setLastname(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_LASTNAME
), $user->getLastname())
);
$user->setUserName(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_USERNAME
), $user->getUsername())
);
$user->setEmail(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_EMAIL
), $user->getEmail())
);
$user->setStatus(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS
), $user->getStatus())
);
$user->setAuthSource('oauth2');
$configFilePath = __DIR__.'/../config.php';
if (file_exists($configFilePath)) {
require_once $configFilePath;
$functionName = 'oauth2_update_user_from_resource_owner_details';
if (function_exists($functionName)) {
$functionName($response, $user);
}
}
UserManager::getManager()->updateUser($user);
}
/**
* Updates the Access URLs associated to a user
* according to the OAuth2 server response resource owner
Expand Down Expand Up @@ -285,6 +327,7 @@ public function getUserInfo($provider, $accessToken)
if (false === $userId) {
throw new RuntimeException(get_lang('FailedUserCreation'));
}
$this->updateUser($userId, $response);
// Not checking function update_extra_field_value return value because not reliable
UserManager::update_extra_field_value($userId, self::EXTRA_FIELD_OAUTH2_ID, $resourceOwnerId);
$this->updateUserUrls($userId, $response);
Expand All @@ -296,37 +339,7 @@ public function getUserInfo($provider, $accessToken)
$userId = $result;
}
if ('true' === $this->get(self::SETTING_UPDATE_USER_INFO)) {
/**
* @var $user FOS\UserBundle\Model\User
*/
$user = UserManager::getRepository()->find($userId);
$user->setFirstname(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_FIRSTNAME
), $user->getFirstname())
);
$user->setLastname(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_LASTNAME
), $user->getLastname())
);
$user->setUserName(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_USERNAME
), $user->getUsername())
);
$user->setEmail(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_EMAIL
), $user->getEmail())
);
$user->setStatus(
$this->getValueByKey($response, $this->get(
self::SETTING_RESPONSE_RESOURCE_OWNER_STATUS
), $user->getStatus())
);
$user->setAuthSource('oauth2');
UserManager::getManager()->updateUser($user);
$this->updateUser($userId, $response);
$this->updateUserUrls($userId, $response);
}
}
Expand Down

0 comments on commit 502f890

Please sign in to comment.