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.

Comments

Version:7.x-1.x-dev» 7.x-1.5

I came across this myself and am testing this patch right now.
If all good, I'll apply the patch to the HEAD dev.

I 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…

Version:7.x-1.5» 7.x-1.x-dev
StatusFileSize
new1.89 KB

Patch 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]

Issue summary:View changes

fixed grammar