Fix Bug #28082, Add Hooks to User::addGroup and User::removeGroup
authorMark A. Hershberger <mah@users.mediawiki.org>
Wed, 16 Mar 2011 23:13:38 +0000 (23:13 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Wed, 16 Mar 2011 23:13:38 +0000 (23:13 +0000)
    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
includes/User.php

index a3705fb..f3e9fa3 100644 (file)
@@ -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
index c9d196d..94fb462 100644 (file)
@@ -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 ) );