UserCancelForm.php 5.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\user\Form\UserCancelForm.
 */

namespace Drupal\user\Form;

10
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
11
use Drupal\Core\Form\FormStateInterface;
12 13 14 15

/**
 * Provides a confirmation form for cancelling user account.
 */
16
class UserCancelForm extends ContentEntityConfirmFormBase {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  /**
   * Available account cancellation methods.
   *
   * @var array
   */
  protected $cancelMethods;

  /**
   * The user being cancelled.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    if ($this->entity->id() == $this->currentUser()->id()) {
      return $this->t('Are you sure you want to cancel your account?');
    }
    return $this->t('Are you sure you want to cancel the account %name?', array('%name' => $this->entity->label()));
  }

  /**
   * {@inheritdoc}
   */
45 46
  public function getCancelUrl() {
    return $this->entity->urlInfo();
47 48 49 50 51 52 53
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    $description = '';
54
    $default_method = $this->config('user.settings')->get('cancel_method');
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    if ($this->currentUser()->hasPermission('administer users') || $this->currentUser()->hasPermission('select account cancellation method')) {
      $description = $this->t('Select the method to cancel the account above.');
    }
    // Options supplied via user_cancel_methods() can have a custom
    // #confirm_description property for the confirmation form description.
    elseif (isset($this->cancelMethods[$default_method]['#confirm_description'])) {
      $description = $this->cancelMethods[$default_method]['#confirm_description'];
    }
    return $description . ' ' . $this->t('This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Cancel account');
  }

  /**
   * {@inheritdoc}
   */
76
  public function buildForm(array $form, FormStateInterface $form_state) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    $user = $this->currentUser();
    $this->cancelMethods = user_cancel_methods();

    // Display account cancellation method selection, if allowed.
    $admin_access = $user->hasPermission('administer users');
    $form['user_cancel_method'] = array(
      '#type' => 'radios',
      '#title' => ($this->entity->id() == $user->id() ? $this->t('When cancelling your account') : $this->t('When cancelling the account')),
      '#access' => $admin_access || $user->hasPermission('select account cancellation method'),
    );
    $form['user_cancel_method'] += $this->cancelMethods;

    // Allow user administrators to skip the account cancellation confirmation
    // mail (by default), as long as they do not attempt to cancel their own
    // account.
    $override_access = $admin_access && ($this->entity->id() != $user->id());
    $form['user_cancel_confirm'] = array(
      '#type' => 'checkbox',
95
      '#title' => $this->t('Require email confirmation to cancel account'),
96 97
      '#default_value' => !$override_access,
      '#access' => $override_access,
98
      '#description' => $this->t('When enabled, the user must confirm the account cancellation via email.'),
99 100
    );
    // Also allow to send account canceled notification mail, if enabled.
101
    $default_notify = $this->config('user.settings')->get('notify.status_canceled');
102 103
    $form['user_cancel_notify'] = array(
      '#type' => 'checkbox',
104
      '#title' => $this->t('Notify user when account is canceled'),
105 106
      '#default_value' => ($override_access ? FALSE : $default_notify),
      '#access' => $override_access && $default_notify,
107
      '#description' => $this->t('When enabled, the user will receive an email notification after the account has been canceled.'),
108 109 110 111 112 113 114 115 116 117 118 119 120
    );

    // Always provide entity id in the same form key as in the entity edit form.
    $form['uid'] = array('#type' => 'value', '#value' => $this->entity->id());

    $form = parent::buildForm($form, $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
121
  public function submitForm(array &$form, FormStateInterface $form_state) {
122 123 124
    // Cancel account immediately, if the current user has administrative
    // privileges, no confirmation mail shall be sent, and the user does not
    // attempt to cancel the own account.
125 126
    if ($this->currentUser()->hasPermission('administer users') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) {
      user_cancel($form_state->getValues(), $this->entity->id(), $form_state->getValue('user_cancel_method'));
127

128
      $form_state->setRedirect('user.admin_account');
129 130 131 132
    }
    else {
      // Store cancelling method and whether to notify the user in
      // $this->entity for user_cancel_confirm().
133 134
      $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
      $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
135 136
      $this->entity->save();
      _user_mail_notify('cancel_confirm', $this->entity);
137
      drupal_set_message($this->t('A confirmation request to cancel your account has been sent to your email address.'));
138
      $this->logger('user')->notice('Sent account cancellation request to %name %email.', array('%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>'));
139

140
      $form_state->setRedirect(
141
        'entity.user.canonical',
142
        array('user' => $this->entity->id())
143
      );
144 145 146 147
    }
  }

}