What is the relationship between user_access and hook_perm in drupal?

It seems that both of these functions are used to check if the user has some prevalence.

And the difference is not obvious to me.

Can someone clarify?

+3
source share
3 answers

If you implement hook_perm, this will determine the permissions for this module, such as

/**
 * Implementation of hook_perm().
 */
function yourmodule_perm() {
  return array('can select', 'can update', 'can delete');
}

However, the permissions themselves do not matter ... One way to control what the user can and cannot do is to user_access:

// @ some other module function
if (user_access('can delete')){
  // delete stuff
} else {
  drupal_access_denied();
}

In addition, when setting up your module’s menu, you hook_menucan use the hook_perm-defined permissions option :

// @hook_menu
$items['modulepath'] = array(
    'title'            => 'modulename',
    'page callback'    => 'module_function',
    'access callback'  => 'user_access',
    'access arguments' =>  array('can select'),
    'type'             => MENU_NORMAL_ITEM,
);

Remember to configure your user access in: admin / user / permissions

+8

hook_perm , admin/user/permissions, , user_access, ( , ).

+1

hook_permallows you to add user permissions through the module. These permissions then appear when you configure user roles. user_accessis to determine if the user has access to certain permissions.

+1
source

Source: https://habr.com/ru/post/1745225/


All Articles