Created
May 25, 2010 12:21
-
-
Save hugowetterberg/413068 to your computer and use it in GitHub Desktop.
Quick Drupal snippet/memo for bulk adding content permissions for roles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This update enables the content_permissions module and gives all anonymous | |
* and authenticated users view access to all fields; and all 'administrator'- | |
* users edit access. | |
*/ | |
function skaneutil_update_6003() { | |
require('includes/skaneutil_updateutils.inc'); | |
drupal_install_modules(array('content_permissions')); | |
$fields = array_keys(content_fields()); | |
$edit_permissions = array(); | |
$view_permissions = array(); | |
foreach ($fields as $field) { | |
$edit_permissions[] = "edit {$field}"; | |
$view_permissions[] = "view {$field}"; | |
} | |
$result = db_query('SELECT r.rid, r.name, p.perm FROM {role} AS r | |
INNER JOIN {permission} AS p ON r.rid=p.rid'); | |
while ($role = db_fetch_object($result)) { | |
switch ($role->rid) { | |
case DRUPAL_ANONYMOUS_RID: | |
case DRUPAL_AUTHENTICATED_RID: | |
$can_view = array_merge(explode(', ', $role->perm), $view_permissions); | |
skaneutil_update_utils_update_permissions($role->rid, $can_view); | |
break; | |
default: | |
if ($role->name == 'administrator') { | |
$can_edit = array_merge(explode(', ', $role->perm), $edit_permissions); | |
skaneutil_update_utils_update_permissions($role->rid, $can_edit); | |
} | |
break; | |
} | |
} | |
return array(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function skaneutil_update_utils_update_permissions($rid, $permissions) { | |
db_query('DELETE FROM {permission} WHERE rid = %d', $rid); | |
if (count($permissions)) { | |
// Sanitize and sort our permissions | |
$permissions = array_unique($permissions); | |
sort($permissions); | |
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", array( | |
':rid' => $rid, | |
':perm' => implode(', ', $permissions), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment