UserRightsProxy.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Representation of an user on a other locally-hosted wiki.
  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\Rdbms\IDatabase;
  23. /**
  24. * Cut-down copy of User interface for local-interwiki-database
  25. * user rights manipulation.
  26. */
  27. class UserRightsProxy {
  28. /** @var IDatabase */
  29. private $db;
  30. /** @var string */
  31. private $dbDomain;
  32. /** @var string */
  33. private $name;
  34. /** @var int */
  35. private $id;
  36. /** @var array */
  37. private $newOptions;
  38. /**
  39. * @see newFromId()
  40. * @see newFromName()
  41. * @param IDatabase $db Db connection
  42. * @param string $dbDomain Database name
  43. * @param string $name User name
  44. * @param int $id User ID
  45. */
  46. private function __construct( $db, $dbDomain, $name, $id ) {
  47. $this->db = $db;
  48. $this->dbDomain = $dbDomain;
  49. $this->name = $name;
  50. $this->id = intval( $id );
  51. $this->newOptions = [];
  52. }
  53. /**
  54. * Confirm the selected database name is a valid local interwiki database name.
  55. *
  56. * @param string $dbDomain Database name
  57. * @return bool
  58. */
  59. public static function validDatabase( $dbDomain ) {
  60. global $wgLocalDatabases;
  61. return in_array( $dbDomain, $wgLocalDatabases );
  62. }
  63. /**
  64. * Same as User::whoIs()
  65. *
  66. * @param string $dbDomain Database name
  67. * @param int $id User ID
  68. * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
  69. * @return string User name or false if the user doesn't exist
  70. */
  71. public static function whoIs( $dbDomain, $id, $ignoreInvalidDB = false ) {
  72. $user = self::newFromId( $dbDomain, $id, $ignoreInvalidDB );
  73. if ( $user ) {
  74. return $user->name;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Factory function; get a remote user entry by ID number.
  81. *
  82. * @param string $dbDomain Database name
  83. * @param int $id User ID
  84. * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
  85. * @return UserRightsProxy|null If doesn't exist
  86. */
  87. public static function newFromId( $dbDomain, $id, $ignoreInvalidDB = false ) {
  88. return self::newFromLookup( $dbDomain, 'user_id', intval( $id ), $ignoreInvalidDB );
  89. }
  90. /**
  91. * Factory function; get a remote user entry by name.
  92. *
  93. * @param string $dbDomain Database name
  94. * @param string $name User name
  95. * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
  96. * @return UserRightsProxy|null If doesn't exist
  97. */
  98. public static function newFromName( $dbDomain, $name, $ignoreInvalidDB = false ) {
  99. return self::newFromLookup( $dbDomain, 'user_name', $name, $ignoreInvalidDB );
  100. }
  101. /**
  102. * @param string $dbDomain
  103. * @param string $field
  104. * @param string $value
  105. * @param bool $ignoreInvalidDB
  106. * @return null|UserRightsProxy
  107. */
  108. private static function newFromLookup( $dbDomain, $field, $value, $ignoreInvalidDB = false ) {
  109. global $wgSharedDB, $wgSharedTables;
  110. // If the user table is shared, perform the user query on it,
  111. // but don't pass it to the UserRightsProxy,
  112. // as user rights are normally not shared.
  113. if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) {
  114. $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB );
  115. } else {
  116. $userdb = self::getDB( $dbDomain, $ignoreInvalidDB );
  117. }
  118. $db = self::getDB( $dbDomain, $ignoreInvalidDB );
  119. if ( $db && $userdb ) {
  120. $row = $userdb->selectRow( 'user',
  121. [ 'user_id', 'user_name' ],
  122. [ $field => $value ],
  123. __METHOD__ );
  124. if ( $row !== false ) {
  125. return new UserRightsProxy(
  126. $db, $dbDomain, $row->user_name, intval( $row->user_id ) );
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * Open a database connection to work on for the requested user.
  133. * This may be a new connection to another database for remote users.
  134. *
  135. * @param string $dbDomain
  136. * @param bool $ignoreInvalidDB If true, don't check if $dbDomain is in $wgLocalDatabases
  137. * @return IDatabase|null If invalid selection
  138. */
  139. public static function getDB( $dbDomain, $ignoreInvalidDB = false ) {
  140. if ( $ignoreInvalidDB || self::validDatabase( $dbDomain ) ) {
  141. if ( WikiMap::isCurrentWikiId( $dbDomain ) ) {
  142. // Hmm... this shouldn't happen though. :)
  143. return wfGetDB( DB_MASTER );
  144. } else {
  145. return wfGetDB( DB_MASTER, [], $dbDomain );
  146. }
  147. }
  148. return null;
  149. }
  150. /**
  151. * @return int
  152. */
  153. public function getId() {
  154. return $this->id;
  155. }
  156. /**
  157. * @return bool
  158. */
  159. public function isAnon() {
  160. return $this->getId() == 0;
  161. }
  162. /**
  163. * Same as User::getName()
  164. *
  165. * @return string
  166. */
  167. public function getName() {
  168. return $this->name . '@' . $this->dbDomain;
  169. }
  170. /**
  171. * Same as User::getUserPage()
  172. *
  173. * @return Title
  174. */
  175. public function getUserPage() {
  176. return Title::makeTitle( NS_USER, $this->getName() );
  177. }
  178. /**
  179. * Replaces User::getUserGroups()
  180. * @return array
  181. */
  182. function getGroups() {
  183. return array_keys( self::getGroupMemberships() );
  184. }
  185. /**
  186. * Replaces User::getGroupMemberships()
  187. *
  188. * @return array
  189. * @since 1.29
  190. */
  191. function getGroupMemberships() {
  192. return UserGroupMembership::getMembershipsForUser( $this->id, $this->db );
  193. }
  194. /**
  195. * Replaces User::addGroup()
  196. *
  197. * @param string $group
  198. * @param string|null $expiry
  199. * @return bool
  200. */
  201. function addGroup( $group, $expiry = null ) {
  202. if ( $expiry ) {
  203. $expiry = wfTimestamp( TS_MW, $expiry );
  204. }
  205. $ugm = new UserGroupMembership( $this->id, $group, $expiry );
  206. return $ugm->insert( true, $this->db );
  207. }
  208. /**
  209. * Replaces User::removeGroup()
  210. *
  211. * @param string $group
  212. * @return bool
  213. */
  214. function removeGroup( $group ) {
  215. $ugm = UserGroupMembership::getMembership( $this->id, $group, $this->db );
  216. if ( !$ugm ) {
  217. return false;
  218. }
  219. return $ugm->delete( $this->db );
  220. }
  221. /**
  222. * Replaces User::setOption()
  223. * @param string $option
  224. * @param mixed $value
  225. */
  226. public function setOption( $option, $value ) {
  227. $this->newOptions[$option] = $value;
  228. }
  229. public function saveSettings() {
  230. $rows = [];
  231. foreach ( $this->newOptions as $option => $value ) {
  232. $rows[] = [
  233. 'up_user' => $this->id,
  234. 'up_property' => $option,
  235. 'up_value' => $value,
  236. ];
  237. }
  238. $this->db->replace( 'user_properties',
  239. [ [ 'up_user', 'up_property' ] ],
  240. $rows, __METHOD__
  241. );
  242. $this->invalidateCache();
  243. }
  244. /**
  245. * Replaces User::touchUser()
  246. */
  247. function invalidateCache() {
  248. $this->db->update(
  249. 'user',
  250. [ 'user_touched' => $this->db->timestamp() ],
  251. [ 'user_id' => $this->id ],
  252. __METHOD__
  253. );
  254. $domainId = $this->db->getDomainID();
  255. $userId = $this->id;
  256. $this->db->onTransactionPreCommitOrIdle(
  257. function () use ( $domainId, $userId ) {
  258. User::purge( $domainId, $userId );
  259. },
  260. __METHOD__
  261. );
  262. }
  263. }