PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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