Use \Drupal::currentUser() instead of global $user.