0

I am having trouble figuring out how to set permissions for users I personally did not programmatically create in my site installation profile. Can someone please help?

I would like to set 'View published content' as true for the ANONYMOUS USER user role in the .install file.

I am using the code below to grant permissions; but I do not know how to access permissions for the ANONYMOUS USER role.

       // Create the roles.
$roles['Content editor'] = array(
    'name' => 'Content editor',
    'weight' => $weight++,
    'permissions' => $base_permissions,
);
foreach ($roles as $role) {
    $role_object = new stdClass();
    $role_object->name = $role['name'];
    $role_object->weight = $role['weight'];

    // Save the role.
    user_role_save($role_object);

    // Grant permissions.
    if (!empty($role['permissions'])) {
        user_role_grant_permissions($role_object->rid, $role['permissions']);
    }
3

There is a constant for the anonymous RID named DRUPAL_ANONYMOUS_RID.

So, you would be able to use something like:

user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('your permission 1'));
1

@Patrick Ryan's answer is great for individual permissions, but I would also suggest looking at https://www.drupal.org/project/secure_permissions and https://www.drupal.org/project/profiler_builder. Those tools are really helpful to export and manage all your permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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