Differential D16565 Diff 39942 src/applications/drydock/conduit/DrydockRequestAuthorizationConduitAPIMethod.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/conduit/DrydockRequestAuthorizationConduitAPIMethod.php
- This file was added.
<?php | |||||
final class DrydockRequestAuthorizationConduitAPIMethod | |||||
extends DrydockConduitAPIMethod { | |||||
public function getAPIMethodName() { | |||||
return 'drydock.requestauthorization'; | |||||
} | |||||
public function getMethodDescription() { | |||||
return pht('Request permission to use a Drydock blueprint.'); | |||||
} | |||||
protected function defineParamTypes() { | |||||
return array( | |||||
'blueprintPHID' => 'required phid', | |||||
); | |||||
} | |||||
protected function defineErrorTypes() { | |||||
return array( | |||||
'ERR_NO_BLUEPRINT' => pht( | |||||
'No active blueprint exists with the specified PHID.'), | |||||
); | |||||
} | |||||
protected function defineReturnType() { | |||||
return 'map<string, wild>'; | |||||
} | |||||
protected function execute(ConduitAPIRequest $request) { | |||||
$viewer = $request->getUser(); | |||||
$blueprint_phid = $request->getValue('blueprintPHID'); | |||||
$blueprint = id(new DrydockBlueprintQuery()) | |||||
->setViewer($viewer) | |||||
->withDisabled(false) | |||||
->withPHIDs(array($blueprint_phid)) | |||||
->executeOne(); | |||||
if (!$blueprint) { | |||||
throw new ConduitException('ERR_NO_BLUEPRINT'); | |||||
} | |||||
$authorizations = id(new DrydockAuthorizationQuery()) | |||||
->setViewer($viewer) | |||||
->withObjectPHIDs(array($viewer->getPHID())) | |||||
->withBlueprintStates(array( | |||||
DrydockAuthorization::BLUEPRINTAUTH_AUTHORIZED, | |||||
)) | |||||
->withObjectStates(array(DrydockAuthorization::OBJECTAUTH_ACTIVE)) | |||||
->execute(); | |||||
$authorizations = mpull($authorizations, null, 'getBlueprintPHID'); | |||||
$old = array_keys($authorizations); | |||||
if (!in_array($blueprint_phid, $old)) { | |||||
$new = array_mergev(array($old, array($blueprint_phid))); | |||||
DrydockAuthorization::applyAuthorizationChanges( | |||||
$viewer, | |||||
$viewer->getPHID(), | |||||
$old, | |||||
$new); | |||||
} | |||||
$search = id(new ConduitCall('drydock.authorization.search', array( | |||||
'constraints' => array( | |||||
'objectPHIDs' => array($viewer->getPHID()), | |||||
'blueprintPHIDs' => array($blueprint_phid), | |||||
), | |||||
))) | |||||
->setUser($viewer) | |||||
->execute(); | |||||
return idxv($search, array('data', 0)); | |||||
} | |||||
} |