AccountInterface.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Drupal\Core\Session;
  3. /**
  4. * Defines an account interface which represents the current user.
  5. *
  6. * Defines an object that has a user id, roles and can have session data. The
  7. * interface is implemented both by the global session and the user entity.
  8. *
  9. * @ingroup user_api
  10. */
  11. interface AccountInterface {
  12. /**
  13. * Role ID for anonymous users.
  14. */
  15. const ANONYMOUS_ROLE = 'anonymous';
  16. /**
  17. * Role ID for authenticated users.
  18. */
  19. const AUTHENTICATED_ROLE = 'authenticated';
  20. /**
  21. * Returns the user ID or 0 for anonymous.
  22. *
  23. * @return int
  24. * The user ID.
  25. */
  26. public function id();
  27. /**
  28. * Returns a list of roles.
  29. *
  30. * @param bool $exclude_locked_roles
  31. * (optional) If TRUE, locked roles (anonymous/authenticated) are not returned.
  32. *
  33. * @return array
  34. * List of role IDs.
  35. */
  36. public function getRoles($exclude_locked_roles = FALSE);
  37. /**
  38. * Checks whether a user has a certain permission.
  39. *
  40. * @param string $permission
  41. * The permission string to check.
  42. *
  43. * @return bool
  44. * TRUE if the user has the permission, FALSE otherwise.
  45. */
  46. public function hasPermission($permission);
  47. /**
  48. * Returns TRUE if the account is authenticated.
  49. *
  50. * @return bool
  51. * TRUE if the account is authenticated.
  52. */
  53. public function isAuthenticated();
  54. /**
  55. * Returns TRUE if the account is anonymous.
  56. *
  57. * @return bool
  58. * TRUE if the account is anonymous.
  59. */
  60. public function isAnonymous();
  61. /**
  62. * Returns the preferred language code of the account.
  63. *
  64. * @param bool $fallback_to_default
  65. * (optional) Whether the return value will fall back to the site default
  66. * language if the user has no language preference.
  67. *
  68. * @return string
  69. * The language code that is preferred by the account. If the preferred
  70. * language is not set or is a language not configured anymore on the site,
  71. * the site default is returned or an empty string is returned (if
  72. * $fallback_to_default is FALSE).
  73. */
  74. public function getPreferredLangcode($fallback_to_default = TRUE);
  75. /**
  76. * Returns the preferred administrative language code of the account.
  77. *
  78. * Defines which language is used on administrative pages.
  79. *
  80. * @param bool $fallback_to_default
  81. * (optional) Whether the return value will fall back to the site default
  82. * language if the user has no administration language preference.
  83. *
  84. * @return string
  85. * The language code that is preferred by the account for administration
  86. * pages. If the preferred language is not set or is a language not
  87. * configured anymore on the site, the site default is returned or an empty
  88. * string is returned (if $fallback_to_default is FALSE).
  89. */
  90. public function getPreferredAdminLangcode($fallback_to_default = TRUE);
  91. /**
  92. * Returns the unaltered login name of this account.
  93. *
  94. * @return string
  95. * An unsanitized plain-text string with the name of this account that is
  96. * used to log in. Only display this name to admins and to the user who owns
  97. * this account, and only in the context of the name used to log in. For
  98. * any other display purposes, use
  99. * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
  100. *
  101. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  102. * Use \Drupal\Core\Session\AccountInterface::getAccountName() or
  103. * \Drupal\user\UserInterface::getDisplayName() instead.
  104. */
  105. public function getUsername();
  106. /**
  107. * Returns the unaltered login name of this account.
  108. *
  109. * @return string
  110. * An unsanitized plain-text string with the name of this account that is
  111. * used to log in. Only display this name to admins and to the user who owns
  112. * this account, and only in the context of the name used to login. For
  113. * any other display purposes, use
  114. * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
  115. */
  116. public function getAccountName();
  117. /**
  118. * Returns the display name of this account.
  119. *
  120. * By default, the passed-in object's 'name' property is used if it exists, or
  121. * else, the site-defined value for the 'anonymous' variable. However, a
  122. * module may override this by implementing
  123. * hook_user_format_name_alter(&$name, $account).
  124. *
  125. * @see hook_user_format_name_alter()
  126. *
  127. * @return string|\Drupal\Component\Render\MarkupInterface
  128. * Either a string that will be auto-escaped on output or a
  129. * MarkupInterface object that is already HTML escaped. Either is safe
  130. * to be printed within HTML fragments.
  131. */
  132. public function getDisplayName();
  133. /**
  134. * Returns the email address of this account.
  135. *
  136. * @return string|null
  137. * The email address, or NULL if the account is anonymous or the user does
  138. * not have an email address.
  139. */
  140. public function getEmail();
  141. /**
  142. * Returns the timezone of this account.
  143. *
  144. * @return string
  145. * Name of the timezone.
  146. */
  147. public function getTimeZone();
  148. /**
  149. * The timestamp when the account last accessed the site.
  150. *
  151. * A value of 0 means the user has never accessed the site.
  152. *
  153. * @return int
  154. * Timestamp of the last access.
  155. */
  156. public function getLastAccessedTime();
  157. }