Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / metamta / editor / PhabricatorMetaMTAApplicationEmailEditor.php
blob843e653039f3d6b6cd40d0c20d9a761e8bd4f626
1 <?php
3 final class PhabricatorMetaMTAApplicationEmailEditor
4 extends PhabricatorApplicationTransactionEditor {
6 public function getEditorApplicationClass() {
7 return pht('PhabricatorMetaMTAApplication');
10 public function getEditorObjectsDescription() {
11 return pht('Application Emails');
14 public function getTransactionTypes() {
15 $types = parent::getTransactionTypes();
17 $types[] = PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS;
18 $types[] = PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG;
20 return $types;
23 protected function getCustomTransactionOldValue(
24 PhabricatorLiskDAO $object,
25 PhabricatorApplicationTransaction $xaction) {
27 switch ($xaction->getTransactionType()) {
28 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS:
29 return $object->getAddress();
30 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG:
31 $key = $xaction->getMetadataValue(
32 PhabricatorMetaMTAApplicationEmailTransaction::KEY_CONFIG);
33 return $object->getConfigValue($key);
36 return parent::getCustomTransactionOldValue($object, $xaction);
39 protected function getCustomTransactionNewValue(
40 PhabricatorLiskDAO $object,
41 PhabricatorApplicationTransaction $xaction) {
43 switch ($xaction->getTransactionType()) {
44 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS:
45 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG:
46 return $xaction->getNewValue();
49 return parent::getCustomTransactionNewValue($object, $xaction);
52 protected function applyCustomInternalTransaction(
53 PhabricatorLiskDAO $object,
54 PhabricatorApplicationTransaction $xaction) {
56 $new = $xaction->getNewValue();
58 switch ($xaction->getTransactionType()) {
59 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS:
60 $object->setAddress($new);
61 return;
62 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG:
63 $key = $xaction->getMetadataValue(
64 PhabricatorMetaMTAApplicationEmailTransaction::KEY_CONFIG);
65 $object->setConfigValue($key, $new);
66 return;
69 return parent::applyCustomInternalTransaction($object, $xaction);
72 protected function applyCustomExternalTransaction(
73 PhabricatorLiskDAO $object,
74 PhabricatorApplicationTransaction $xaction) {
76 switch ($xaction->getTransactionType()) {
77 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS:
78 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG:
79 return;
82 return parent::applyCustomExternalTransaction($object, $xaction);
85 protected function validateTransaction(
86 PhabricatorLiskDAO $object,
87 $type,
88 array $xactions) {
90 $errors = parent::validateTransaction($object, $type, $xactions);
92 switch ($type) {
93 case PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS:
94 foreach ($xactions as $xaction) {
95 $email = $xaction->getNewValue();
96 if (!strlen($email)) {
97 // We'll deal with this below.
98 continue;
101 if (!PhabricatorUserEmail::isValidAddress($email)) {
102 $errors[] = new PhabricatorApplicationTransactionValidationError(
103 $type,
104 pht('Invalid'),
105 pht('Email address is not formatted properly.'));
106 continue;
109 $address = new PhutilEmailAddress($email);
110 if (PhabricatorMailUtil::isReservedAddress($address)) {
111 $errors[] = new PhabricatorApplicationTransactionValidationError(
112 $type,
113 pht('Reserved'),
114 pht(
115 'This email address is reserved. Choose a different '.
116 'address.'));
117 continue;
120 // See T13234. Prevent use of user email addresses as application
121 // email addresses.
122 if (PhabricatorMailUtil::isUserAddress($address)) {
123 $errors[] = new PhabricatorApplicationTransactionValidationError(
124 $type,
125 pht('In Use'),
126 pht(
127 'This email address is already in use by a user. Choose '.
128 'a different address.'));
129 continue;
133 $missing = $this->validateIsEmptyTextField(
134 $object->getAddress(),
135 $xactions);
137 if ($missing) {
138 $error = new PhabricatorApplicationTransactionValidationError(
139 $type,
140 pht('Required'),
141 pht('You must provide an email address.'),
142 nonempty(last($xactions), null));
144 $error->setIsMissingFieldError(true);
145 $errors[] = $error;
147 break;
150 return $errors;
153 protected function didCatchDuplicateKeyException(
154 PhabricatorLiskDAO $object,
155 array $xactions,
156 Exception $ex) {
158 $errors = array();
159 $errors[] = new PhabricatorApplicationTransactionValidationError(
160 PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS,
161 pht('Duplicate'),
162 pht('This email address is already in use.'),
163 null);
165 throw new PhabricatorApplicationTransactionValidationException($errors);