2013-09-04 02:27:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DoorkeeperBridgeJIRA extends DoorkeeperBridge {
|
|
|
|
|
|
|
|
const APPTYPE_JIRA = 'jira';
|
|
|
|
const OBJTYPE_ISSUE = 'jira:issue';
|
|
|
|
|
|
|
|
public function canPullRef(DoorkeeperObjectRef $ref) {
|
|
|
|
if ($ref->getApplicationType() != self::APPTYPE_JIRA) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$types = array(
|
|
|
|
self::OBJTYPE_ISSUE => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
return isset($types[$ref->getObjectType()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pullRefs(array $refs) {
|
|
|
|
|
|
|
|
$id_map = mpull($refs, 'getObjectID', 'getObjectKey');
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
|
2014-07-22 13:18:15 +02:00
|
|
|
$provider = PhabricatorJIRAAuthProvider::getJIRAProvider();
|
2013-09-04 02:27:38 +02:00
|
|
|
if (!$provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$accounts = id(new PhabricatorExternalAccountQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withUserPHIDs(array($viewer->getPHID()))
|
|
|
|
->withAccountTypes(array($provider->getProviderType()))
|
Introduce CAN_EDIT for ExternalAccount, and make CAN_VIEW more liberal
Summary:
Fixes T3732. Ref T1205. Ref T3116.
External accounts (like emails used as identities, Facebook accounts, LDAP accounts, etc.) are stored in "ExternalAccount" objects.
Currently, we have a very restrictive `CAN_VIEW` policy for ExternalAccounts, to add an extra layer of protection to make sure users can't use them in unintended ways. For example, it would be bad if a user could link their Phabricator account to a Facebook account without proper authentication. All of the controllers which do sensitive things have checks anyway, but a restrictive CAN_VIEW provided an extra layer of protection. Se T3116 for some discussion.
However, this means that when grey/external users take actions (via email, or via applications like Legalpad) other users can't load the account handles and can't see anything about the actor (they just see "Restricted External Account" or similar).
Balancing these concerns is mostly about not making a huge mess while doing it. This seems like a reasonable approach:
- Add `CAN_EDIT` on these objects.
- Make that very restricted, but open up `CAN_VIEW`.
- Require `CAN_EDIT` any time we're going to do something authentication/identity related.
This is slightly easier to get wrong (forget CAN_EDIT) than other approaches, but pretty simple, and we always have extra checks in place anyway -- this is just a safety net.
I'm not quite sure how we should identify external accounts, so for now we're just rendering "Email User" or similar -- clearly not a bug, but not identifying. We can figure out what to render in the long term elsewhere.
Test Plan:
- Viewed external accounts.
- Linked an external account.
- Refreshed an external account.
- Edited profile picture.
- Viewed sessions panel.
- Published a bunch of stuff to Asana/JIRA.
- Legalpad signature page now shows external accounts.
{F171595}
Reviewers: chad, btrahan
Reviewed By: btrahan
Subscribers: epriestley
Maniphest Tasks: T3732, T1205, T3116
Differential Revision: https://secure.phabricator.com/D9767
2014-07-10 19:18:10 +02:00
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
2013-09-04 02:27:38 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
if (!$accounts) {
|
2014-04-02 21:03:59 +02:00
|
|
|
return $this->didFailOnMissingLink();
|
2013-09-04 02:27:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: When we support multiple JIRA instances, we need to disambiguate
|
|
|
|
// issues (perhaps with additional configuration) or cast a wide net
|
|
|
|
// (by querying all instances). For now, just query the one instance.
|
|
|
|
$account = head($accounts);
|
|
|
|
|
|
|
|
$futures = array();
|
|
|
|
foreach ($id_map as $key => $id) {
|
|
|
|
$futures[$key] = $provider->newJIRAFuture(
|
|
|
|
$account,
|
|
|
|
'rest/api/2/issue/'.phutil_escape_uri($id),
|
|
|
|
'GET');
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = array();
|
|
|
|
$failed = array();
|
2014-12-30 13:13:38 +01:00
|
|
|
foreach (new FutureIterator($futures) as $key => $future) {
|
2013-09-04 02:27:38 +02:00
|
|
|
try {
|
|
|
|
$results[$key] = $future->resolveJSON();
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
if (($ex instanceof HTTPFutureResponseStatus) &&
|
|
|
|
($ex->getStatusCode() == 404)) {
|
|
|
|
// This indicates that the object has been deleted (or never existed,
|
|
|
|
// or isn't visible to the current user) but it's a successful sync of
|
|
|
|
// an object which isn't visible.
|
|
|
|
} else {
|
|
|
|
// This is something else, so consider it a synchronization failure.
|
|
|
|
phlog($ex);
|
|
|
|
$failed[$key] = $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($refs as $ref) {
|
|
|
|
$ref->setAttribute('name', pht('JIRA %s', $ref->getObjectID()));
|
|
|
|
|
|
|
|
$did_fail = idx($failed, $ref->getObjectKey());
|
|
|
|
if ($did_fail) {
|
|
|
|
$ref->setSyncFailed(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = idx($results, $ref->getObjectKey());
|
|
|
|
if (!$result) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = idx($result, 'fields', array());
|
|
|
|
|
|
|
|
$ref->setIsVisible(true);
|
|
|
|
$ref->setAttribute(
|
|
|
|
'fullname',
|
|
|
|
pht('JIRA %s %s', $result['key'], idx($fields, 'summary')));
|
|
|
|
|
|
|
|
$ref->setAttribute('title', idx($fields, 'summary'));
|
|
|
|
$ref->setAttribute('description', idx($result, 'description'));
|
|
|
|
|
|
|
|
$obj = $ref->getExternalObject();
|
|
|
|
if ($obj->getID()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fillObjectFromData($obj, $result);
|
|
|
|
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
$obj->save();
|
|
|
|
unset($unguarded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fillObjectFromData(DoorkeeperExternalObject $obj, $result) {
|
|
|
|
// Convert the "self" URI, which points at the REST endpoint, into a
|
|
|
|
// browse URI.
|
|
|
|
$self = idx($result, 'self');
|
2014-05-18 14:45:21 +02:00
|
|
|
$object_id = $obj->getObjectID();
|
2013-09-04 02:27:38 +02:00
|
|
|
|
2014-05-18 14:45:21 +02:00
|
|
|
$uri = self::getJIRAIssueBrowseURIFromJIRARestURI($self, $object_id);
|
|
|
|
if ($uri !== null) {
|
|
|
|
$obj->setObjectURI($uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getJIRAIssueBrowseURIFromJIRARestURI(
|
|
|
|
$uri,
|
|
|
|
$object_id) {
|
|
|
|
|
|
|
|
$uri = new PhutilURI($uri);
|
|
|
|
|
|
|
|
// The JIRA install might not be at the domain root, so we may need to
|
|
|
|
// keep an initial part of the path, like "/jira/". Find the API specific
|
|
|
|
// part of the URI, strip it off, then replace it with the web version.
|
|
|
|
$path = $uri->getPath();
|
|
|
|
$pos = strrpos($path, 'rest/api/2/issue/');
|
|
|
|
if ($pos === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = substr($path, 0, $pos);
|
|
|
|
$path = $path.'browse/'.$object_id;
|
|
|
|
$uri->setPath($path);
|
|
|
|
|
|
|
|
return (string)$uri;
|
2013-09-04 02:27:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|