Index: trunk/extensions/NssMySQLAuth/NssMySQLAuthPlugin.php |
— | — | @@ -18,7 +18,9 @@ |
19 | 19 | $wgHooks['UserEffectiveGroups'][] = array( $wgAuth, 'onUserEffectiveGroups' ); |
20 | 20 | $wgHooks['UserGetEmail'][] = array( $wgAuth, 'onUserGetEmail' ); |
21 | 21 | $wgHooks['UserSetEmail'][] = array( $wgAuth, 'onUserSetEmail' ); |
| 22 | + $wgHooks['UserRights'][] = array( $wgAuth, 'onUserRights' ); |
22 | 23 | |
| 24 | + |
23 | 25 | wfLoadExtensionMessages( 'nssmysqlauth' ); |
24 | 26 | } |
25 | 27 | |
— | — | @@ -32,15 +34,23 @@ |
33 | 35 | } |
34 | 36 | |
35 | 37 | function userExists( $username ) { |
36 | | - if( isset( $this->users[$username] )) |
37 | | - return $this->users[$username]; |
38 | | - |
| 38 | + $this->loadUser( $username ); |
| 39 | + return $this->users[$username] !== false; |
| 40 | + } |
| 41 | + function getUid( $username ) { |
| 42 | + $this->loadUser( $username ); |
| 43 | + return $this->users[$username]; |
| 44 | + } |
| 45 | + |
| 46 | + function loadUser( $username ) { |
| 47 | + if ( isset( $this->users[$username] ) ) |
| 48 | + return; |
39 | 49 | $dbr = $this->getDB( DB_READ ); |
40 | | - return $this->users[$username] = |
41 | | - false !== $dbr->select( |
42 | | - 'passwd', 1, array( 'pwd_name' => $username ), |
| 50 | + $row = $dbr->selectRow( |
| 51 | + 'passwd', 'pwd_uid', array( 'pwd_name' => $username ), |
43 | 52 | __METHOD__ |
44 | 53 | ); |
| 54 | + $this->users[$username] = ($row === false ? false : $row->pwd_uid); |
45 | 55 | } |
46 | 56 | |
47 | 57 | function authenticate( $username, $password ) { |
— | — | @@ -148,6 +158,35 @@ |
149 | 159 | ); |
150 | 160 | } |
151 | 161 | |
| 162 | + function onUserRights( &$user, $addgroup, $removegroup ) { |
| 163 | + $uid = $this->getUid( $user->getName() ); |
| 164 | + if( $uid === false ) |
| 165 | + return true; |
| 166 | + |
| 167 | + $dbr = $this->getDB( DB_READ ); |
| 168 | + $res = $dbr->select( 'groups', 'grp_name', array(), __METHOD__ ); |
| 169 | + $groups = array(); |
| 170 | + while ( $row = $res->fetchObject() ) |
| 171 | + $groups[] = $row->grp_name; |
| 172 | + $res->free(); |
| 173 | + |
| 174 | + $addgroup = array_intersect( $groups, $addgroup ); |
| 175 | + $removegroup = array_intersect( $groups, $removegroup ); |
| 176 | + |
| 177 | + $dbw = $this->getDB( DB_WRITE ); |
| 178 | + foreach ( $addgroup as $group ) |
| 179 | + $dbw->insert( 'group_membership', array( |
| 180 | + 'gm_user' => $uid, |
| 181 | + 'gm_group' => $group, |
| 182 | + ), __METHOD__, 'IGNORE' ); |
| 183 | + foreach ( $removegroup as $group ) |
| 184 | + $dbw->delete( 'group_membership', array( |
| 185 | + 'gm_user' => $uid, |
| 186 | + 'gm_group' => $group, |
| 187 | + ), __METHOD__ ); |
| 188 | + return true; |
| 189 | + } |
| 190 | + |
152 | 191 | /** |
153 | 192 | * Create an account, returning a random password |
154 | 193 | */ |