Comments

bfroehle’s picture

Ooops. Forgot --no-prefix last time. Updated patch.

bfroehle’s picture

Priority: Major » Normal
StatusFileSize
new2.8 KB

Attached is a unit test routine. Won't pass until the patch in #1 is applied.

----
For the curious, the bug comes from commit c407765a4dae4ab103f5a7b13ceaf9237efbe4a2
Author: dries
Date: Thu Apr 30 16:10:10 2009 +0000

- Patch #394594: DBTNG: User module by Berdir: additional conversion to the new database abstraction layer plus clean-up.
----

Beware: this is my first stab at writing a unit test, so it's probably not the prettiest. For example the assertEqual( ... , 0) should probably be assertFalse(...). Etc.

Status: Needs review » Needs work

The last submitted patch, 0001-user.test-Test-getting-and-setting-authmaps.patch, failed testing.

damien tournoud’s picture

Priority: Normal » Major

D'oh. This means that this feature is nearly completely broken. Raising to major.

bfroehle’s picture

Status: Needs work » Needs review

The unit test in #2 passes after application of the patch in #1 on my system. Changing status to needs review.

moshe weitzman’s picture

Please combine into one patch so the bot shows a green result. http://drupal.org/patch/create

Core does not use this function, and basically neither should anyone else in D7. How did you find the bug? ... The Distributed Auth system was ripped out. Thanks still for writing tests.

bfroehle’s picture

Combined patch is attached. Description in the patch follows:

955414: (user_get_authmaps) Fix key value pair ordering.

The comments for user_get_authmaps describe the function as
returning a key-value pair '%module' => '%authname'. This agrees
with functionality in past releases.

In #394594: DBTNG: User module, the key value pair ordering was (inadvertently)
reversed to be '%authname' => '%module'.

Included also is a unit test for user_get_authmaps and
user_set_authmaps which verifies that authmaps can be set and
removed, and their output has the proper key-value pair ordering.

bfroehle’s picture

Priority: Normal » Major

moshe: I discovered the bug while working to update the CAS module to D7. I assume using authmaps is still the preferred way to interface with external authentication systems.

(Changing priority to major since this completely breaks external authentication schemes.)

moshe weitzman’s picture

Thanks. I'd actually recommend maintaining your own map table. These last vestiges of dist auth will be removed in D8. I don't really see much benefit in using this core table.

bfroehle’s picture

Thanks moshe. The OpenID module in core does make use of the core authmap table, but uses it's own SQL code to query it (and hence is unaffected by this bug).

bfroehle’s picture

StatusFileSize
new4.09 KB

A follow up to patch in #7, this time replacing the static strings in the test routine with ones generated by $this->randomName().

metzlerd’s picture

Hi moshe, Dave... cas module maintainer here, In light of #10. Is it still the case that the authmap table will dissapear? We've been modeling a lot of what we do here based on how drupal does openID support. Just trying to plan for the future. A few hundred university sites still use this authentication mechanism, and I'll need to start planning for a data migration path before drupal 8 gets released if that's the case.

moshe weitzman’s picture

Yes, I think that OpenId should gets its own map table as well in D8. There is no longer any benefit to centralization of mappings since login never consults this table. Contrib modules and OpenID need to inject custom validators into the login form and do login check there. Actually, some contrib modules may choose to forego the map table if they know that drupal username will always match external username.

bfroehle’s picture

I'm posting a summary of the problem in hopes of getting some action on this. The bug and patch are straightforward.

Summary of the bug:

The function user_get_authmaps claims to return "an associative array with module as key and username as value." However the current code returns an associative array with the username as key and module as the value, effectively reversing the the key/value pair.

This key/value reversal problem stems from a mistake in #394594: DBTNG: User module. You can clearly see the bug introduced in the diff

