Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
A bunch of our sites (we run a lot on the same codebase) were intermittently throwing an exception on module enable and disable:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'anonymous user' for key 'name': INSERT INTO {role} (name, weight) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => anonymous user [:db_insert_placeholder_1] => 5 ) in drupal_write_record() (line 7036 of /mnt/www/html/cam8001/docroot/includes/common.inc).
I tracked this down to secure_permissions_build_roles()
. In some cases, it seems like secure_permissions thinks that some roles are not yet created, when they are. I'm not sure what causes this, as when I run debugging code in devel/php, 'secure_permissions_get_roles()' and 'user_roles()' return exactly the same thing.
This band-aid solution stops the exceptions being thrown, and will only insert roles if they don't already exist in the user_roles table.
Comment | File | Size | Author |
---|---|---|---|
#4 | secure_permissions-duplicate_role_exception-1744274-4.patch | 1.89 KB | bellesmanieres |
secure_permissions-duplicate_role_exception.patch | 993 bytes | Cameron Tod |
Comments
Comment #1
Cameron Tod CreditAttribution: Cameron Tod commentedComment #2
Vincenzo CreditAttribution: Vincenzo commentedI came across this myself and am testing this patch right now.
If all good, I'll apply the patch to the HEAD dev.
Comment #3
bellesmanieres CreditAttribution: bellesmanieres commentedI experienced the same pb. In my case pb came from enabling i18n at some point. Given that secure_permissions fetches roles using user_roles, it ends up with translated role name, which don't match those in db. One solution would probably be to gather the untranslated role names instead everywhere secure_permissions uses user_roles, as the provided patch fixes the db error, but (in my case at least) prevented auth and anon permissions from being rebuilt correctly.
Will give it a try and provide a patch soon…
Comment #4
bellesmanieres CreditAttribution: bellesmanieres commentedPatch attached. Got a feeling it might also fix #1772484: On sites with a non-English default language, secure permissions cannot be rebuilt
[edit]I mean the multilanguage related issue part of #1772484: On sites with a non-English default language, secure permissions cannot be rebuilt. The patch itself that removes the return if no perms are defined for a role still is valid[/edit]
Comment #4.0
bellesmanieres CreditAttribution: bellesmanieres commentedfixed grammar
Comment #5
danquah CreditAttribution: danquah commentedJust ran into this issue, applied the patch and verified that it works as advertised with a debugger.
Comment #7
kreynen CreditAttribution: kreynen commented