コード例 #1
0
 private function processBasicRequest(PhabricatorUser $user)
 {
     $request = $this->getRequest();
     $admin = $request->getUser();
     $e_username = true;
     $e_realname = true;
     $e_email = true;
     $errors = array();
     $welcome_checked = true;
     $new_email = null;
     $request = $this->getRequest();
     if ($request->isFormPost()) {
         $welcome_checked = $request->getInt('welcome');
         if (!$user->getID()) {
             $user->setUsername($request->getStr('username'));
             $new_email = $request->getStr('email');
             if (!strlen($new_email)) {
                 $errors[] = 'Email is required.';
                 $e_email = 'Required';
             } else {
                 if (!PhabricatorUserEmail::isAllowedAddress($new_email)) {
                     $e_email = 'Invalid';
                     $errors[] = PhabricatorUserEmail::describeAllowedAddresses();
                 } else {
                     $e_email = null;
                 }
             }
             if ($request->getStr('role') == 'agent') {
                 $user->setIsSystemAgent(true);
             }
         }
         $user->setRealName($request->getStr('realname'));
         if (!strlen($user->getUsername())) {
             $errors[] = "Username is required.";
             $e_username = '******';
         } else {
             if (!PhabricatorUser::validateUsername($user->getUsername())) {
                 $errors[] = PhabricatorUser::describeValidUsername();
                 $e_username = '******';
             } else {
                 $e_username = null;
             }
         }
         if (!strlen($user->getRealName())) {
             $errors[] = 'Real name is required.';
             $e_realname = 'Required';
         } else {
             $e_realname = null;
         }
         if (!$errors) {
             try {
                 $is_new = !$user->getID();
                 if (!$is_new) {
                     id(new PhabricatorUserEditor())->setActor($admin)->updateUser($user);
                 } else {
                     $email = id(new PhabricatorUserEmail())->setAddress($new_email)->setIsVerified(0);
                     id(new PhabricatorUserEditor())->setActor($admin)->createNewUser($user, $email);
                 }
                 if ($welcome_checked) {
                     $user->sendWelcomeEmail($admin);
                 }
                 $response = id(new AphrontRedirectResponse())->setURI('/people/edit/' . $user->getID() . '/?saved=true');
                 return $response;
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $errors[] = 'Username and email must be unique.';
                 $same_username = id(new PhabricatorUser())->loadOneWhere('username = %s', $user->getUsername());
                 $same_email = id(new PhabricatorUserEmail())->loadOneWhere('address = %s', $new_email);
                 if ($same_username) {
                     $e_username = '******';
                 }
                 if ($same_email) {
                     $e_email = 'Duplicate';
                 }
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = id(new AphrontErrorView())->setTitle('Form Errors')->setErrors($errors);
     }
     $form = new AphrontFormView();
     $form->setUser($admin);
     if ($user->getID()) {
         $form->setAction('/people/edit/' . $user->getID() . '/');
     } else {
         $form->setAction('/people/edit/');
     }
     if ($user->getID()) {
         $is_immutable = true;
     } else {
         $is_immutable = false;
     }
     $form->appendChild(id(new AphrontFormTextControl())->setLabel('Username')->setName('username')->setValue($user->getUsername())->setError($e_username)->setDisabled($is_immutable))->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setValue($user->getRealName())->setError($e_realname));
     if (!$user->getID()) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setDisabled($is_immutable)->setValue($new_email)->setCaption(PhabricatorUserEmail::describeAllowedAddresses())->setError($e_email));
     } else {
         $email = $user->loadPrimaryEmail();
         if ($email) {
             $status = $email->getIsVerified() ? 'Verified' : 'Unverified';
         } else {
             $status = 'No Email Address';
         }
         $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Email')->setValue($status));
         $form->appendChild(id(new AphrontFormCheckboxControl())->addCheckbox('welcome', 1, 'Re-send "Welcome to Phabricator" email.', false));
     }
     $form->appendChild($this->getRoleInstructions());
     if (!$user->getID()) {
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Role')->setName('role')->setValue('user')->setOptions(array('user' => 'Normal User', 'agent' => 'System Agent'))->setCaption('You can create a "system agent" account for bots, scripts, ' . 'etc.'))->appendChild(id(new AphrontFormCheckboxControl())->addCheckbox('welcome', 1, 'Send "Welcome to Phabricator" email.', $welcome_checked));
     } else {
         $roles = array();
         if ($user->getIsSystemAgent()) {
             $roles[] = 'System Agent';
         }
         if ($user->getIsAdmin()) {
             $roles[] = 'Admin';
         }
         if ($user->getIsDisabled()) {
             $roles[] = 'Disabled';
         }
         if (!$roles) {
             $roles[] = 'Normal User';
         }
         $roles = implode(', ', $roles);
         $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Roles')->setValue($roles));
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     $panel = new AphrontPanelView();
     if ($user->getID()) {
         $panel->setHeader('Edit User');
     } else {
         $panel->setHeader('Create New User');
     }
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return array($error_view, $panel);
 }
