You are here

function drush_acsf_connect_factory in Acquia Cloud Site Factory Connector 8

Command callback: Connect a site to a Factory.

1 string reference to 'drush_acsf_connect_factory'
acsf_init_drush_command in acsf_init/acsf_init.drush.inc
Implements hook_drush_command().

File

acsf_init/acsf_init.drush.inc, line 450
Provides drush commands to set up a site for Acquia Site Factory.

Code

function drush_acsf_connect_factory() {

  // Preliminary validation before starting to modify the database.
  $site_admin_mail = trim(drush_get_option('site-admin-mail'));
  $site_owner_name = trim(drush_get_option('site-owner-name'));
  $site_owner_mail = trim(drush_get_option('site-owner-mail'));
  $site_owner_roles = drush_get_option_list('site-owner-roles');

  // Validate email addresses.
  $validator = \Drupal::service('email.validator');
  if (!$validator
    ->isValid($site_admin_mail)) {
    return drush_set_error(dt('The site-admin-mail value is not a valid email address.'));
  }
  if (!$validator
    ->isValid($site_owner_mail)) {
    return drush_set_error(dt('The site-owner-mail value is not a valid email address.'));
  }
  $user_storage = \Drupal::entityTypeManager()
    ->getStorage('user');

  // Make sure there is no regular user account with the admin email address.
  $site_admin_mail_accounts = $user_storage
    ->loadByProperties([
    'mail' => $site_admin_mail,
  ]);
  $site_admin_mail_account = reset($site_admin_mail_accounts);
  if ($site_admin_mail_account && $site_admin_mail_account
    ->id() > 1) {
    return drush_set_error(dt('Unable to sync the admin account, the email address @mail is already used by the user account @uid.', [
      '@mail' => $site_admin_mail,
      '@uid' => $site_admin_mail_account
        ->id(),
    ]));
  }

  // The site owner's email address may have been changed on the factory (for
  // instance, if the user updated their email address on the factory and the
  // new email address has not yet been synced to the site). First, try to
  // locate the user account by site-owner-mail.
  $site_owner_accounts = $user_storage
    ->loadByProperties([
    'mail' => $site_owner_mail,
  ]);
  $site_owner_account = reset($site_owner_accounts);
  if ($site_owner_account && $site_owner_account
    ->getAccountName() !== $site_owner_name) {
    return drush_set_error(dt('The site-owner-name value does not match the name of the user loaded by site-owner-mail.'));
  }

  // If the site owner user account is not found, try to locate it by
  // site-owner-name.
  if (!$site_owner_account) {
    $site_owner_accounts = $user_storage
      ->loadByProperties([
      'name' => $site_owner_name,
    ]);
    $site_owner_account = reset($site_owner_accounts);
  }

  // If the site owner account is still not found then either the customer has
  // made a typo or maybe there is going to be a new owner who needs a new
  // account. Ask for confirmation to create a new account.
  if (!$site_owner_account) {
    if (!drush_confirm(dt('The site owner name or email address that you provided does not correspond to any account on the site. Do you want to create a new account?'))) {
      return drush_user_abort();
    }
  }

  // Clear all caches ahead of time so Drupal has a chance to rebuild
  // registries.
  drupal_flush_all_caches();
  acsf_build_registry();
  drush_log(dt('Cleared all caches.'), 'ok');

  // Set default settings for user accounts.
  $admin_role_ids = \Drupal::EntityQuery('user_role')
    ->condition('is_admin', TRUE)
    ->execute();

  // Take over uid 1 with our Site Factory admin user.
  $admin_account = User::load(1);

  // Create a new user if uid 1 doesn't exist.
  if (!$admin_account) {
    $admin_account = User::create([
      'uid' => 1,
    ]);
  }

  // Ensure the default admin role is added to the account.
  $admin_account
    ->addRole(reset($admin_role_ids));

  // Set login time to avoid e-mail verification needed error.
  $admin_account
    ->setLastLoginTime(1)
    ->setUsername('Site Factory admin')
    ->setEmail($site_admin_mail)
    ->setPassword(user_password())
    ->activate()
    ->save();

  // Create or update site owner account.
  // Prepare roles for site owner.
  if (!$site_owner_account) {
    $site_owner_account = User::create();
  }
  foreach ($site_owner_roles as $owner_role) {
    if (Role::load($owner_role)) {
      $site_owner_account
        ->addRole($owner_role);
    }
    else {
      drush_log(dt('The role @role does not exist; not adding it to the site owner.', [
        '@role' => $owner_role,
      ]), 'warning');
    }
  }

  // Site owners also get the default administrator role.
  $site_owner_account
    ->addRole(reset($admin_role_ids));
  $site_owner_account
    ->setLastLoginTime(1)
    ->setUsername($site_owner_name)
    ->setEmail($site_owner_mail)
    ->setPassword(user_password())
    ->activate()
    ->save();
  drush_log(dt('Synched Site Factory admin and site owner accounts.'), 'ok');

  // Remove acsf variable so that it can be repopulated with the right value
  // on the next acsf-site-sync.
  \Drupal::service('acsf.variable_storage')
    ->delete('acsf_site_info');

  // Reset the local site data and run acsf-site-sync to fetch factory data
  // about the site.
  $site = AcsfSite::load();
  $site
    ->clean();
  drush_acsf_site_sync();
  drush_log(dt('Executed acsf-site-sync to gather site data from factory and reset all acsf variables.'), 'ok');

  // Clear all caches.
  drupal_flush_all_caches();
  drush_log(dt('Cleared all caches.'), 'ok');

  // Send a theme event notification to the Factory.
  \Drupal::service('acsf.theme_notification')
    ->sendNotification('site', 'create');
}