user_access($string, $account = NULL)
$string: The permission, such as "administer nodes", being checked for.
How can I find out a list of available $string values for user_access()
function?
As long as every module can define their own permissions, there is not a "strict" list of those string. You will need to "construct" it if you really need to have such a list programmatically.
You can run this script in a /devel/php page. (Of course, you need the Devel module.)
// Render role/permission overview:
$options = array();
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
print_r($module);
// Drupal 6
// if ($permissions = module_invoke($module, 'perm')) {
// print_r($permissions);
// }
// Drupal 7
if ($permissions = module_invoke($module, 'permission')) {
print_r($permissions);
}
}
As @Berdir said, the easiest way to see which permissions are available is to go to the permissions admin/people/permissions in Drupal 7.
The problem I kept running into is that I could never figure out what string I needed to supply as an 'access argument' to actually make use of the permission. Well, here is how you find the string name for the permissions you want to use (This example uses Google Chrome.)
Step one. Go to admin/people/permissions find the permission you would like to use and right click on a check box to the right of the permission you would like to use. Select 'Inspect Element' or just go look at the source.
Next look for the value of the check box and note the value. (In this case the string is 'create coupon content')
This is the string you need to supply as an access argument in hook_menu()
Example Code: (non-relevant items removed, don't forget title, callback, etc. in hook_menu())
function fsrsys_menu() {
$items = array();
$items['my-custom-url'] = array(
'access callback' => 'user_access',
'access arguments' => array('create coupon content'),
);
return $items;
}
Here's a D7 version of Haza's answer, modified to use DSM instead of print_r and to leave out modules that don't implement hook_permission:
// Render permission overview:
$options = array();
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
if ($permissions = module_invoke($module, 'permission')) {
// List only the modules that have permissions.
dsm($module);
dsm($permissions);
}
}
You can use user_permission_get_modules()
function, which returns an array of all defined permissions and their modules.
Determine the modules that permissions belong to.
Return value
An associative array in the format $permission => $module.
array_keys()
you obviously get only the requested information. You can then also use the result in user_role_grant_permissions
, for example to grant the admin role all permissions: user_role_grant_permissions(variable_get("user_admin_role"), array_keys(user_permission_get_modules()));
The easiest way is to check the permission page (D7: admin/people/permissions, D6: admin/user/permissions). Note that you directly use the displayed string in Drupal 6, for 7 you need to have a look at the checkbox name/value. This is because permissions now have a translated title and description in Drupal 7.
So if you want the name for the permission "View published content" in Drupal 7, the value is "access content" which is your permission name.
If you know which module the permission is coming from, you can also check the corresponding hook_perm() (D6) / hook_pemission() (D7) implementation of that module.
In many cases, you want to define your own pemissions and use these, you can do that with the above hooks.
Using the Devel Module
After installing and enabling the Devel module, navigate to admin/config/development/devel
. There is a setting titled, "Display machine names of permissions and modules". Be sure that is checked.
Now on the permissions page at people/permissions
, hover over the name of the permission you are questioning. It may delay a second or two, but then the string value will show.
Here is a simple form alter that will add the permission key to the name on the permissions page. That should make it easier to look it up. You will need to add it to a custom module and replace "mymodule" with the name of that module.
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_user_admin_permissions_alter(&$form, $form_state) {
foreach ($form['permission'] as $key => &$item) {
if (isset($item['#type']) && $item['#type'] == 'item') {
$item['#markup'] .= ' (' . $key . ')';
}
}
}
I created this JavaScript snippet that helps me with this. I tested it in Drupal 7 only on the Shiny theme.
It basically outputs the machine name, taken from the first checkbox on that name, on to the first cell, under the label. The script relies on jQuery, which shouldn't be a problem on Drupal 7.
I typically paste it on the Developer Tools console and run it from there. I might create a bookmarklet for it in future.
// Go through each of the rows on the permissin table
rows = jQuery('#permissions tr');
rows.each(function() {
// Get the first checkbox in the row.
checkbox = jQuery('.checkbox .form-checkbox', this).first();
// Get the first cell, containing the permission label.
nameCell = jQuery('td:first-child', this);
// In case this is not a permission row
if(checkbox.length == 0) {
return;
}
// Embed the permission maching name on permission label cell.
nameCell.append("<div><code>" + checkbox.val() + "</code></div>");
});
just drop in some module, you will see machine permissions at /admin/people/permissions
function YOURMODULE_form_user_admin_permissions_alter(&$form, &$form_state)
{
$perms = &$form['permission'];
$group_counter = 0;
foreach ($perms as $k => &$v) {
if ($k == $group_counter) { // skip fieldsets
$group_counter++;
continue;
}
$v['#markup'] .= ' (<span class="description" style="color:brown">'.$k.'</span>)';
}
}
regards