コード例 #2
0
 private function processBasicRequest(PhabricatorUser $user)
 {
     $request = $this->getRequest();
     $admin = $request->getUser();
     $e_username = true;
     $e_realname = true;
     $e_email = true;
     $errors = array();
     $welcome_checked = true;
     $request = $this->getRequest();
     if ($request->isFormPost()) {
         $welcome_checked = $request->getInt('welcome');
         if (!$user->getID()) {
             $user->setUsername($request->getStr('username'));
             $user->setEmail($request->getStr('email'));
             if ($request->getStr('role') == 'agent') {
                 $user->setIsSystemAgent(true);
             }
         }
         $user->setRealName($request->getStr('realname'));
         if (!strlen($user->getUsername())) {
             $errors[] = "Username is required.";
             $e_username = '******';
         } else {
             if (!PhabricatorUser::validateUsername($user->getUsername())) {
                 $errors[] = "Username must consist of only numbers and letters.";
                 $e_username = '******';
             } else {
                 $e_username = null;
             }
         }
         if (!strlen($user->getRealName())) {
             $errors[] = 'Real name is required.';
             $e_realname = 'Required';
         } else {
             $e_realname = null;
         }
         if (!strlen($user->getEmail())) {
             $errors[] = 'Email is required.';
             $e_email = 'Required';
         } else {
             $e_email = null;
         }
         if (!$errors) {
             try {
                 $is_new = !$user->getID();
                 $user->save();
                 if ($is_new) {
                     $log = PhabricatorUserLog::newLog($admin, $user, PhabricatorUserLog::ACTION_CREATE);
                     $log->save();
                     if ($welcome_checked) {
                         $user->sendWelcomeEmail($admin);
                     }
                 }
                 $response = id(new AphrontRedirectResponse())->setURI('/people/edit/' . $user->getID() . '/?saved=true');
                 return $response;
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $errors[] = 'Username and email must be unique.';
                 $same_username = id(new PhabricatorUser())->loadOneWhere('username = %s', $user->getUsername());
                 $same_email = id(new PhabricatorUser())->loadOneWhere('email = %s', $user->getEmail());
                 if ($same_username) {
                     $e_username = '******';
                 }
                 if ($same_email) {
                     $e_email = 'Duplicate';
                 }
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = id(new AphrontErrorView())->setTitle('Form Errors')->setErrors($errors);
     }
     $form = new AphrontFormView();
     $form->setUser($admin);
     if ($user->getID()) {
         $form->setAction('/people/edit/' . $user->getID() . '/');
     } else {
         $form->setAction('/people/edit/');
     }
     if ($user->getID()) {
         $is_immutable = true;
     } else {
         $is_immutable = false;
     }
     $form->appendChild(id(new AphrontFormTextControl())->setLabel('Username')->setName('username')->setValue($user->getUsername())->setError($e_username)->setDisabled($is_immutable)->setCaption('Usernames are permanent and can not be changed later!'))->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setValue($user->getRealName())->setError($e_realname))->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setDisabled($is_immutable)->setValue($user->getEmail())->setError($e_email))->appendChild($this->getRoleInstructions());
     if (!$user->getID()) {
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Role')->setName('role')->setValue('user')->setOptions(array('user' => 'Normal User', 'agent' => 'System Agent'))->setCaption('You can create a "system agent" account for bots, scripts, ' . 'etc.'))->appendChild(id(new AphrontFormCheckboxControl())->addCheckbox('welcome', 1, 'Send "Welcome to Phabricator" email.', $welcome_checked));
     } else {
         $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Role')->setValue($user->getIsSystemAgent() ? 'System Agent' : 'Normal User'));
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     $panel = new AphrontPanelView();
     if ($user->getID()) {
         $panel->setHeader('Edit User');
     } else {
         $panel->setHeader('Create New User');
     }
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return array($error_view, $panel);
 }
コード例 #3
0
 /**
  * @task role
  */
 public function makeSystemAgentUser(PhabricatorUser $user, $system_agent)
 {
     $actor = $this->requireActor();
     if (!$user->getID()) {
         throw new Exception('User has not been created yet!');
     }
     $user->openTransaction();
     $user->beginWriteLocking();
     $user->reload();
     if ($user->getIsSystemAgent() == $system_agent) {
         $user->endWriteLocking();
         $user->killTransaction();
         return $this;
     }
     $log = PhabricatorUserLog::initializeNewLog($actor, $user->getPHID(), PhabricatorUserLog::ACTION_SYSTEM_AGENT);
     $log->setOldValue($user->getIsSystemAgent());
     $log->setNewValue($system_agent);
     $user->setIsSystemAgent((int) $system_agent);
     $user->save();
     $log->save();
     $user->endWriteLocking();
     $user->saveTransaction();
     return $this;
 }
コード例 #4
0
    private function processBasicRequest(PhabricatorUser $user)
    {
        $request = $this->getRequest();
        $admin = $request->getUser();
        $e_username = true;
        $e_realname = true;
        $e_email = true;
        $errors = array();
        $welcome_checked = true;
        $request = $this->getRequest();
        if ($request->isFormPost()) {
            $welcome_checked = $request->getInt('welcome');
            if (!$user->getID()) {
                $user->setUsername($request->getStr('username'));
                $user->setEmail($request->getStr('email'));
                if ($request->getStr('role') == 'agent') {
                    $user->setIsSystemAgent(true);
                }
            }
            $user->setRealName($request->getStr('realname'));
            if (!strlen($user->getUsername())) {
                $errors[] = "Username is required.";
                $e_username = '******';
            } else {
                if (!preg_match('/^[a-z0-9]+$/', $user->getUsername())) {
                    $errors[] = "Username must consist of only numbers and letters.";
                    $e_username = '******';
                } else {
                    $e_username = null;
                }
            }
            if (!strlen($user->getRealName())) {
                $errors[] = 'Real name is required.';
                $e_realname = 'Required';
            } else {
                $e_realname = null;
            }
            if (!strlen($user->getEmail())) {
                $errors[] = 'Email is required.';
                $e_email = 'Required';
            } else {
                $e_email = null;
            }
            if (!$errors) {
                try {
                    $is_new = !$user->getID();
                    $user->save();
                    if ($is_new) {
                        $log = PhabricatorUserLog::newLog($admin, $user, PhabricatorUserLog::ACTION_CREATE);
                        $log->save();
                        if ($welcome_checked) {
                            $admin_username = $admin->getUserName();
                            $admin_realname = $admin->getRealName();
                            $user_username = $user->getUserName();
                            $base_uri = PhabricatorEnv::getProductionURI('/');
                            $uri = $user->getEmailLoginURI();
                            $body = <<<EOBODY
Welcome to Phabricator!

{$admin_username} ({$admin_realname}) has created an account for you.

  Username: {$user_username}

To login to Phabricator, follow this link and set a password:

  {$uri}

After you have set a password, you can login in the future by going here:

  {$base_uri}

Love,
Phabricator

EOBODY;
                            $mail = id(new PhabricatorMetaMTAMail())->addTos(array($user->getPHID()))->setSubject('[Phabricator] Welcome to Phabricator')->setBody($body)->setFrom($admin->getPHID())->saveAndSend();
                        }
                    }
                    $response = id(new AphrontRedirectResponse())->setURI('/people/edit/' . $user->getID() . '/?saved=true');
                    return $response;
                } catch (AphrontQueryDuplicateKeyException $ex) {
                    $errors[] = 'Username and email must be unique.';
                    $same_username = id(new PhabricatorUser())->loadOneWhere('username = %s', $user->getUsername());
                    $same_email = id(new PhabricatorUser())->loadOneWhere('email = %s', $user->getEmail());
                    if ($same_username) {
                        $e_username = '******';
                    }
                    if ($same_email) {
                        $e_email = 'Duplicate';
                    }
                }
            }
        }
        $error_view = null;
        if ($errors) {
            $error_view = id(new AphrontErrorView())->setTitle('Form Errors')->setErrors($errors);
        }
        $form = new AphrontFormView();
        $form->setUser($admin);
        if ($user->getID()) {
            $form->setAction('/people/edit/' . $user->getID() . '/');
        } else {
            $form->setAction('/people/edit/');
        }
        if ($user->getID()) {
            $is_immutable = true;
        } else {
            $is_immutable = false;
        }
        $form->appendChild(id(new AphrontFormTextControl())->setLabel('Username')->setName('username')->setValue($user->getUsername())->setError($e_username)->setDisabled($is_immutable)->setCaption('Usernames are permanent and can not be changed later!'))->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setValue($user->getRealName())->setError($e_realname))->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setDisabled($is_immutable)->setValue($user->getEmail())->setError($e_email));
        if (!$user->getID()) {
            $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Role')->setName('role')->setValue('user')->setOptions(array('user' => 'Normal User', 'agent' => 'System Agent'))->setCaption('You can create a "system agent" account for bots, scripts, ' . 'etc.'))->appendChild(id(new AphrontFormCheckboxControl())->addCheckbox('welcome', 1, 'Send "Welcome to Phabricator" email.', $welcome_checked));
        } else {
            $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Role')->setValue($user->getIsSystemAgent() ? 'System Agent' : 'Normal User'));
        }
        $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
        $panel = new AphrontPanelView();
        if ($user->getID()) {
            $panel->setHeader('Edit User');
        } else {
            $panel->setHeader('Create New User');
        }
        $panel->appendChild($form);
        $panel->setWidth(AphrontPanelView::WIDTH_FORM);
        return array($error_view, $panel);
    }