« How to use the Version Control and Bulk Export features of Views | Main | Advanced help patch for Views »

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:


$handler->override_option('access', array(
'type' => 'role',
));

To this:


$handler->override_option('access', array(
'type' => 'role',
'role' => array(
'3' => 3,
'4' => 4,
),
));

I've used python to accomplish this:

#! /usr/bin/python/

#execfile("/blah/modify_permissions.py")

marker = " $handler->override_option('access', array(\n"

try:
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:
fo.write(item)

except IOError:

print 'Can\'t open file for reading.'
#sys.exit(0)

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:

biblio_views
taxonomy_term

Posted by kkwaiser at November 15, 2010 09:54 AM

Comments

Login to leave a comment. Create a new account.