From: Mark A. Hershberger Date: Wed, 16 Mar 2011 23:13:38 +0000 (+0000) Subject: Fix Bug #28082, Add Hooks to User::addGroup and User::removeGroup X-Git-Tag: 1.31.0-rc.0~31369 X-Git-Url: http://git.cyclocoop.org/%27.parametre_url%28%20%20%20generer_action_auteur%28%27charger_plugin%27%2C%20%27update_flux%27%29%2C%27update_flux%27%2C%20%27oui%27%29.%27?a=commitdiff_plain;h=282870288937e20be0c7200f29068a062b520baf;p=lhc%2Fweb%2Fwiklou.git Fix Bug #28082, Add Hooks to User::addGroup and User::removeGroup I propose to add two new hooks, one should be called when adding a group to a user, one when a group is removed. This allows MediaWiki for example to add/remove groups from a remote authentication/authorization service. Adapted provided patch. --- diff --git a/docs/hooks.txt b/docs/hooks.txt index a3705fb064..f3e9fa35db 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1757,6 +1757,11 @@ $user: the user who sent the message out $ip: IP of the user who sent the message out $u: the account whose new password will be set +'UserAddGroup': called when adding a group; return false to override +stock group addition. +$user: the user object that is to have a group added +$group: the group to add + 'UserArrayFromResult': called when creating an UserArray object from a database result &$userArray: set this to an object to override the default object returned @@ -1884,6 +1889,11 @@ $user: the user object _after_ logout (won't have name, ID, etc.) $inject_html: Any HTML to inject after the "logged out" message. $oldName: name of the user before logout (string) +'UserRemoveGroup': called when removing a group; return false to override +stock group removal. +$user: the user object that is to have a group removed +$group: the group to be removed + 'UserRights': After a user's group memberships are changed $user : User object that was changed $add : Array of strings corresponding to groups added diff --git a/includes/User.php b/includes/User.php index c9d196db58..94fb46262b 100644 --- a/includes/User.php +++ b/includes/User.php @@ -2160,17 +2160,18 @@ class User { * @param $group String Name of the group to add */ function addGroup( $group ) { - $dbw = wfGetDB( DB_MASTER ); - if( $this->getId() ) { - $dbw->insert( 'user_groups', - array( - 'ug_user' => $this->getID(), - 'ug_group' => $group, - ), - __METHOD__, - array( 'IGNORE' ) ); + if( wfRunHooks( 'UserAddGroup', array( &$this, &$group ) ) ) { + $dbw = wfGetDB( DB_MASTER ); + if( $this->getId() ) { + $dbw->insert( 'user_groups', + array( + 'ug_user' => $this->getID(), + 'ug_group' => $group, + ), + __METHOD__, + array( 'IGNORE' ) ); + } } - $this->loadGroups(); $this->mGroups[] = $group; $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) ); @@ -2185,13 +2186,14 @@ class User { */ function removeGroup( $group ) { $this->load(); - $dbw = wfGetDB( DB_MASTER ); - $dbw->delete( 'user_groups', - array( - 'ug_user' => $this->getID(), - 'ug_group' => $group, - ), __METHOD__ ); - + if( wfRunHooks( 'UserRemoveGroup', array( &$this, &$group ) ) ) { + $dbw = wfGetDB( DB_MASTER ); + $dbw->delete( 'user_groups', + array( + 'ug_user' => $this->getID(), + 'ug_group' => $group, + ), __METHOD__ ); + } $this->loadGroups(); $this->mGroups = array_diff( $this->mGroups, array( $group ) ); $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );