0

I am trying to only grant user the access to the site if one of the user fields have a particular value.

If not, the site should show an access denied.

Is there an efficient way to achieve this?

0

Let's say there is a specific path you want this rule to apply to, e.g. /special

<?php

function my_module_menu()
{

    $items['special'] = array(
        'title' => 'Special Page',
        'page callback' => 'special_page',
        // Permission in next line will be overridden by the check inside the page callback function.
        'access arguments' => array('access content'),
        'type' => MENU_SUGGESTED_ITEM,
        );
}

function special_page()
{
    global $user;
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'user');
    $query->propertyCondition('uid', $user->uid, '=');
    $query->fieldCondition('field_special_field', 'value', 'required_value', '=');
    $query->range(0, 1);
    $result = $query->execute();

    if (isset($result['user'])) {
        // User meets the condition
        // Do whatsoever, show special content ..

    } else {
        // Deny access
        drupal_access_denied();

    }
}

?>

Make sure to download and enable the Entity API module to use the above code.

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.