[Drupal] How to define user permissions in Drupal

| | 1 min read

User permissions are permissions that allows or restrict the user in accessing a feature. We can define our own permissions in our custom module using the hook_permission() function.

How permissions are defined?

As said above hook_permission() is used to declare permissions. Below code explains the hook_permission().

 * Implements hook_permission().
function yourmodule_permission() {
  return array(
    'permission string1' => array(
      'title' => t('description of permission'),
    'permission string2' => array(
      'title' => t('description of permission'),

How hook_permission(). works?

Here we define 2 permission strings using our hook_permission(). But how you use it to restrict or allow to any feature? Lets check whether our permissions are defines or not, just go to (admin/people) option and click the permission tab, there lists all the permissions (both custom and pre-defined) for that project. Give appropriate access rights to the string that you had created at Administration » People » permissions. when a user logs in ,these permissions invokes and you can check that user`s permission using the function user_access('permission string'); this will return true when the user has the permission string ticked in the permissions list else false. So our code will be like this:

function yourmodule_block_view() {
  if(user_access('permission string 1')) {
    //content of the block

that's it, you had limited the scope for accessing that block`s view!!!