1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-22 20:51:10 +01:00

T870 - add a refresh button for sync'd OAuth accounts

Summary: nice title!

Test Plan: refreshed my profile pic against my OAuth Phabricator instance.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, epriestley

Maniphest Tasks: T870

Differential Revision: https://secure.phabricator.com/D1980
This commit is contained in:
Bob Trahan 2012-03-21 16:10:50 -07:00
parent 4fba549a99
commit 5eb922fdb4
2 changed files with 65 additions and 14 deletions

View file

@ -27,24 +27,24 @@ final class PhabricatorUserOAuthSettingsPanelController
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest();
$request = $this->getRequest(); $user = $request->getUser();
$user = $request->getUser(); $provider = $this->provider;
$provider = $this->provider; $notice = null;
$notice = null;
$provider_name = $provider->getProviderName(); $provider_name = $provider->getProviderName();
$provider_key = $provider->getProviderKey(); $provider_key = $provider->getProviderKey();
$oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere( $oauth_info = id(new PhabricatorUserOAuthInfo())->loadOneWhere(
'userID = %d AND oauthProvider = %s', 'userID = %d AND oauthProvider = %s',
$user->getID(), $user->getID(),
$provider->getProviderKey()); $provider->getProviderKey());
if ($request->isFormPost() && $oauth_info) {
$notice = $this->refreshProfileImage($oauth_info);
}
$form = new AphrontFormView(); $form = new AphrontFormView();
$form $form->setUser($user);
->setUser($user);
$forms = array(); $forms = array();
$forms[] = $form; $forms[] = $form;
@ -86,15 +86,22 @@ final class PhabricatorUserOAuthSettingsPanelController
->appendChild( ->appendChild(
id(new AphrontFormStaticControl()) id(new AphrontFormStaticControl())
->setLabel($provider_name.' ID') ->setLabel($provider_name.' ID')
->setValue($oauth_info->getOAuthUID())) ->setValue($oauth_info->getOAuthUID())
)
->appendChild( ->appendChild(
id(new AphrontFormStaticControl()) id(new AphrontFormStaticControl())
->setLabel($provider_name.' Name') ->setLabel($provider_name.' Name')
->setValue($oauth_info->getAccountName())) ->setValue($oauth_info->getAccountName())
)
->appendChild( ->appendChild(
id(new AphrontFormStaticControl()) id(new AphrontFormStaticControl())
->setLabel($provider_name.' URI') ->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()) { if (!$provider->isProviderLinkPermanent()) {
$unlink = 'Unlink '.$provider_name.' Account'; $unlink = 'Unlink '.$provider_name.' Account';
@ -157,7 +164,7 @@ final class PhabricatorUserOAuthSettingsPanelController
$panel->setWidth(AphrontPanelView::WIDTH_FORM); $panel->setWidth(AphrontPanelView::WIDTH_FORM);
foreach ($forms as $name => $form) { foreach ($forms as $name => $form) {
if ($name) { if ($name) {
$panel->appendChild('<br /><br /><h1>'.$name.'</h1>'); $panel->appendChild('<br /><h1>'.$name.'</h1><br />');
} }
$panel->appendChild($form); $panel->appendChild($form);
} }
@ -169,4 +176,45 @@ final class PhabricatorUserOAuthSettingsPanelController
$panel, $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;
}
} }

View file

@ -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/controller/settings/panels/base');
phutil_require_module('phabricator', 'applications/people/storage/useroauthinfo'); phutil_require_module('phabricator', 'applications/people/storage/useroauthinfo');
phutil_require_module('phabricator', 'view/form/base'); phutil_require_module('phabricator', 'view/form/base');
phutil_require_module('phabricator', 'view/form/control/static'); phutil_require_module('phabricator', 'view/form/control/static');
phutil_require_module('phabricator', 'view/form/control/submit'); 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/layout/panel');
phutil_require_module('phabricator', 'view/null'); phutil_require_module('phabricator', 'view/null');
phutil_require_module('phabricator', 'view/utils'); phutil_require_module('phabricator', 'view/utils');
phutil_require_module('phutil', 'markup'); phutil_require_module('phutil', 'markup');
phutil_require_module('phutil', 'parser/uri');
phutil_require_module('phutil', 'utils'); phutil_require_module('phutil', 'utils');