diff --git a/resources/sql/autopatches/20150120.maniphestdefaultauthor.php b/resources/sql/autopatches/20150120.maniphestdefaultauthor.php new file mode 100644 index 0000000000..5aae97f863 --- /dev/null +++ b/resources/sql/autopatches/20150120.maniphestdefaultauthor.php @@ -0,0 +1,22 @@ +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"; diff --git a/src/applications/maniphest/config/PhabricatorManiphestConfigOptions.php b/src/applications/maniphest/config/PhabricatorManiphestConfigOptions.php index 337ebd8e43..167161f07a 100644 --- a/src/applications/maniphest/config/PhabricatorManiphestConfigOptions.php +++ b/src/applications/maniphest/config/PhabricatorManiphestConfigOptions.php @@ -318,9 +318,18 @@ EOTEXT 'metamta.maniphest.default-public-author', 'string', 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( 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 '. 'an email address like "bugs@phabricator.example.com", it will '. 'default to rejecting mail which doesn\'t come from a known user. '. diff --git a/src/applications/maniphest/mail/ManiphestCreateMailReceiver.php b/src/applications/maniphest/mail/ManiphestCreateMailReceiver.php index 3f7c132cb9..fecfca9410 100644 --- a/src/applications/maniphest/mail/ManiphestCreateMailReceiver.php +++ b/src/applications/maniphest/mail/ManiphestCreateMailReceiver.php @@ -18,6 +18,7 @@ final class ManiphestCreateMailReceiver extends PhabricatorMailReceiver { foreach ($application_emails as $application_email) { $create_address = $application_email->getAddress(); if ($this->matchAddresses($create_address, $to_address)) { + $this->setApplicationEmail($application_email); return true; } } @@ -26,42 +27,6 @@ final class ManiphestCreateMailReceiver extends PhabricatorMailReceiver { 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( PhabricatorMetaMTAReceivedMail $mail, PhabricatorUser $sender) { diff --git a/src/applications/meta/controller/PhabricatorApplicationEditEmailController.php b/src/applications/meta/controller/PhabricatorApplicationEditEmailController.php index 052da430c1..8b37289e1a 100644 --- a/src/applications/meta/controller/PhabricatorApplicationEditEmailController.php +++ b/src/applications/meta/controller/PhabricatorApplicationEditEmailController.php @@ -220,10 +220,17 @@ final class PhabricatorApplicationEditEmailController $e_email = true; $email = null; $errors = array(); + $default_user_key = + PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR; if ($request->isDialogFormPost()) { $email = trim($request->getStr('email')); list($e_email, $errors) = $this->validateApplicationEmail($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) { try { @@ -244,6 +251,13 @@ final class PhabricatorApplicationEditEmailController ->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()) ->appendChild( id(new AphrontFormTextControl()) @@ -251,11 +265,25 @@ final class PhabricatorApplicationEditEmailController ->setName('email') ->setValue($email_object->getAddress()) ->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()) ->setUser($viewer) - ->setTitle(pht('New Address')) + ->setWidth(AphrontDialogView::WIDTH_FORM) + ->setTitle($title) ->appendChild($errors) ->appendChild($form) ->addSubmitButton(pht('Save')) diff --git a/src/applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php b/src/applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php index ebaf76817c..2ee5fdf886 100644 --- a/src/applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php +++ b/src/applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php @@ -35,7 +35,32 @@ final class PhabricatorMailManagementReceiveTestWorkflow public function execute(PhutilArgumentParser $args) { $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'); + 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) { throw new PhutilArgumentUsageException( pht("Use '--as' to specify the acting user.")); @@ -50,11 +75,6 @@ final class PhabricatorMailManagementReceiveTestWorkflow 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'); if (!$from) { diff --git a/src/applications/metamta/receiver/PhabricatorMailReceiver.php b/src/applications/metamta/receiver/PhabricatorMailReceiver.php index 2a17034fd7..ee309ffe0a 100644 --- a/src/applications/metamta/receiver/PhabricatorMailReceiver.php +++ b/src/applications/metamta/receiver/PhabricatorMailReceiver.php @@ -2,9 +2,22 @@ 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 canAcceptMail(PhabricatorMetaMTAReceivedMail $mail); + abstract protected function processReceivedMail( PhabricatorMetaMTAReceivedMail $mail, PhabricatorUser $sender); @@ -128,6 +141,27 @@ abstract class PhabricatorMailReceiver { $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); throw new PhabricatorMetaMTAReceivedMailProcessingException( diff --git a/src/applications/metamta/storage/PhabricatorMetaMTAApplicationEmail.php b/src/applications/metamta/storage/PhabricatorMetaMTAApplicationEmail.php index c001b4b70c..0cec7f1950 100644 --- a/src/applications/metamta/storage/PhabricatorMetaMTAApplicationEmail.php +++ b/src/applications/metamta/storage/PhabricatorMetaMTAApplicationEmail.php @@ -10,6 +10,8 @@ final class PhabricatorMetaMTAApplicationEmail private $application = self::ATTACHABLE; + const CONFIG_DEFAULT_AUTHOR = 'config:default:author'; + protected function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, @@ -50,6 +52,15 @@ final class PhabricatorMetaMTAApplicationEmail 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 )----------------------------------------- */