It seems both of these functions are used to check whether a user has some previledge.
And the difference isn't obvious to me.
Can someone elaborate?
It seems both of these functions are used to check whether a user has some previledge.
And the difference isn't obvious to me.
Can someone elaborate?
hook_perm defines the possible permissions then you can go to admin/user/permissions assign the permissions to specific roles and then use user_access whether the user got the permission (belongs to a role which contains the permission).
hook_perm
lets you add custom permissions through a module. These permissions then appear when configuring user roles. user_access
is about determining whether a user has access to particular permissions.
If you implement hook_perm, this will define permissions for the given module, like this
/**
* Implementation of hook_perm().
*/
function yourmodule_perm() {
return array('can select', 'can update', 'can delete');
}
However the permissions per se, mean nothing... One way of controling what user can and can't do is thanks to user_access:
// @ some other module function
if (user_access('can delete')){
// delete stuff
} else {
drupal_access_denied();
}
Also, hook_perm defined permissions can be used while setting up your module menu hook_menu by doing:
// @hook_menu
$items['modulepath'] = array(
'title' => 'modulename',
'page callback' => 'module_function',
'access callback' => 'user_access',
'access arguments' => array('can select'),
'type' => MENU_NORMAL_ITEM,
);
Don't forget to configure your user perms at: admin/user/permissions