CentralIdLookup.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * A central user id lookup service
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use Wikimedia\ObjectFactory;
  23. /**
  24. * The CentralIdLookup service allows for connecting local users with
  25. * cluster-wide IDs.
  26. *
  27. * @since 1.27
  28. */
  29. abstract class CentralIdLookup implements IDBAccessObject {
  30. // Audience options for accessors
  31. const AUDIENCE_PUBLIC = 1;
  32. const AUDIENCE_RAW = 2;
  33. /** @var CentralIdLookup[] */
  34. private static $instances = [];
  35. /** @var string */
  36. private $providerId;
  37. /**
  38. * Fetch a CentralIdLookup
  39. * @param string|null $providerId Provider ID from $wgCentralIdLookupProviders
  40. * @return CentralIdLookup|null
  41. */
  42. public static function factory( $providerId = null ) {
  43. global $wgCentralIdLookupProviders, $wgCentralIdLookupProvider;
  44. if ( $providerId === null ) {
  45. $providerId = $wgCentralIdLookupProvider;
  46. }
  47. if ( !array_key_exists( $providerId, self::$instances ) ) {
  48. self::$instances[$providerId] = null;
  49. if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
  50. $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
  51. if ( $provider instanceof CentralIdLookup ) {
  52. $provider->providerId = $providerId;
  53. self::$instances[$providerId] = $provider;
  54. }
  55. }
  56. }
  57. return self::$instances[$providerId];
  58. }
  59. /**
  60. * Reset internal cache for unit testing
  61. * @codeCoverageIgnore
  62. */
  63. public static function resetCache() {
  64. if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
  65. throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
  66. }
  67. self::$instances = [];
  68. }
  69. final public function getProviderId() {
  70. return $this->providerId;
  71. }
  72. /**
  73. * Check that the "audience" parameter is valid
  74. * @param int|User $audience One of the audience constants, or a specific user
  75. * @return User|null User to check against, or null if no checks are needed
  76. * @throws InvalidArgumentException
  77. */
  78. protected function checkAudience( $audience ) {
  79. if ( $audience instanceof User ) {
  80. return $audience;
  81. }
  82. if ( $audience === self::AUDIENCE_PUBLIC ) {
  83. return new User;
  84. }
  85. if ( $audience === self::AUDIENCE_RAW ) {
  86. return null;
  87. }
  88. throw new InvalidArgumentException( 'Invalid audience' );
  89. }
  90. /**
  91. * Check that a User is attached on the specified wiki.
  92. *
  93. * If unattached local accounts don't exist in your extension, this comes
  94. * down to a check whether the central account exists at all and that
  95. * $wikiId is using the same central database.
  96. *
  97. * @param User $user
  98. * @param string|null $wikiId Wiki to check attachment status. If null, check the current wiki.
  99. * @return bool
  100. */
  101. abstract public function isAttached( User $user, $wikiId = null );
  102. /**
  103. * Given central user IDs, return the (local) user names
  104. * @note There's no requirement that the user names actually exist locally,
  105. * or if they do that they're actually attached to the central account.
  106. * @param array $idToName Array with keys being central user IDs
  107. * @param int|User $audience One of the audience constants, or a specific user
  108. * @param int $flags IDBAccessObject read flags
  109. * @return array Copy of $idToName with values set to user names (or
  110. * empty-string if the user exists but $audience lacks the rights needed
  111. * to see it). IDs not corresponding to a user are unchanged.
  112. */
  113. abstract public function lookupCentralIds(
  114. array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  115. );
  116. /**
  117. * Given (local) user names, return the central IDs
  118. * @note There's no requirement that the user names actually exist locally,
  119. * or if they do that they're actually attached to the central account.
  120. * @param array $nameToId Array with keys being canonicalized user names
  121. * @param int|User $audience One of the audience constants, or a specific user
  122. * @param int $flags IDBAccessObject read flags
  123. * @return array Copy of $nameToId with values set to central IDs.
  124. * Names not corresponding to a user (or $audience lacks the rights needed
  125. * to see it) are unchanged.
  126. */
  127. abstract public function lookupUserNames(
  128. array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  129. );
  130. /**
  131. * Given a central user ID, return the (local) user name
  132. * @note There's no requirement that the user name actually exists locally,
  133. * or if it does that it's actually attached to the central account.
  134. * @param int $id Central user ID
  135. * @param int|User $audience One of the audience constants, or a specific user
  136. * @param int $flags IDBAccessObject read flags
  137. * @return string|null User name, or empty string if $audience lacks the
  138. * rights needed to see it, or null if $id doesn't correspond to a user
  139. */
  140. public function nameFromCentralId(
  141. $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  142. ) {
  143. $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
  144. return $idToName[$id];
  145. }
  146. /**
  147. * Given a an array of central user IDs, return the (local) user names.
  148. * @param int[] $ids Central user IDs
  149. * @param int|User $audience One of the audience constants, or a specific user
  150. * @param int $flags IDBAccessObject read flags
  151. * @return string[] User names
  152. * @since 1.30
  153. */
  154. public function namesFromCentralIds(
  155. array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  156. ) {
  157. $idToName = array_fill_keys( $ids, false );
  158. $names = $this->lookupCentralIds( $idToName, $audience, $flags );
  159. $names = array_unique( $names );
  160. $names = array_filter( $names, function ( $name ) {
  161. return $name !== false && $name !== '';
  162. } );
  163. return array_values( $names );
  164. }
  165. /**
  166. * Given a (local) user name, return the central ID
  167. * @note There's no requirement that the user name actually exists locally,
  168. * or if it does that it's actually attached to the central account.
  169. * @param string $name Canonicalized user name
  170. * @param int|User $audience One of the audience constants, or a specific user
  171. * @param int $flags IDBAccessObject read flags
  172. * @return int User ID; 0 if the name does not correspond to a user or
  173. * $audience lacks the rights needed to see it.
  174. */
  175. public function centralIdFromName(
  176. $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  177. ) {
  178. $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
  179. return $nameToId[$name];
  180. }
  181. /**
  182. * Given an array of (local) user names, return the central IDs.
  183. * @param string[] $names Canonicalized user names
  184. * @param int|User $audience One of the audience constants, or a specific user
  185. * @param int $flags IDBAccessObject read flags
  186. * @return int[] User IDs
  187. * @since 1.30
  188. */
  189. public function centralIdsFromNames(
  190. array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  191. ) {
  192. $nameToId = array_fill_keys( $names, false );
  193. $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
  194. $ids = array_unique( $ids );
  195. $ids = array_filter( $ids, function ( $id ) {
  196. return $id !== false;
  197. } );
  198. return array_values( $ids );
  199. }
  200. /**
  201. * Given a central user ID, return a local User object
  202. * @note Unlike nameFromCentralId(), this does guarantee that the local
  203. * user exists and is attached to the central account.
  204. * @param int $id Central user ID
  205. * @param int|User $audience One of the audience constants, or a specific user
  206. * @param int $flags IDBAccessObject read flags
  207. * @return User|null Local user, or null if: $id doesn't correspond to a
  208. * user, $audience lacks the rights needed to see the user, the user
  209. * doesn't exist locally, or the user isn't locally attached.
  210. */
  211. public function localUserFromCentralId(
  212. $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  213. ) {
  214. $name = $this->nameFromCentralId( $id, $audience, $flags );
  215. if ( $name !== null && $name !== '' ) {
  216. $user = User::newFromName( $name );
  217. if ( $user && $user->getId() && $this->isAttached( $user ) ) {
  218. return $user;
  219. }
  220. }
  221. return null;
  222. }
  223. /**
  224. * Given a local User object, return the central ID
  225. * @note Unlike centralIdFromName(), this does guarantee that the local
  226. * user is attached to the central account.
  227. * @param User $user Local user
  228. * @param int|User $audience One of the audience constants, or a specific user
  229. * @param int $flags IDBAccessObject read flags
  230. * @return int User ID; 0 if the local user does not correspond to a
  231. * central user, $audience lacks the rights needed to see it, or the
  232. * central user isn't locally attached.
  233. */
  234. public function centralIdFromLocalUser(
  235. User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
  236. ) {
  237. return $this->isAttached( $user )
  238. ? $this->centralIdFromName( $user->getName(), $audience, $flags )
  239. : 0;
  240. }
  241. }