2013-06-17 10:52:38 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
final class PhabricatorAuthEditController
|
|
|
|
|
extends PhabricatorAuthProviderConfigController {
|
|
|
|
|
|
|
|
|
|
private $providerClass;
|
|
|
|
|
private $configID;
|
|
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
|
$this->providerClass = idx($data, 'className');
|
|
|
|
|
$this->configID = idx($data, 'id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$viewer = $request->getUser();
|
|
|
|
|
|
|
|
|
|
if ($this->configID) {
|
|
|
|
|
$config = id(new PhabricatorAuthProviderConfigQuery())
|
|
|
|
|
->setViewer($viewer)
|
|
|
|
|
->requireCapabilities(
|
|
|
|
|
array(
|
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
|
))
|
|
|
|
|
->withIDs(array($this->configID))
|
|
|
|
|
->executeOne();
|
|
|
|
|
if (!$config) {
|
|
|
|
|
return new Aphront404Response();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:53:29 -07:00
|
|
|
$provider = $config->getProvider();
|
|
|
|
|
if (!$provider) {
|
|
|
|
|
return new Aphront404Response();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$is_new = false;
|
|
|
|
|
} else {
|
|
|
|
|
$providers = PhabricatorAuthProvider::getAllBaseProviders();
|
|
|
|
|
foreach ($providers as $candidate_provider) {
|
|
|
|
|
if (get_class($candidate_provider) === $this->providerClass) {
|
|
|
|
|
$provider = $candidate_provider;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$provider) {
|
|
|
|
|
return new Aphront404Response();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: When we have multi-auth providers, support them here.
|
|
|
|
|
|
|
|
|
|
$configs = id(new PhabricatorAuthProviderConfigQuery())
|
|
|
|
|
->setViewer($viewer)
|
|
|
|
|
->withProviderClasses(array(get_class($provider)))
|
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
|
|
if ($configs) {
|
2013-06-20 14:13:53 -07:00
|
|
|
$id = head($configs)->getID();
|
|
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
|
->setUser($viewer)
|
|
|
|
|
->setMethod('GET')
|
|
|
|
|
->setSubmitURI($this->getApplicationURI('config/edit/'.$id.'/'))
|
|
|
|
|
->setTitle(pht('Provider Already Configured'))
|
|
|
|
|
->appendChild(
|
|
|
|
|
pht(
|
|
|
|
|
'This provider ("%s") already exists, and you can not add more '.
|
|
|
|
|
'than one instance of it. You can edit the existing provider, '.
|
|
|
|
|
'or you can choose a different provider.',
|
|
|
|
|
$provider->getProviderName()))
|
|
|
|
|
->addCancelButton($this->getApplicationURI('config/new/'))
|
|
|
|
|
->addSubmitButton(pht('Edit Existing Provider'));
|
|
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2013-06-17 10:52:38 -07:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 11:17:53 -07:00
|
|
|
$config = $provider->getDefaultProviderConfig();
|
|
|
|
|
$provider->attachProviderConfig($config);
|
2013-06-17 10:52:38 -07:00
|
|
|
|
|
|
|
|
$is_new = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
|
|
$v_registration = $config->getShouldAllowRegistration();
|
|
|
|
|
$v_link = $config->getShouldAllowLink();
|
|
|
|
|
$v_unlink = $config->getShouldAllowUnlink();
|
|
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
2013-06-18 10:02:34 -07:00
|
|
|
|
|
|
|
|
$properties = $provider->readFormValuesFromRequest($request);
|
|
|
|
|
list($errors, $issues, $properties) = $provider->processEditForm(
|
|
|
|
|
$request,
|
|
|
|
|
$properties);
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$xactions = array();
|
|
|
|
|
|
2013-06-18 10:02:34 -07:00
|
|
|
if (!$errors) {
|
|
|
|
|
if ($is_new) {
|
|
|
|
|
$config->setProviderType($provider->getProviderType());
|
|
|
|
|
$config->setProviderDomain($provider->getProviderDomain());
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$xactions[] = id(new PhabricatorAuthProviderConfigTransaction())
|
|
|
|
|
->setTransactionType(
|
2013-06-18 10:02:34 -07:00
|
|
|
PhabricatorAuthProviderConfigTransaction::TYPE_REGISTRATION)
|
|
|
|
|
->setNewValue($request->getInt('allowRegistration', 0));
|
2013-06-17 10:52:38 -07:00
|
|
|
|
2013-06-18 10:02:34 -07:00
|
|
|
$xactions[] = id(new PhabricatorAuthProviderConfigTransaction())
|
|
|
|
|
->setTransactionType(
|
|
|
|
|
PhabricatorAuthProviderConfigTransaction::TYPE_LINK)
|
|
|
|
|
->setNewValue($request->getInt('allowLink', 0));
|
2013-06-17 10:52:38 -07:00
|
|
|
|
2013-06-18 10:02:34 -07:00
|
|
|
$xactions[] = id(new PhabricatorAuthProviderConfigTransaction())
|
|
|
|
|
->setTransactionType(
|
|
|
|
|
PhabricatorAuthProviderConfigTransaction::TYPE_UNLINK)
|
|
|
|
|
->setNewValue($request->getInt('allowUnlink', 0));
|
|
|
|
|
|
|
|
|
|
foreach ($properties as $key => $value) {
|
|
|
|
|
$xactions[] = id(new PhabricatorAuthProviderConfigTransaction())
|
|
|
|
|
->setTransactionType(
|
|
|
|
|
PhabricatorAuthProviderConfigTransaction::TYPE_PROPERTY)
|
|
|
|
|
->setMetadataValue('auth:property', $key)
|
|
|
|
|
->setNewValue($value);
|
|
|
|
|
}
|
2013-06-17 10:52:38 -07:00
|
|
|
|
2013-06-20 11:23:58 -07:00
|
|
|
if ($is_new) {
|
|
|
|
|
$config->save();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$editor = id(new PhabricatorAuthProviderConfigEditor())
|
|
|
|
|
->setActor($viewer)
|
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
|
->setContinueOnNoEffect(true)
|
|
|
|
|
->applyTransactions($config, $xactions);
|
|
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI(
|
|
|
|
|
$this->getApplicationURI());
|
|
|
|
|
}
|
2013-06-18 10:02:34 -07:00
|
|
|
} else {
|
|
|
|
|
$properties = $provider->readFormValuesFromProvider();
|
|
|
|
|
$issues = array();
|
2013-06-17 10:52:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
|
$errors = id(new AphrontErrorView())->setErrors($errors);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($is_new) {
|
|
|
|
|
$button = pht('Add Provider');
|
|
|
|
|
$crumb = pht('Add Provider');
|
|
|
|
|
$title = pht('Add Authentication Provider');
|
|
|
|
|
$cancel_uri = $this->getApplicationURI('/config/new/');
|
|
|
|
|
} else {
|
|
|
|
|
$button = pht('Save');
|
|
|
|
|
$crumb = pht('Edit Provider');
|
|
|
|
|
$title = pht('Edit Authentication Provider');
|
|
|
|
|
$cancel_uri = $this->getApplicationURI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$str_registration = hsprintf(
|
|
|
|
|
'<strong>%s:</strong> %s',
|
|
|
|
|
pht('Allow Registration'),
|
|
|
|
|
pht(
|
|
|
|
|
'Allow users to register new Phabricator accounts using this '.
|
|
|
|
|
'provider. If you disable registration, users can still use this '.
|
|
|
|
|
'provider to log in to existing accounts, but will not be able to '.
|
|
|
|
|
'create new accounts.'));
|
|
|
|
|
|
|
|
|
|
$str_link = hsprintf(
|
|
|
|
|
'<strong>%s:</strong> %s',
|
|
|
|
|
pht('Allow Linking Accounts'),
|
|
|
|
|
pht(
|
|
|
|
|
'Allow users to link account credentials for this provider to '.
|
|
|
|
|
'existing Phabricator accounts. There is normally no reason to '.
|
|
|
|
|
'disable this unless you are trying to move away from a provider '.
|
|
|
|
|
'and want to stop users from creating new account links.'));
|
|
|
|
|
|
|
|
|
|
$str_unlink = hsprintf(
|
|
|
|
|
'<strong>%s:</strong> %s',
|
|
|
|
|
pht('Allow Unlinking Accounts'),
|
|
|
|
|
pht(
|
|
|
|
|
'Allow users to unlink account credentials for this provider from '.
|
|
|
|
|
'existing Phabricator accounts. If you disable this, Phabricator '.
|
|
|
|
|
'accounts will be permanently bound to provider accounts.'));
|
|
|
|
|
|
2013-06-17 10:54:08 -07:00
|
|
|
$status_tag = id(new PhabricatorTagView())
|
|
|
|
|
->setType(PhabricatorTagView::TYPE_STATE);
|
2013-06-20 11:17:53 -07:00
|
|
|
if ($is_new) {
|
|
|
|
|
$status_tag
|
|
|
|
|
->setName(pht('New Provider'))
|
|
|
|
|
->setBackgroundColor('blue');
|
|
|
|
|
} else if ($config->getIsEnabled()) {
|
2013-06-17 10:54:08 -07:00
|
|
|
$status_tag
|
|
|
|
|
->setName(pht('Enabled'))
|
|
|
|
|
->setBackgroundColor('green');
|
|
|
|
|
} else {
|
|
|
|
|
$status_tag
|
|
|
|
|
->setName(pht('Disabled'))
|
|
|
|
|
->setBackgroundColor('red');
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
|
->setUser($viewer)
|
|
|
|
|
->setFlexible(true)
|
|
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
|
->setLabel(pht('Provider'))
|
|
|
|
|
->setValue($provider->getProviderName()))
|
2013-06-17 10:54:08 -07:00
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
|
->setLabel(pht('Status'))
|
|
|
|
|
->setValue($status_tag))
|
2013-06-17 10:52:38 -07:00
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormCheckboxControl())
|
|
|
|
|
->setLabel(pht('Allow'))
|
|
|
|
|
->addCheckbox(
|
|
|
|
|
'allowRegistration',
|
|
|
|
|
1,
|
|
|
|
|
$str_registration,
|
|
|
|
|
$v_registration))
|
|
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormCheckboxControl())
|
|
|
|
|
->addCheckbox(
|
|
|
|
|
'allowLink',
|
|
|
|
|
1,
|
|
|
|
|
$str_link,
|
|
|
|
|
$v_link))
|
|
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormCheckboxControl())
|
|
|
|
|
->addCheckbox(
|
|
|
|
|
'allowUnlink',
|
|
|
|
|
1,
|
|
|
|
|
$str_unlink,
|
|
|
|
|
$v_unlink));
|
|
|
|
|
|
2013-06-18 10:02:34 -07:00
|
|
|
$provider->extendEditForm($request, $form, $properties, $issues);
|
2013-06-17 10:52:38 -07:00
|
|
|
|
|
|
|
|
$form
|
|
|
|
|
->appendChild(
|
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
|
->addCancelButton($cancel_uri)
|
|
|
|
|
->setValue($button));
|
|
|
|
|
|
2013-06-20 11:18:48 -07:00
|
|
|
$help = $provider->getConfigurationHelp();
|
|
|
|
|
if ($help) {
|
|
|
|
|
$form->appendChild(id(new PHUIFormDividerControl()));
|
|
|
|
|
$form->appendRemarkupInstructions($help);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:52:38 -07:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
|
->setName($crumb));
|
|
|
|
|
|
|
|
|
|
$xaction_view = null;
|
|
|
|
|
if (!$is_new) {
|
2013-06-17 10:53:29 -07:00
|
|
|
$xactions = id(new PhabricatorAuthProviderConfigTransactionQuery())
|
|
|
|
|
->withObjectPHIDs(array($config->getPHID()))
|
|
|
|
|
->setViewer($viewer)
|
|
|
|
|
->execute();
|
|
|
|
|
|
2013-06-18 10:02:34 -07:00
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
|
$xaction->setProvider($provider);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 10:53:29 -07:00
|
|
|
$xaction_view = id(new PhabricatorApplicationTransactionView())
|
|
|
|
|
->setUser($viewer)
|
2013-07-28 18:21:22 -07:00
|
|
|
->setObjectPHID($config->getPHID())
|
2013-06-17 10:53:29 -07:00
|
|
|
->setTransactions($xactions);
|
2013-06-17 10:52:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
|
array(
|
|
|
|
|
$crumbs,
|
|
|
|
|
$errors,
|
|
|
|
|
$form,
|
2013-06-17 10:53:29 -07:00
|
|
|
$xaction_view,
|
2013-06-17 10:52:38 -07:00
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'dust' => true,
|
|
|
|
|
'device' => true,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|