How do I get the global user object?
&lt;?php
public function behave() {
  $account = \Drupal::currentUser();
  if ($account-&gt;id() == 1) {
    return "Hi, boss!";
  }
  else {
    return "You are not the site administrator.";
  }
}
?&gt;


Note that in neither case is the value an actual User object. Rather, as of Drupal 8 it is a UserSession object, which duplicates some, but not all, of the information of a user. It may also represent the Anonymous User rather than a saved User entity. See link for more information.