[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorSettingsPanelEmailAddresses 4 extends PhabricatorSettingsPanel { 5 6 public function getPanelKey() { 7 return 'email'; 8 } 9 10 public function getPanelName() { 11 return pht('Email Addresses'); 12 } 13 14 public function getPanelGroup() { 15 return pht('Email'); 16 } 17 18 public function processRequest(AphrontRequest $request) { 19 $user = $request->getUser(); 20 $editable = PhabricatorEnv::getEnvConfig('account.editable'); 21 22 $uri = $request->getRequestURI(); 23 $uri->setQueryParams(array()); 24 25 if ($editable) { 26 $new = $request->getStr('new'); 27 if ($new) { 28 return $this->returnNewAddressResponse($request, $uri, $new); 29 } 30 31 $delete = $request->getInt('delete'); 32 if ($delete) { 33 return $this->returnDeleteAddressResponse($request, $uri, $delete); 34 } 35 } 36 37 $verify = $request->getInt('verify'); 38 if ($verify) { 39 return $this->returnVerifyAddressResponse($request, $uri, $verify); 40 } 41 42 $primary = $request->getInt('primary'); 43 if ($primary) { 44 return $this->returnPrimaryAddressResponse($request, $uri, $primary); 45 } 46 47 $emails = id(new PhabricatorUserEmail())->loadAllWhere( 48 'userPHID = %s ORDER BY address', 49 $user->getPHID()); 50 51 $rowc = array(); 52 $rows = array(); 53 foreach ($emails as $email) { 54 55 $button_verify = javelin_tag( 56 'a', 57 array( 58 'class' => 'button small grey', 59 'href' => $uri->alter('verify', $email->getID()), 60 'sigil' => 'workflow', 61 ), 62 pht('Verify')); 63 64 $button_make_primary = javelin_tag( 65 'a', 66 array( 67 'class' => 'button small grey', 68 'href' => $uri->alter('primary', $email->getID()), 69 'sigil' => 'workflow', 70 ), 71 pht('Make Primary')); 72 73 $button_remove = javelin_tag( 74 'a', 75 array( 76 'class' => 'button small grey', 77 'href' => $uri->alter('delete', $email->getID()), 78 'sigil' => 'workflow', 79 ), 80 pht('Remove')); 81 82 $button_primary = phutil_tag( 83 'a', 84 array( 85 'class' => 'button small disabled', 86 ), 87 pht('Primary')); 88 89 if (!$email->getIsVerified()) { 90 $action = $button_verify; 91 } else if ($email->getIsPrimary()) { 92 $action = $button_primary; 93 } else { 94 $action = $button_make_primary; 95 } 96 97 if ($email->getIsPrimary()) { 98 $remove = $button_primary; 99 $rowc[] = 'highlighted'; 100 } else { 101 $remove = $button_remove; 102 $rowc[] = null; 103 } 104 105 $rows[] = array( 106 $email->getAddress(), 107 $action, 108 $remove, 109 ); 110 } 111 112 $table = new AphrontTableView($rows); 113 $table->setHeaders( 114 array( 115 pht('Email'), 116 pht('Status'), 117 pht('Remove'), 118 )); 119 $table->setColumnClasses( 120 array( 121 'wide', 122 'action', 123 'action', 124 )); 125 $table->setRowClasses($rowc); 126 $table->setColumnVisibility( 127 array( 128 true, 129 true, 130 $editable, 131 )); 132 133 $view = new PHUIObjectBoxView(); 134 $header = new PHUIHeaderView(); 135 $header->setHeader(pht('Email Addresses')); 136 137 if ($editable) { 138 $icon = id(new PHUIIconView()) 139 ->setIconFont('fa-plus'); 140 141 $button = new PHUIButtonView(); 142 $button->setText(pht('Add New Address')); 143 $button->setTag('a'); 144 $button->setHref($uri->alter('new', 'true')); 145 $button->setIcon($icon); 146 $button->addSigil('workflow'); 147 $header->addActionLink($button); 148 } 149 $view->setHeader($header); 150 $view->appendChild($table); 151 152 return $view; 153 } 154 155 private function returnNewAddressResponse( 156 AphrontRequest $request, 157 PhutilURI $uri, 158 $new) { 159 160 $user = $request->getUser(); 161 162 $e_email = true; 163 $email = null; 164 $errors = array(); 165 if ($request->isDialogFormPost()) { 166 $email = trim($request->getStr('email')); 167 168 if ($new == 'verify') { 169 // The user clicked "Done" from the "an email has been sent" dialog. 170 return id(new AphrontReloadResponse())->setURI($uri); 171 } 172 173 PhabricatorSystemActionEngine::willTakeAction( 174 array($user->getPHID()), 175 new PhabricatorSettingsAddEmailAction(), 176 1); 177 178 if (!strlen($email)) { 179 $e_email = pht('Required'); 180 $errors[] = pht('Email is required.'); 181 } else if (!PhabricatorUserEmail::isValidAddress($email)) { 182 $e_email = pht('Invalid'); 183 $errors[] = PhabricatorUserEmail::describeValidAddresses(); 184 } else if (!PhabricatorUserEmail::isAllowedAddress($email)) { 185 $e_email = pht('Disallowed'); 186 $errors[] = PhabricatorUserEmail::describeAllowedAddresses(); 187 } 188 189 if (!$errors) { 190 $object = id(new PhabricatorUserEmail()) 191 ->setAddress($email) 192 ->setIsVerified(0); 193 194 try { 195 196 id(new PhabricatorUserEditor()) 197 ->setActor($user) 198 ->addEmail($user, $object); 199 200 $object->sendVerificationEmail($user); 201 202 $dialog = id(new AphrontDialogView()) 203 ->setUser($user) 204 ->addHiddenInput('new', 'verify') 205 ->setTitle(pht('Verification Email Sent')) 206 ->appendChild(phutil_tag('p', array(), pht( 207 'A verification email has been sent. Click the link in the '. 208 'email to verify your address.'))) 209 ->setSubmitURI($uri) 210 ->addSubmitButton(pht('Done')); 211 212 return id(new AphrontDialogResponse())->setDialog($dialog); 213 } catch (AphrontDuplicateKeyQueryException $ex) { 214 $e_email = pht('Duplicate'); 215 $errors[] = pht('Another user already has this email.'); 216 } 217 } 218 } 219 220 if ($errors) { 221 $errors = id(new AphrontErrorView()) 222 ->setErrors($errors); 223 } 224 225 $form = id(new PHUIFormLayoutView()) 226 ->appendChild( 227 id(new AphrontFormTextControl()) 228 ->setLabel(pht('Email')) 229 ->setName('email') 230 ->setValue($email) 231 ->setCaption(PhabricatorUserEmail::describeAllowedAddresses()) 232 ->setError($e_email)); 233 234 $dialog = id(new AphrontDialogView()) 235 ->setUser($user) 236 ->addHiddenInput('new', 'true') 237 ->setTitle(pht('New Address')) 238 ->appendChild($errors) 239 ->appendChild($form) 240 ->addSubmitButton(pht('Save')) 241 ->addCancelButton($uri); 242 243 return id(new AphrontDialogResponse())->setDialog($dialog); 244 } 245 246 private function returnDeleteAddressResponse( 247 AphrontRequest $request, 248 PhutilURI $uri, 249 $email_id) { 250 251 $user = $request->getUser(); 252 253 // NOTE: You can only delete your own email addresses, and you can not 254 // delete your primary address. 255 $email = id(new PhabricatorUserEmail())->loadOneWhere( 256 'id = %d AND userPHID = %s AND isPrimary = 0', 257 $email_id, 258 $user->getPHID()); 259 260 if (!$email) { 261 return new Aphront404Response(); 262 } 263 264 if ($request->isFormPost()) { 265 266 id(new PhabricatorUserEditor()) 267 ->setActor($user) 268 ->removeEmail($user, $email); 269 270 return id(new AphrontRedirectResponse())->setURI($uri); 271 } 272 273 $address = $email->getAddress(); 274 275 $dialog = id(new AphrontDialogView()) 276 ->setUser($user) 277 ->addHiddenInput('delete', $email_id) 278 ->setTitle(pht("Really delete address '%s'?", $address)) 279 ->appendParagraph( 280 pht( 281 'Are you sure you want to delete this address? You will no '. 282 'longer be able to use it to login.')) 283 ->appendParagraph( 284 pht( 285 'Note: Removing an email address from your account will invalidate '. 286 'any outstanding password reset links.')) 287 ->addSubmitButton(pht('Delete')) 288 ->addCancelButton($uri); 289 290 return id(new AphrontDialogResponse())->setDialog($dialog); 291 } 292 293 private function returnVerifyAddressResponse( 294 AphrontRequest $request, 295 PhutilURI $uri, 296 $email_id) { 297 298 $user = $request->getUser(); 299 300 // NOTE: You can only send more email for your unverified addresses. 301 $email = id(new PhabricatorUserEmail())->loadOneWhere( 302 'id = %d AND userPHID = %s AND isVerified = 0', 303 $email_id, 304 $user->getPHID()); 305 306 if (!$email) { 307 return new Aphront404Response(); 308 } 309 310 if ($request->isFormPost()) { 311 $email->sendVerificationEmail($user); 312 return id(new AphrontRedirectResponse())->setURI($uri); 313 } 314 315 $address = $email->getAddress(); 316 317 $dialog = id(new AphrontDialogView()) 318 ->setUser($user) 319 ->addHiddenInput('verify', $email_id) 320 ->setTitle(pht('Send Another Verification Email?')) 321 ->appendChild(phutil_tag('p', array(), pht( 322 'Send another copy of the verification email to %s?', 323 $address))) 324 ->addSubmitButton(pht('Send Email')) 325 ->addCancelButton($uri); 326 327 return id(new AphrontDialogResponse())->setDialog($dialog); 328 } 329 330 private function returnPrimaryAddressResponse( 331 AphrontRequest $request, 332 PhutilURI $uri, 333 $email_id) { 334 335 $user = $request->getUser(); 336 337 $token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession( 338 $user, 339 $request, 340 $this->getPanelURI()); 341 342 // NOTE: You can only make your own verified addresses primary. 343 $email = id(new PhabricatorUserEmail())->loadOneWhere( 344 'id = %d AND userPHID = %s AND isVerified = 1 AND isPrimary = 0', 345 $email_id, 346 $user->getPHID()); 347 348 if (!$email) { 349 return new Aphront404Response(); 350 } 351 352 if ($request->isFormPost()) { 353 354 id(new PhabricatorUserEditor()) 355 ->setActor($user) 356 ->changePrimaryEmail($user, $email); 357 358 return id(new AphrontRedirectResponse())->setURI($uri); 359 } 360 361 $address = $email->getAddress(); 362 363 $dialog = id(new AphrontDialogView()) 364 ->setUser($user) 365 ->addHiddenInput('primary', $email_id) 366 ->setTitle(pht('Change primary email address?')) 367 ->appendParagraph( 368 pht( 369 'If you change your primary address, Phabricator will send all '. 370 'email to %s.', 371 $address)) 372 ->appendParagraph( 373 pht( 374 'Note: Changing your primary email address will invalidate any '. 375 'outstanding password reset links.')) 376 ->addSubmitButton(pht('Change Primary Address')) 377 ->addCancelButton($uri); 378 379 return id(new AphrontDialogResponse())->setDialog($dialog); 380 } 381 382 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |