1

I want to change the permission of anonymous user to view content hits only for a blog section. I came across user_role_change_permissions(), but it is throwing an error when I use the following code.

 if ($user->uid == "0") {
   $custom_role = user_role_load_by_name('anonymous user'); 
   $custom_rid = $custom_role->rid; 
   $custom_permissions = array(
     'view content hits' => TRUE,
   );
  user_role_change_permissions($custom_rid, $custom_permissions);
 }

The error I get is the following:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module' cannot be null: INSERT INTO {role_permission} (rid, permission, module) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 1 [:db_insert_placeholder_1] => view content hits [:db_insert_placeholder_2] => ) in user_role_grant_permissions() (line 3154 of C:\xampp\htdocs\capacad\modules\user\user.module).

Notice: Undefined index: view content hits in user_role_grant_permissions() (line 3152 of C:\xampp\htdocs\capacad\modules\user\user.module).

I tried also with user_role_grant_permissions(), but it doesn't work too.

How do I set a new permission for a role?

2 Answers 2

1

It should be like this:

if($user->uid == "0"){
  $custom_role = user_role_load_by_name('anonymous user'); 
  $custom_rid = $custom_role->rid; 
  $custom_permissions = array('view content hits');
  user_role_change_permissions($custom_rid, $custom_permissions);
 }

Here you have given

$custom_permissions = array( 'view content hits' => TRUE, );

TRUE should not be there.

1
1

As described in user_role_change_permissions(), the second argument of user_role_change_permissions() ($permissions) is:

An associative array, where the key holds the permission name and the value determines whether to grant or revoke that permission. Any value that evaluates to TRUE will cause the permission to be granted. Any value that evaluates to FALSE will cause the permission to be revoked.

The array given as example makes clear what that description means.

array(
  'administer nodes' => 0,                // Revoke 'administer nodes'
  'administer blocks' => FALSE,           // Revoke 'administer blocks'
  'access user profiles' => 1,            // Grant 'access user profiles'
  'access content' => TRUE,               // Grant 'access content'
  'access comments' => 'access comments', // Grant 'access comments'
)

Your code is correct. The problem is that there isn't any module defining the view content hits permission. This causes user_role_grant_permission() to fail since it is using this code.

  $modules = user_permission_get_modules();
  // Grant new permissions for the role.
  foreach ($permissions as $name) {
    db_merge('role_permission')
      ->key(array(
        'rid' => $rid,
        'permission' => $name,
      ))
      ->fields(array(
        'module' => $modules[$name],
      ))
      ->execute();
  }

Without a module defining that permission, $modules[$name] is NULL, but the database field for module doesn't accept NULL values, and Drupal throws that error.
The warming error is caused by the code I shown, which tries to access $modules['view content hits'] when that is not a defined index for the $modules array.

In short, your code is correct, but you need to use a permission that a module is defining in its hook_permission() implementation. It is fine if you set a permission defined by a Drupal core module, or any third-party module. You cannot just set any random permission.

As side note, if the $user variable you are using is the global one, you can use the following code.

if (user_is_logged_in()) {
   $permissions = array(
     'view content hits' => TRUE,
   );

   $role = user_role_load_by_name('anonymous user'); 
   user_role_change_permissions($role->rid, $permissions);
 }

Otherwise, you can just use the following code, keeping in mind what I said in the rest of my answer.

if (!$user->uid) {
   $permissions = array(
     'view content hits' => TRUE,
   );

   $role = user_role_load_by_name('anonymous user'); 
   user_role_change_permissions($role->rid, $permissions);
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.