--- /cvs/drupal/drupal/modules/user/user.module	2009/04/29 08:04:24	1.981
+++ /cvs/drupal/drupal/modules/user/user.module	2009/04/30 16:10:10	1.982
@@ -1520,14 +1524,8 @@
  *   An associative array with module as key and username as value.
  */
 function user_get_authmaps($authname = NULL) {
-  $result = db_query("SELECT authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname));
-  $authmaps = array();
-  $has_rows = FALSE;
-  foreach ($result as $authmap) {
-    $authmaps[$authmap->module] = $authmap->authname;
-    $has_rows = TRUE;
-  }
-  return $has_rows ? $authmaps : 0;
+  $authmaps = db_query("SELECT authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed();
+  return count($authmaps) ? $authmaps : 0;
 }
 
 /**

Recommended solution:

The fix is to replace fetchAllKeyed() with fetchAllKeyed(1,0).

The proposed patch in #11 fixes the key/value ordering and provides a unit test to ensure that user_get_authmaps and user_set_authmaps work as advertised. I'm uploading a rebased version of this patch since user.module has seen some changes in the last week.

bfroehle’s picture

Carlos8f and I reworked the unit test at this morning's coding session at BAD Camp.

carlos8f’s picture

Status: Needs review » Needs work

We can remove the setUp() method, I think other than that it is RTBC.

I worked with @bfroehle on this, mainly as an exercise in test writing. It's just a unit test; core doesn't use this so no functional test is possible.

I'm surprised these API functions still exist. user_save() no longer does any authmap stuff. Even in D6, user_get_authmaps() is not used in core. At this point, the authmaps table might as well belong to the OpenID schema (a module I think should not even be in core). But that's neither here nor there :)

carlos8f’s picture

protected $admin_user isn't used, either.

bfroehle’s picture

Status: Needs work » Needs review
StatusFileSize
new3.93 KB

carlos8f: You're right about removing the setUp() and $admin_user parts as they are not used.

Updated patch attached, which is #15 plus the changes carlos8f suggested in #16 and #17.

bfroehle’s picture

Edit: Okay that was weird... ended up with a double post some how.

Status: Needs review » Needs work

The last submitted patch, 955414-18-user_get_authmaps-Fix-key-value-pair-ordering.patch, failed testing.

carlos8f’s picture

Something messed up. The patch link doesn't even link to anywhere! Try re-uploading the patch.

bfroehle’s picture

#18, re-uploaded. I'm scratching my head about how the file just disappeared, but whatever.

bfroehle’s picture

Status: Needs work » Needs review

And changing the status to needs review, to trigger the bot. The file seems to actually exist this time... ;)

bfroehle’s picture

StatusFileSize
new13.34 KB

Since there seems to be minimal interest in fixing this regression, here's a patch which rips out all of the external authentication functions ala moshe in #9... Why ship them in a broken state when you can just remove them instead?

(obviously, I'd prefer to just have #22 committed, but I'm a bit frustrated).

carlos8f’s picture

Frustration does not merit scope creep. Let's not go from a simple bugfix to a major API change, if possible. #22 is on my list of stuff to review, but there are criticals now that take precedence. Please be patient :)

Status: Needs review » Needs work

The last submitted patch, 955414-remove-distauth.patch, failed testing.

carlos8f’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.93 KB

#18 looks great, let's RTBC that. Re-uploaded.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

I might very well be missing something, but...

-  $authmaps = db_query("SELECT authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed();
+  $authmaps = db_query("SELECT authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed(1,0);

Couldn't we just flip the order of "authname, module" in the SELECT clause so we don't need to use that weird 1,0 pattern?

If not, then I'd prefer to see a comment here explaining why we do this, because it looks odd, and there's information in the commit message explaining it that's not captured here.

chx’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.93 KB

We could.

webchick’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +API change

Ok, cool. Thanks.

Technically, at this point, we should be fixing documentation rather than changing the API. However, I think it's clear that this was an accidental oversight during DBTNG conversion, and there are only a handful of possible modules affected by this. It looks like not even OpenID is affected by this, which seems a bit odd? Hm.

Anyway, Committed to HEAD.

Note to Randy: This changes the format of the array coming back from user_get_authmaps() so that it's "An associative array with module as key and username as value." like the documentation says. Previous, it was an array with username as key and module as value, due to a bug introduced during DBTNG conversion. Affects only modules implementing external authentication systems. Not sure it's worth an announcement, I'd defer to bfroehle on this point.

rfay’s picture

Thanks for the note about the API change.

I'm getting a bit overwhelmed by these, but I'll try to get a compendium of them together. Wow, are they coming "willy nilly" :-)

webchick’s picture

Well. It's the last chance to fix bugs that require them, so they'll probably continue coming in for another week. They are not "willy-nilly," though. I've moved most of these to D8 except in cases where they cause regressions or can't be solved another way.

bfroehle’s picture

@rfay: I don't think this needs to be announced, as it is rarely used and a regression from D6 in the DBTNG conversion. Also, any module developer is likely to just mimic openid's implementation which never even calls the function.

Status: Fixed » Closed (fixed)
Issue tags: -API change

Automatically closed -- issue fixed for 2 weeks with no activity.