[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/people/controller/ -> PhabricatorPeopleNewController.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1