Quick Start Guide
In this tutorial we're to use the Access Policy module to create a site with some gated content. This means that anonymous users can see content on listing pages but when they click it they will get an Access denied message.
Step by Step:
Step One: Update the anonymous user
- Go to /admin/people/roles
- Grant the following permission to the anonymous user:
- Access the Content overview page
Or feel free to create a new view if you prefer :D
Step Two: Create the Gated Access Policy
- If you haven’t already, download and install the Access policy module.
- Enable the access_policy_ui sub module.
- Go to /admin/people/access-policies
- Add a new Access Policy called "Gated"
- Click Edit permissions next to the Subscriber role and grant the permission:
- Gated: View any content assigned this access policy
- Click Save permissions
- Uncheck Apply access policy to queries.
- Under the 403 (access denied) response choose "403 message"
- Provide a custom message "This content is only available to subscribers."
- Save the Access policy.
Step Three: Set selection mode to Manual
A document-style approach to assigning access works really well in a case like this. So let's turn on "Manual" mode so that we can assign access from an "Access" tab.
- Go to /admin/people/access-policies
- Click Content settings
- Under Selection mode choose "Manual"
- Leave all the other settings in place and click save.
- Clear the Drupal cache.
Setup is complete. Now let's create some gated content!
Tip: After you've set up your access policy, you can disable the access_policy_ui sub module and continue to manage access from the permissions page.
Step Four: Create some gated content
- Go to /admin/content
- Create a new Basic page
- Click the Access tab next to Edit
- Set the access to “Gated” and click save.
This content will now appear on listing pages but, when anonymous users attempt to view it, they will see an access denied message.
Step Five: View gated content as an anonymous user
- Log out and go to /admin/content
- Note that it shows the the content in the view.
- Click the content
- It now will show you an access denied page with the message "This content is only available to subscribers". Pretty neat huh?!
Now let’s grant access to a subscriber!
Step Six: View gated content as a subscriber
- Go to /admin/people/roles
- Create a new Role called "Subscriber"
- Give them the following permissions
- View published content
- Access the Content overview page
- Add a new user with the Subscriber role.
- Log in as that user
- View the content that you created in step three
- Observe that you can now see the content!
Developer tip:
We've provided a useful drush command to help diagnose any issues that you may run into as you're building out your access policies.
drush access-policy:check-access [entity type] [id] [username]This will provide a table of operations, their access status and any relevant messages.
Access policies assigned to this entity: Me only
-------------------- -------- ----------------------------------------------------
Operation Access Message
-------------------- -------- ----------------------------------------------------
view Yes
view all revisions No The 'Authored by current user' access rule failed.
update No The 'Authored by current user' access rule failed.
delete No The 'Authored by current user' access rule failed.
-------------------- -------- ---------------------------------------------------- Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.