tags:

views:

175

answers:

3

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?

A: 

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).

chx
A: 

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.

Finbarr
+4  A: 

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

acmatos
while hook_perm defines the permissions available, user_access checks whether the user has the permission enabled (checks whether any of the user roles has the permission enabled in admin/user/permissions)
barraponto