AccountInterface.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * Returned language code depends upon following:
  70. * - The user preferred language code is returned if set in the account.
  71. * - If the user has no preferred language and $fallback_to_default is TRUE
  72. * then the site default language code is returned.
  73. * - If the user has no preferred language and $fallback_to_default is FALSE
  74. * then empty string is returned.
  75. */
  76. public function getPreferredLangcode($fallback_to_default = TRUE);
  77. /**
  78. * Returns the preferred administrative language code of the account.
  79. *
  80. * Defines which language is used on administrative pages.
  81. *
  82. * @param bool $fallback_to_default
  83. * (optional) Whether the return value will fall back to the site default
  84. * language if the user has no administration language preference.
  85. *
  86. * @return string
  87. * The language code that is preferred by the account for administration
  88. * pages. If the preferred language is not set or is a language not
  89. * configured anymore on the site, the site default is returned or an empty
  90. * string is returned (if $fallback_to_default is FALSE).
  91. */
  92. public function getPreferredAdminLangcode($fallback_to_default = TRUE);
  93. /**
  94. * Returns the unaltered login name of this account.
  95. *
  96. * @return string
  97. * An unsanitized plain-text string with the name of this account that is
  98. * used to log in. Only display this name to admins and to the user who owns
  99. * this account, and only in the context of the name used to log in. For
  100. * any other display purposes, use
  101. * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
  102. *
  103. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  104. * Use \Drupal\Core\Session\AccountInterface::getAccountName() or
  105. * \Drupal\user\UserInterface::getDisplayName() instead.
  106. */
  107. public function getUsername();
  108. /**
  109. * Returns the unaltered login name of this account.
  110. *
  111. * @return string
  112. * An unsanitized plain-text string with the name of this account that is
  113. * used to log in. Only display this name to admins and to the user who owns
  114. * this account, and only in the context of the name used to login. For
  115. * any other display purposes, use
  116. * \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
  117. */
  118. public function getAccountName();
  119. /**
  120. * Returns the display name of this account.
  121. *
  122. * By default, the passed-in object's 'name' property is used if it exists, or
  123. * else, the site-defined value for the 'anonymous' variable. However, a
  124. * module may override this by implementing
  125. * hook_user_format_name_alter(&$name, $account).
  126. *
  127. * @see hook_user_format_name_alter()
  128. *
  129. * @return string|\Drupal\Component\Render\MarkupInterface
  130. * Either a string that will be auto-escaped on output or a
  131. * MarkupInterface object that is already HTML escaped. Either is safe
  132. * to be printed within HTML fragments.
  133. */
  134. public function getDisplayName();
  135. /**
  136. * Returns the email address of this account.
  137. *
  138. * @return string|null
  139. * The email address, or NULL if the account is anonymous or the user does
  140. * not have an email address.
  141. */
  142. public function getEmail();
  143. /**
  144. * Returns the timezone of this account.
  145. *
  146. * @return string
  147. * Name of the timezone.
  148. */
  149. public function getTimeZone();
  150. /**
  151. * The timestamp when the account last accessed the site.
  152. *
  153. * A value of 0 means the user has never accessed the site.
  154. *
  155. * @return int
  156. * Timestamp of the last access.
  157. */
  158. public function getLastAccessedTime();
  159. }