PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/people/controller/PhabricatorPeopleNewController.php

http://github.com/facebook/phabricator
PHP | 240 lines | 201 code | 37 blank | 2 comment | 31 complexity | 19496fd97553fb5ec7bb2eda3e9d619b MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class PhabricatorPeopleNewController
  3. extends PhabricatorPeopleController {
  4. public function handleRequest(AphrontRequest $request) {
  5. $type = $request->getURIData('type');
  6. $admin = $request->getUser();
  7. id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
  8. $admin,
  9. $request,
  10. $this->getApplicationURI());
  11. $is_bot = false;
  12. $is_list = false;
  13. switch ($type) {
  14. case 'standard':
  15. $this->requireApplicationCapability(
  16. PeopleCreateUsersCapability::CAPABILITY);
  17. break;
  18. case 'bot':
  19. $is_bot = true;
  20. break;
  21. case 'list':
  22. $is_list = true;
  23. break;
  24. default:
  25. return new Aphront404Response();
  26. }
  27. $user = new PhabricatorUser();
  28. $require_real_name = PhabricatorEnv::getEnvConfig('user.require-real-name');
  29. $e_username = true;
  30. $e_realname = $require_real_name ? true : null;
  31. $e_email = true;
  32. $errors = array();
  33. $welcome_checked = true;
  34. $new_email = null;
  35. if ($request->isFormPost()) {
  36. $welcome_checked = $request->getInt('welcome');
  37. $user->setUsername($request->getStr('username'));
  38. $new_email = $request->getStr('email');
  39. if (!strlen($new_email)) {
  40. $errors[] = pht('Email is required.');
  41. $e_email = pht('Required');
  42. } else if (!PhabricatorUserEmail::isAllowedAddress($new_email)) {
  43. $e_email = pht('Invalid');
  44. $errors[] = PhabricatorUserEmail::describeAllowedAddresses();
  45. } else {
  46. $e_email = null;
  47. }
  48. $user->setRealName($request->getStr('realname'));
  49. if (!strlen($user->getUsername())) {
  50. $errors[] = pht('Username is required.');
  51. $e_username = pht('Required');
  52. } else if (!PhabricatorUser::validateUsername($user->getUsername())) {
  53. $errors[] = PhabricatorUser::describeValidUsername();
  54. $e_username = pht('Invalid');
  55. } else {
  56. $e_username = null;
  57. }
  58. if (!strlen($user->getRealName()) && $require_real_name) {
  59. $errors[] = pht('Real name is required.');
  60. $e_realname = pht('Required');
  61. } else {
  62. $e_realname = null;
  63. }
  64. if (!$errors) {
  65. try {
  66. $email = id(new PhabricatorUserEmail())
  67. ->setAddress($new_email)
  68. ->setIsVerified(0);
  69. // Automatically approve the user, since an admin is creating them.
  70. $user->setIsApproved(1);
  71. // If the user is a bot or list, approve their email too.
  72. if ($is_bot || $is_list) {
  73. $email->setIsVerified(1);
  74. }
  75. id(new PhabricatorUserEditor())
  76. ->setActor($admin)
  77. ->createNewUser($user, $email);
  78. if ($is_bot) {
  79. id(new PhabricatorUserEditor())
  80. ->setActor($admin)
  81. ->makeSystemAgentUser($user, true);
  82. }
  83. if ($is_list) {
  84. id(new PhabricatorUserEditor())
  85. ->setActor($admin)
  86. ->makeMailingListUser($user, true);
  87. }
  88. if ($welcome_checked) {
  89. $welcome_engine = id(new PhabricatorPeopleWelcomeMailEngine())
  90. ->setSender($admin)
  91. ->setRecipient($user);
  92. if ($welcome_engine->canSendMail()) {
  93. $welcome_engine->sendMail();
  94. }
  95. }
  96. $response = id(new AphrontRedirectResponse())
  97. ->setURI('/p/'.$user->getUsername().'/');
  98. return $response;
  99. } catch (AphrontDuplicateKeyQueryException $ex) {
  100. $errors[] = pht('Username and email must be unique.');
  101. $same_username = id(new PhabricatorUser())
  102. ->loadOneWhere('username = %s', $user->getUsername());
  103. $same_email = id(new PhabricatorUserEmail())
  104. ->loadOneWhere('address = %s', $new_email);
  105. if ($same_username) {
  106. $e_username = pht('Duplicate');
  107. }
  108. if ($same_email) {
  109. $e_email = pht('Duplicate');
  110. }
  111. }
  112. }
  113. }
  114. $form = id(new AphrontFormView())
  115. ->setUser($admin);
  116. if ($is_bot) {
  117. $title = pht('Create New Bot');
  118. $form->appendRemarkupInstructions(
  119. pht('You are creating a new **bot** user account.'));
  120. } else if ($is_list) {
  121. $title = pht('Create New Mailing List');
  122. $form->appendRemarkupInstructions(
  123. pht('You are creating a new **mailing list** user account.'));
  124. } else {
  125. $title = pht('Create New User');
  126. $form->appendRemarkupInstructions(
  127. pht('You are creating a new **standard** user account.'));
  128. }
  129. $form
  130. ->appendChild(
  131. id(new AphrontFormTextControl())
  132. ->setLabel(pht('Username'))
  133. ->setName('username')
  134. ->setValue($user->getUsername())
  135. ->setError($e_username))
  136. ->appendChild(
  137. id(new AphrontFormTextControl())
  138. ->setLabel(pht('Real Name'))
  139. ->setName('realname')
  140. ->setValue($user->getRealName())
  141. ->setError($e_realname))
  142. ->appendChild(
  143. id(new AphrontFormTextControl())
  144. ->setLabel(pht('Email'))
  145. ->setName('email')
  146. ->setValue($new_email)
  147. ->setCaption(PhabricatorUserEmail::describeAllowedAddresses())
  148. ->setError($e_email));
  149. if (!$is_bot && !$is_list) {
  150. $form->appendChild(
  151. id(new AphrontFormCheckboxControl())
  152. ->addCheckbox(
  153. 'welcome',
  154. 1,
  155. pht('Send "Welcome to Phabricator" email with login instructions.'),
  156. $welcome_checked));
  157. }
  158. $form
  159. ->appendChild(
  160. id(new AphrontFormSubmitControl())
  161. ->addCancelButton($this->getApplicationURI())
  162. ->setValue(pht('Create User')));
  163. if ($is_bot) {
  164. $form
  165. ->appendChild(id(new AphrontFormDividerControl()))
  166. ->appendRemarkupInstructions(
  167. pht(
  168. '**Why do bot accounts need an email address?**'.
  169. "\n\n".
  170. 'Although bots do not normally receive email from Phabricator, '.
  171. 'they can interact with other systems which require an email '.
  172. 'address. Examples include:'.
  173. "\n\n".
  174. " - If the account takes actions which //send// email, we need ".
  175. " an address to use in the //From// header.\n".
  176. " - If the account creates commits, Git and Mercurial require ".
  177. " an email address for authorship.\n".
  178. " - If you send email //to// Phabricator on behalf of the ".
  179. " account, the address can identify the sender.\n".
  180. " - Some internal authentication functions depend on accounts ".
  181. " having an email address.\n".
  182. "\n\n".
  183. "The address will automatically be verified, so you do not need ".
  184. "to be able to receive mail at this address, and can enter some ".
  185. "invalid or nonexistent (but correctly formatted) address like ".
  186. "`bot@yourcompany.com` if you prefer."));
  187. }
  188. $box = id(new PHUIObjectBoxView())
  189. ->setHeaderText($title)
  190. ->setFormErrors($errors)
  191. ->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
  192. ->setForm($form);
  193. $crumbs = $this->buildApplicationCrumbs();
  194. $crumbs->addTextCrumb($title);
  195. $crumbs->setBorder(true);
  196. $view = id(new PHUITwoColumnView())
  197. ->setFooter($box);
  198. return $this->newPage()
  199. ->setTitle($title)
  200. ->setCrumbs($crumbs)
  201. ->appendChild($view);
  202. }
  203. }