diff --git a/src/applications/people/controller/settings/panels/oauth/PhabricatorUserOAuthSettingsPanelController.php b/src/applications/people/controller/settings/panels/oauth/PhabricatorUserOAuthSettingsPanelController.php index df735b9e02..0b6c88226d 100644 --- a/src/applications/people/controller/settings/panels/oauth/PhabricatorUserOAuthSettingsPanelController.php +++ b/src/applications/people/controller/settings/panels/oauth/PhabricatorUserOAuthSettingsPanelController.php @@ -27,24 +27,24 @@ final class PhabricatorUserOAuthSettingsPanelController } public function processRequest() { - - $request = $this->getRequest(); - $user = $request->getUser(); - $provider = $this->provider; - - $notice = null; - + $request = $this->getRequest(); + $user = $request->getUser(); + $provider = $this->provider; + $notice = null; $provider_name = $provider->getProviderName(); - $provider_key = $provider->getProviderKey(); + $provider_key = $provider->getProviderKey(); $oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere( 'userID = %d AND oauthProvider = %s', $user->getID(), $provider->getProviderKey()); + if ($request->isFormPost() && $oauth_info) { + $notice = $this->refreshProfileImage($oauth_info); + } + $form = new AphrontFormView(); - $form - ->setUser($user); + $form->setUser($user); $forms = array(); $forms[] = $form; @@ -86,15 +86,22 @@ final class PhabricatorUserOAuthSettingsPanelController ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' ID') - ->setValue($oauth_info->getOAuthUID())) + ->setValue($oauth_info->getOAuthUID()) + ) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' Name') - ->setValue($oauth_info->getAccountName())) + ->setValue($oauth_info->getAccountName()) + ) ->appendChild( id(new AphrontFormStaticControl()) ->setLabel($provider_name.' URI') - ->setValue($oauth_info->getAccountURI())); + ->setValue($oauth_info->getAccountURI()) + ) + ->appendChild( + id(new AphrontFormSubmitControl()) + ->setValue('Refresh Profile Image from '.$provider_name) + ); if (!$provider->isProviderLinkPermanent()) { $unlink = 'Unlink '.$provider_name.' Account'; @@ -157,7 +164,7 @@ final class PhabricatorUserOAuthSettingsPanelController $panel->setWidth(AphrontPanelView::WIDTH_FORM); foreach ($forms as $name => $form) { if ($name) { - $panel->appendChild('

'.$name.'

'); + $panel->appendChild('

'.$name.'


'); } $panel->appendChild($form); } @@ -169,4 +176,45 @@ final class PhabricatorUserOAuthSettingsPanelController $panel, )); } + + private function refreshProfileImage(PhabricatorUserOAuthInfo $oauth_info) { + $user = $this->getRequest()->getUser(); + $provider = $this->provider; + $error = false; + $userinfo_uri = new PhutilURI($provider->getUserInfoURI()); + try { + $userinfo_uri->setQueryParams( + array( + 'access_token' => $oauth_info->getToken(), + )); + $user_data = @file_get_contents($userinfo_uri); + $provider->setUserData($user_data); + $image = $provider->retrieveUserProfileImage(); + if ($image) { + $file = PhabricatorFile::newFromFileData( + $image, + array( + 'name' => $provider->getProviderKey().'-profile.jpg', + 'authorPHID' => $user->getPHID(), + )); + $user->setProfileImagePHID($file->getPHID()); + $user->save(); + } else { + $error = 'Unable to retrive image.'; + } + } catch (Exception $e) { + $error = 'Unable to save image.'; + } + $notice = new AphrontErrorView(); + if ($error) { + $notice + ->setTitle('Error Refreshing Profile Picture') + ->setErrors(array($error)); + } else { + $notice + ->setSeverity(AphrontErrorView::SEVERITY_NOTICE) + ->setTitle('Successfully Refreshed Profile Picture'); + } + return $notice; + } } diff --git a/src/applications/people/controller/settings/panels/oauth/__init__.php b/src/applications/people/controller/settings/panels/oauth/__init__.php index ab67b72259..d97e13a458 100644 --- a/src/applications/people/controller/settings/panels/oauth/__init__.php +++ b/src/applications/people/controller/settings/panels/oauth/__init__.php @@ -6,16 +6,19 @@ +phutil_require_module('phabricator', 'applications/files/storage/file'); phutil_require_module('phabricator', 'applications/people/controller/settings/panels/base'); phutil_require_module('phabricator', 'applications/people/storage/useroauthinfo'); phutil_require_module('phabricator', 'view/form/base'); phutil_require_module('phabricator', 'view/form/control/static'); phutil_require_module('phabricator', 'view/form/control/submit'); +phutil_require_module('phabricator', 'view/form/error'); phutil_require_module('phabricator', 'view/layout/panel'); phutil_require_module('phabricator', 'view/null'); phutil_require_module('phabricator', 'view/utils'); phutil_require_module('phutil', 'markup'); +phutil_require_module('phutil', 'parser/uri'); phutil_require_module('phutil', 'utils');