r84134 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84133‎ | r84134 | r84135 >
Date:23:13, 16 March 2011
Author:mah
Status:resolved (Comments)
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -1757,6 +1757,11 @@
17581758 $ip: IP of the user who sent the message out
17591759 $u: the account whose new password will be set
17601760
 1761+'UserAddGroup': called when adding a group; return false to override
 1762+stock group addition.
 1763+$user: the user object that is to have a group added
 1764+$group: the group to add
 1765+
17611766 'UserArrayFromResult': called when creating an UserArray object from a
17621767 database result
17631768 &$userArray: set this to an object to override the default object returned
@@ -1884,6 +1889,11 @@
18851890 $inject_html: Any HTML to inject after the "logged out" message.
18861891 $oldName: name of the user before logout (string)
18871892
 1893+'UserRemoveGroup': called when removing a group; return false to override
 1894+stock group removal.
 1895+$user: the user object that is to have a group removed
 1896+$group: the group to be removed
 1897+
18881898 'UserRights': After a user's group memberships are changed
18891899 $user : User object that was changed
18901900 $add : Array of strings corresponding to groups added
Index: trunk/phase3/includes/User.php
@@ -2160,17 +2160,18 @@
21612161 * @param $group String Name of the group to add
21622162 */
21632163 function addGroup( $group ) {
2164 - $dbw = wfGetDB( DB_MASTER );
2165 - if( $this->getId() ) {
2166 - $dbw->insert( 'user_groups',
2167 - array(
2168 - 'ug_user' => $this->getID(),
2169 - 'ug_group' => $group,
2170 - ),
2171 - __METHOD__,
2172 - array( 'IGNORE' ) );
 2164+ if( wfRunHooks( 'UserAddGroup', array( &$this, &$group ) ) ) {
 2165+ $dbw = wfGetDB( DB_MASTER );
 2166+ if( $this->getId() ) {
 2167+ $dbw->insert( 'user_groups',
 2168+ array(
 2169+ 'ug_user' => $this->getID(),
 2170+ 'ug_group' => $group,
 2171+ ),
 2172+ __METHOD__,
 2173+ array( 'IGNORE' ) );
 2174+ }
21732175 }
2174 -
21752176 $this->loadGroups();
21762177 $this->mGroups[] = $group;
21772178 $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
@@ -2185,13 +2186,14 @@
21862187 */
21872188 function removeGroup( $group ) {
21882189 $this->load();
2189 - $dbw = wfGetDB( DB_MASTER );
2190 - $dbw->delete( 'user_groups',
2191 - array(
2192 - 'ug_user' => $this->getID(),
2193 - 'ug_group' => $group,
2194 - ), __METHOD__ );
2195 -
 2190+ if( wfRunHooks( 'UserRemoveGroup', array( &$this, &$group ) ) ) {
 2191+ $dbw = wfGetDB( DB_MASTER );
 2192+ $dbw->delete( 'user_groups',
 2193+ array(
 2194+ 'ug_user' => $this->getID(),
 2195+ 'ug_group' => $group,
 2196+ ), __METHOD__ );
 2197+ }
21962198 $this->loadGroups();
21972199 $this->mGroups = array_diff( $this->mGroups, array( $group ) );
21982200 $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );

Follow-up revisions

RevisionCommit summaryAuthorDate
r84196Follow up r84134 document reference passing.mah19:22, 17 March 2011
r90247follow up r84134 — removing passing $this by referencemah23:36, 16 June 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:59, 17 March 2011

Must $this be passed by reference? Documentation should also say which parameters are passed as a reference.

#Comment by MarkAHershberger (talk | contribs)   19:19, 17 March 2011

Compare the documentation and arg passing for GetBlockedStatus which passes &$this. But, you're right, $group should be documented.

#Comment by Platonides (talk | contribs)   22:41, 18 March 2011

There's no point in passing the User object by reference, since it's an object. That kind of thing was only needed on PHP 4.

Status & tagging log