You are here

protected function AccountRegistration::incomingPatternMessage in SMS Framework 8

Same name and namespace in other branches
  1. 2.x modules/sms_user/src/AccountRegistration.php \Drupal\sms_user\AccountRegistration::incomingPatternMessage()
  2. 2.1.x modules/sms_user/src/AccountRegistration.php \Drupal\sms_user\AccountRegistration::incomingPatternMessage()

Creates a user if an incoming message contents matches a pattern.

Parameters

\Drupal\sms\Message\SmsMessageInterface $sms_message: An incoming SMS message.

1 call to AccountRegistration::incomingPatternMessage()
AccountRegistration::createAccount in modules/sms_user/src/AccountRegistration.php
Process an incoming SMS to see if a new account should be created.

File

modules/sms_user/src/AccountRegistration.php, line 156

Class

AccountRegistration
Defines the account registration service.

Namespace

Drupal\sms_user

Code

protected function incomingPatternMessage(SmsMessageInterface $sms_message) {
  if (!empty($this
    ->settings('incoming_pattern.incoming_messages.0'))) {
    $incoming_form = $this
      ->settings('incoming_pattern.incoming_messages.0');
    $incoming_form = str_replace("\r\n", "\n", $incoming_form);
    $compiled = $this
      ->compileFormRegex($incoming_form, '/');
    $matches = [];
    if (preg_match_all('/^' . $compiled . '$/', $sms_message
      ->getMessage(), $matches)) {
      $contains_email = strpos($incoming_form, '[email]') !== FALSE;
      $contains_username = strpos($incoming_form, '[username]') !== FALSE;
      $contains_password = strpos($incoming_form, '[password]') !== FALSE;
      $username = !empty($matches['username'][0]) && $contains_username ? $matches['username'][0] : $this
        ->generateUniqueUsername();
      $user = User::create([
        'name' => $username,
      ]);
      $user
        ->activate();

      // Sender phone number.
      $sender_number = $sms_message
        ->getSenderNumber();
      $t_args['%sender_phone_number'] = $sender_number;

      // Sender phone number.
      $phone_field_name = $this->userPhoneNumberSettings
        ->getFieldName('phone_number');
      $user->{$phone_field_name}[] = $sender_number;
      if (!empty($matches['email'][0]) && $contains_email) {
        $user
          ->setEmail($matches['email'][0]);
      }
      $password = !empty($matches['password'][0]) && $contains_password ? $matches['password'][0] : user_password();
      $user
        ->setPassword($password);
      $validate = $this
        ->removeAcceptableViolations($user
        ->validate(), $incoming_form);
      if ($validate
        ->count() == 0) {
        $user
          ->save();

        // @todo autoconfirm the number?
        // @see https://www.drupal.org/node/2709911
        $message = $this
          ->settings('incoming_pattern.reply.message');
        $message = str_replace('[user:password]', $password, $message);
        \Drupal::logger('sms_user.account_registration.incoming_pattern')
          ->info('Creating new account for %sender_phone_number. Username: %name. User ID: %uid', $t_args + [
          '%uid' => $user
            ->id(),
          '%name' => $user
            ->label(),
        ]);

        // Send an activation email if no password placeholder is found.
        if (!$contains_password && !empty($this
          ->settings('incoming_pattern.send_activation_email'))) {
          _user_mail_notify('register_no_approval_required', $user);
        }
      }
      else {
        $message = $this
          ->settings('incoming_pattern.reply.message_failure');
        $error = $this
          ->buildError($validate);
        $message = str_replace('[error]', $error, $message);
        \Drupal::logger('sms_user.account_registration.incoming_pattern')
          ->warning('Could not create new account for %sender_phone_number because there was a problem with validation: @error', $t_args + [
          '@error' => $error,
        ]);
      }

      // Optionally send a reply.
      if (!empty($this
        ->settings('incoming_pattern.reply.status'))) {
        $this
          ->sendReply($sender_number, $user, $message);
      }
    }
  }
}