email_changelog-8.x-1.0-alpha1/email_changelog.module

email_changelog.module
<?php

/**
 * @file
 * Logs changes to user email.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\UserInterface;

/**
 * Implements hook_help().
 */
function user_changelog_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.email_changelog':
      return
      '<p>' . t('Email Changlog will automatically start recording all email changes. No further configuration is necessary for this functionality, the module will do this "out of the box". A record of these changes is shown in a Email Changelog tab on each user\'s page. Users will need  either "View email changelog" or "View own email changelog" access permissions to  view the tab.') . '</p>';
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave() for user entities.
 */
function email_changelog_user_presave(UserInterface $account) {
  $unchanged_account = $account->original;
  if (!$account->isNew() && $account->getEmail() != $unchanged_account->getEmail()) {
    _email_changelog_log($account->id(), $account->getEmail(), $unchanged_account->getEmail());
  }
}

/**
 * Implements hook_user_cancel().
 */
function email_changelog_user_cancel($edit, UserInterface $account, $method) {
  $database = \Drupal::database();
  $database->delete('email_changelog')
    ->condition('uid', $account->id())
    ->execute();
  $database->update('email_changelog')
    ->fields(['logged_in_uid' => 0])
    ->condition('logged_in_uid', $account->id())
    ->execute();
}

/**
 * Access callback for viewing email changelog page.
 */
function email_changelog_history_access($account) {
  $current_user = \Drupal::currentUser();
  if ($current_user->hasPermission('view email changelog')) {
    return TRUE;
  }

  return ($current_user->id() == $account->id() && $current_user->hasPermission('view own email changelog')) ? TRUE : FALSE;
}

/**
 * This function logs a email change.
 *
 * @param $uid
 * @param $new_email
 * @param $old_email
 * @param array $data
 */
function _email_changelog_log($uid, $new_email, $old_email, $data = []) {
  if ($uid == NULL) {
    \Drupal::logger('email_changelog')->warning('Email log called without user id');
    $uid = 0;
  }
  $request = \Drupal::request();
  $url = \Drupal\Core\Url::fromUri($request->getUri(), [
    'absolute' => TRUE,
    'query' => $request->query->all(),
  ])->toString();
  $fields = [
    'uid' => $uid,
    'old_email_address' => $old_email,
    'new_email_address' => $new_email,
    'data' => serialize($data),
    'logged_in_uid' => \Drupal::currentUser()->id(),
    'location' => $url,
    'timestamp' => \Drupal::time()->getRequestTime(),
  ];

  \Drupal::database()->insert('email_changelog')
    ->fields($fields)
    ->execute();
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc