1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-21 11:09:02 +01:00

MetaMTA - add support for "Default Author"

Summary: Ref T5952. This adds support for a "default author" and deploys it on Maniphest.

Test Plan: used augmented (by this diff) bin/mail receive-test to test creation via an application email with a default author configured and no author specified. a task was created with the author as the default author i configured.

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T5952

Differential Revision: https://secure.phabricator.com/D11446
This commit is contained in:
Bob Trahan 2015-01-28 11:13:29 -08:00
parent 9b359affe7
commit 799dada3ad
7 changed files with 134 additions and 45 deletions

View file

@ -0,0 +1,22 @@
<?php
$key = 'metamta.maniphest.default-public-author';
echo "Migrating `$key` to new application email infrastructure...\n";
$value = PhabricatorEnv::getEnvConfigIfExists($key);
$maniphest = new PhabricatorManiphestApplication();
$config_key =
PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR;
if ($value) {
$app_emails = id(new PhabricatorMetaMTAApplicationEmailQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withApplicationPHIDs($maniphest->getPHID())
->execute();
foreach ($app_emails as $app_email) {
$app_email->setConfigValue($config_key, $value);
$app_email->save();
}
}
echo "Done.\n";

View file

@ -318,9 +318,18 @@ EOTEXT
'metamta.maniphest.default-public-author', 'metamta.maniphest.default-public-author',
'string', 'string',
null) null)
->setSummary(pht('Username anonymous bugs are filed under.')) ->setLocked(true)
->setLockedMessage(pht(
'This configuration is deprecated. See description for details.'))
->setSummary(pht(
'DEPRECATED - Username anonymous bugs are filed under.'))
->setDescription( ->setDescription(
pht( pht(
'This config has been deprecated in favor of [[ '.
'/applications/view/PhabricatorManiphestApplication/ | '.
'application settings ]], which allow for multiple email '.
'addresses each with its own default author, and other '.
'functionality.'."\n\n".
'If you enable `metamta.maniphest.public-create-email` and create '. 'If you enable `metamta.maniphest.public-create-email` and create '.
'an email address like "bugs@phabricator.example.com", it will '. 'an email address like "bugs@phabricator.example.com", it will '.
'default to rejecting mail which doesn\'t come from a known user. '. 'default to rejecting mail which doesn\'t come from a known user. '.

View file

@ -18,6 +18,7 @@ final class ManiphestCreateMailReceiver extends PhabricatorMailReceiver {
foreach ($application_emails as $application_email) { foreach ($application_emails as $application_email) {
$create_address = $application_email->getAddress(); $create_address = $application_email->getAddress();
if ($this->matchAddresses($create_address, $to_address)) { if ($this->matchAddresses($create_address, $to_address)) {
$this->setApplicationEmail($application_email);
return true; return true;
} }
} }
@ -26,42 +27,6 @@ final class ManiphestCreateMailReceiver extends PhabricatorMailReceiver {
return false; return false;
} }
public function loadSender(PhabricatorMetaMTAReceivedMail $mail) {
try {
// Try to load the sender normally.
return parent::loadSender($mail);
} catch (PhabricatorMetaMTAReceivedMailProcessingException $ex) {
// If we failed to load the sender normally, use this special legacy
// black magic.
// TODO: Deprecate and remove this.
$default_author_key = 'metamta.maniphest.default-public-author';
$default_author = PhabricatorEnv::getEnvConfig($default_author_key);
if (!strlen($default_author)) {
throw $ex;
}
$user = id(new PhabricatorUser())->loadOneWhere(
'username = %s',
$default_author);
if ($user) {
return $user;
}
throw new PhabricatorMetaMTAReceivedMailProcessingException(
MetaMTAReceivedMailStatus::STATUS_UNKNOWN_SENDER,
pht(
"Phabricator is misconfigured, the configuration key ".
"'metamta.maniphest.default-public-author' is set to user ".
"'%s' but that user does not exist.",
$default_author));
}
}
protected function processReceivedMail( protected function processReceivedMail(
PhabricatorMetaMTAReceivedMail $mail, PhabricatorMetaMTAReceivedMail $mail,
PhabricatorUser $sender) { PhabricatorUser $sender) {

View file

@ -220,10 +220,17 @@ final class PhabricatorApplicationEditEmailController
$e_email = true; $e_email = true;
$email = null; $email = null;
$errors = array(); $errors = array();
$default_user_key =
PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR;
if ($request->isDialogFormPost()) { if ($request->isDialogFormPost()) {
$email = trim($request->getStr('email')); $email = trim($request->getStr('email'));
list($e_email, $errors) = $this->validateApplicationEmail($email); list($e_email, $errors) = $this->validateApplicationEmail($email);
$email_object->setAddress($email); $email_object->setAddress($email);
$default_user = $request->getArr($default_user_key);
$default_user = reset($default_user);
if ($default_user) {
$email_object->setConfigValue($default_user_key, $default_user);
}
if (!$errors) { if (!$errors) {
try { try {
@ -244,6 +251,13 @@ final class PhabricatorApplicationEditEmailController
->setErrors($errors); ->setErrors($errors);
} }
$default_user = $email_object->getConfigValue($default_user_key);
if ($default_user) {
$default_user_handle = $this->loadViewerHandles(array($default_user));
} else {
$default_user_handle = array();
}
$form = id(new PHUIFormLayoutView()) $form = id(new PHUIFormLayoutView())
->appendChild( ->appendChild(
id(new AphrontFormTextControl()) id(new AphrontFormTextControl())
@ -251,11 +265,25 @@ final class PhabricatorApplicationEditEmailController
->setName('email') ->setName('email')
->setValue($email_object->getAddress()) ->setValue($email_object->getAddress())
->setCaption(PhabricatorUserEmail::describeAllowedAddresses()) ->setCaption(PhabricatorUserEmail::describeAllowedAddresses())
->setError($e_email)); ->setError($e_email))
->appendChild(
id(new AphrontFormTokenizerControl())
->setDatasource(new PhabricatorPeopleDatasource())
->setLabel(pht('Default Author'))
->setName($default_user_key)
->setLimit(1)
->setValue($default_user_handle)
->setCaption(pht(
'Used if the "From:" address does not map to a known account.')));
if ($is_new) {
$title = pht('New Address');
} else {
$title = pht('Edit Address');
}
$dialog = id(new AphrontDialogView()) $dialog = id(new AphrontDialogView())
->setUser($viewer) ->setUser($viewer)
->setTitle(pht('New Address')) ->setWidth(AphrontDialogView::WIDTH_FORM)
->setTitle($title)
->appendChild($errors) ->appendChild($errors)
->appendChild($form) ->appendChild($form)
->addSubmitButton(pht('Save')) ->addSubmitButton(pht('Save'))

View file

@ -35,7 +35,32 @@ final class PhabricatorMailManagementReceiveTestWorkflow
public function execute(PhutilArgumentParser $args) { public function execute(PhutilArgumentParser $args) {
$console = PhutilConsole::getConsole(); $console = PhutilConsole::getConsole();
$to = $args->getArg('to');
if (!$to) {
throw new PhutilArgumentUsageException(
"Use '--to' to specify the receiving object or email address.");
}
$to_application_email = id(new PhabricatorMetaMTAApplicationEmailQuery())
->setViewer($this->getViewer())
->withAddresses(array($to))
->executeOne();
$as = $args->getArg('as'); $as = $args->getArg('as');
if (!$as && $to_application_email) {
$default_phid = $to_application_email->getConfigValue(
PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR);
if ($default_phid) {
$default_user = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->withPHIDs(array($default_phid))
->executeOne();
if ($default_user) {
$as = $default_user->getUsername();
}
}
}
if (!$as) { if (!$as) {
throw new PhutilArgumentUsageException( throw new PhutilArgumentUsageException(
pht("Use '--as' to specify the acting user.")); pht("Use '--as' to specify the acting user."));
@ -50,11 +75,6 @@ final class PhabricatorMailManagementReceiveTestWorkflow
pht("No such user '%s' exists.", $as)); pht("No such user '%s' exists.", $as));
} }
$to = $args->getArg('to');
if (!$to) {
throw new PhutilArgumentUsageException(
"Use '--to' to specify the receiving object or email address.");
}
$from = $args->getArg('from'); $from = $args->getArg('from');
if (!$from) { if (!$from) {

View file

@ -2,9 +2,22 @@
abstract class PhabricatorMailReceiver { abstract class PhabricatorMailReceiver {
private $applicationEmail;
public function setApplicationEmail(
PhabricatorMetaMTAApplicationEmail $email) {
$this->applicationEmail = $email;
return $this;
}
public function getApplicationEmail() {
return $this->applicationEmail;
}
abstract public function isEnabled(); abstract public function isEnabled();
abstract public function canAcceptMail(PhabricatorMetaMTAReceivedMail $mail); abstract public function canAcceptMail(PhabricatorMetaMTAReceivedMail $mail);
abstract protected function processReceivedMail( abstract protected function processReceivedMail(
PhabricatorMetaMTAReceivedMail $mail, PhabricatorMetaMTAReceivedMail $mail,
PhabricatorUser $sender); PhabricatorUser $sender);
@ -128,6 +141,27 @@ abstract class PhabricatorMailReceiver {
$raw_from); $raw_from);
} }
if ($this->getApplicationEmail()) {
$application_email = $this->getApplicationEmail();
$default_user_phid = $application_email->getConfigValue(
PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR);
if ($default_user_phid) {
$user = id(new PhabricatorUser())->loadOneWhere(
'phid = %s',
$default_user_phid);
if ($user) {
return $user;
}
}
$reasons[] = pht(
"Phabricator is misconfigured, the application email ".
"'%s' is set to user '%s' but that user does not exist.",
$application_email->getAddress(),
$default_user_phid);
}
$reasons = implode("\n\n", $reasons); $reasons = implode("\n\n", $reasons);
throw new PhabricatorMetaMTAReceivedMailProcessingException( throw new PhabricatorMetaMTAReceivedMailProcessingException(

View file

@ -10,6 +10,8 @@ final class PhabricatorMetaMTAApplicationEmail
private $application = self::ATTACHABLE; private $application = self::ATTACHABLE;
const CONFIG_DEFAULT_AUTHOR = 'config:default:author';
protected function getConfiguration() { protected function getConfiguration() {
return array( return array(
self::CONFIG_AUX_PHID => true, self::CONFIG_AUX_PHID => true,
@ -50,6 +52,15 @@ final class PhabricatorMetaMTAApplicationEmail
return self::assertAttached($this->application); return self::assertAttached($this->application);
} }
public function setConfigValue($key, $value) {
$this->configData[$key] = $value;
return $this;
}
public function getConfigValue($key, $default = null) {
return idx($this->configData, $key, $default);
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */ /* -( PhabricatorPolicyInterface )----------------------------------------- */