November 15, 2010
Bulk Modify Access Permissions of Views in Drupal
I'm an idiot and I'm not going to explain why.
Drupal's Views module allows you to limit access to views based upon user Role or by some permission setting. The catch is that the global permissions settings will override those of individual views. Unfortunately, I didn't know this when I started using Views and I have a lot of views I want accessible to Anonymous users and a few views I want accessible only to particular roles.
What are the implications of this:
1) I need to switch my Global permissions so that only the Admin can "access all views"
2) I then need to adjust the Access rights of every single view I've constructed.
There are two options to accomplish 2):
a) Click through every single view and modify the Access settings
b) Export all the views, run a script that modifies the code and then re-import the views
This post is a How To for number 2b):
1) Export all of the views you want to modify. In this case, I want identical permissions on the views I'm going to modify. Specifically, they will all go from no Access settings to access to Anonymous and Authenticated.
2) Use a script to cycle through the my_views.views_default.inc file and change this:
'type' => 'role',
'type' => 'role',
'role' => array(
'3' => 3,
'4' => 4,
I've used python to accomplish this:
marker = " $handler->override_option('access', array(\n"
f1 = open('/blah/sites/all/modules/my_views/my_views.views_default.inc').readlines()
# f1 = open("/blah/myview_test.txt").readlines()
fo = open("/blah/new_view.txt", 'w')
for index, line in enumerate(f1):
if line == marker:
del f1[ index+1]
f1.insert( index+1, " 'type' => 'role',\n")
f1.insert( index+2, " 'role' => array(\n")
f1.insert( index+3, " '3' => 3,\n")
f1.insert( index+4, " '4' => 4,\n")
f1.insert( index+5, " ),\n")
print index, line
for item in f1:
print 'Can\'t open file for reading.'
3) Creating a module of your exported views if you haven't already. Otherwise, place the new views_default.inc file in that module directory. Enable the module and revert all views.
4) These views are hard-coded, will need to fix manually:
Posted by kkwaiser at November 15, 2010 09:54 AM