Snippet: How to check for user access or permission in Drupal 8

In Drupal 7 you did user_access(), but in D8 you should do:
1
2
3
4
5
6
7
  1. <?php
  2. // Get current user
  3. $user = \Drupal::currentUser();
  4. // Check for permission
  5. $user->hasPermission('administer content');