コード例 #1
0
ファイル: User.php プロジェクト: redcrackle/redtest-core
 /**
  * Register a new user.
  *
  * @param string $username
  *   Username.
  * @param string $email
  *   Email address.
  * @param string $password
  *   Password.
  * @param array $options
  *   Options array. Usually this will have the following 3 keys:
  *   (a) roles: An array of roles that the newsly created user has to be
  *   assigned.
  *   (b) skip: An array of fields that need to be skipped during
  *   registration.
  *   (c) required_fields_only: Whether only required fields in the
  *   registration field are to be filled.
  *
  * @return mixed $user
  *   User object if the user logged in successfully and an array of errors,
  *   otherwise.
  */
 public static function registerUser($username, $email, $password, $options = array())
 {
     $options += array('roles' => array(), 'skip' => array(), 'required_fields_only' => TRUE);
     $userRegisterForm = new UserForms\UserRegisterForm();
     $userRegisterForm->fillFieldValues(array('account', 'name'), $username);
     $userRegisterForm->fillFieldValues(array('account', 'mail'), $email);
     $userRegisterForm->fillFieldValues(array('pass', 'pass1'), $password);
     $userRegisterForm->fillFieldValues(array('pass', 'pass2'), $password);
     $userObject = new User();
     $field_instances = $userObject->getFieldInstances();
     $fields = array();
     foreach ($field_instances as $field_name => $field_info) {
         if (!$field_info['settings']['user_register_form']) {
             continue;
         }
         if (!in_array($field_name, $options['skip']) && ($userRegisterForm->isRequired($field_name) || $field_info['required'] || !$options['required_fields_only'])) {
             $function = 'fill' . Utils::makeTitleCase($field_name) . 'RandomValues';
             $response = $userRegisterForm->{$function}();
             if (!$response->getSuccess()) {
                 $response->setVar($fields);
                 return $response;
             }
             $fields[$field_name] = $response->getVar();
         }
     }
     $response = $userRegisterForm->submit();
     if (!$response->getSuccess()) {
         return $response;
     }
     /**
      * @todo Find a better way to make the user active and add roles than using user_save().
      */
     try {
         $roles = self::formatRoles($options['roles']);
     } catch (\Exception $e) {
         return new Response(FALSE, NULL, $e->getMessage());
     }
     $userObject = $response->getVar();
     if (!$userObject->getStatusValues() || sizeof($roles)) {
         $account = $userObject->getEntity();
         $edit['status'] = TRUE;
         $edit['roles'] = $account->roles + $roles;
         $account = user_save($account, $edit);
         if (!$account) {
             return new Response(FALSE, NULL, "Could not make the user active or could not add roles.");
         }
         $userObject = new User(Utils::getId($userObject));
     }
     // Add password key so that it can be used later to log in.
     $form_state = $userRegisterForm->getFormState();
     $account = $userObject->getEntity();
     $account->password = $form_state['user']->password;
     $userObject->setEntity($account);
     return new Response(TRUE, $userObject, "");
 }
コード例 #2
0
ファイル: User.php プロジェクト: vishalred/redtest-core-pw
 /**
  * Register a new user.
  *
  * @param string $username
  *   Username.
  * @param string $email
  *   Email address.
  * @param string $password
  *   Password.
  * @param array $roles
  *   An array of roles that are to be added to the user in addition to the
  *   default role(s) that the user gets on registering. You can either pass
  *   in role id or role string.
  *
  * @return mixed $user
  *   User object if the user logged in successfully and an array of errors,
  *   otherwise.
  */
 public static function registerUser($username, $email, $password, $roles = array())
 {
     $userRegisterForm = new UserForms\UserRegisterForm();
     $userRegisterForm->fillFieldValues(array('account', 'name'), $username);
     $userRegisterForm->fillFieldValues(array('account', 'mail'), $email);
     $userRegisterForm->fillFieldValues(array('pass', 'pass1'), $password);
     $userRegisterForm->fillFieldValues(array('pass', 'pass2'), $password);
     $response = $userRegisterForm->submit();
     if (!$response->getSuccess()) {
         return $response;
     }
     /**
      * @todo Find a better way to make the user active and add roles than using user_save().
      */
     $roles = self::formatRoles($roles);
     $userObject = $response->getVar();
     if (!$userObject->getStatusValues() || sizeof($roles)) {
         $account = $userObject->getEntity();
         $edit['status'] = TRUE;
         $edit['roles'] = $account->roles + $roles;
         $account = user_save($account, $edit);
         if (!$account) {
             return new Response(FALSE, NULL, "Could not make the user active or could not add roles.");
         }
         $userObject = new User(Utils::getId($userObject));
     }
     // Add password key so that it can be used later to log in.
     $form_state = $userRegisterForm->getFormState();
     $account = $userObject->getEntity();
     $account->password = $form_state['user']->password;
     $userObject->setEntity($account);
     return new Response(TRUE, $userObject, "");
 }