From 282870288937e20be0c7200f29068a062b520baf Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Wed, 16 Mar 2011 23:13:38 +0000 Subject: [PATCH] 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. --- docs/hooks.txt | 10 ++++++++++ includes/User.php | 36 +++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-) 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 ) ); -- 2.20.1