home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (user.eval.inc)

This example Drupal source code file (user.eval.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

account, as, case, false, foreach, function, if, isset, php, return, rid, roles, true, variable

The user.eval.inc Drupal example source code

<?php
// $Id: user.eval.inc,v 1.1.2.1 2010/12/16 09:30:43 fago Exp $

/**
 * @file
 * Contains rules integration for the user module needed during evaluation.
 *
 * @addtogroup rules
 * @{
 */

/**
 * Condition user: condition to check whether user has particular roles
 */
function rules_condition_user_has_role($account, $roles, $operation = 'AND') {
  switch ($operation) {
    case 'OR':
      foreach ($roles as $rid) {
        if (isset($account->roles[$rid])) {
          return TRUE;
        }
      }
      return FALSE;

    case 'AND':
      foreach ($roles as $rid) {
        if (!isset($account->roles[$rid])) {
          return FALSE;
        }
      }
      return TRUE;
  }
}

/**
 * Condition: User is blocked.
 */
function rules_condition_user_is_blocked($account) {
  return $account->status == 0;
}

/**
 * Action: Adds roles to a particular user.
 */
function rules_action_user_add_role($account, $roles) {
  if ($account->uid) {
    // Get role list (minus the anonymous)
    $role_list = user_roles(TRUE);

    foreach ($roles as $rid) {
      $account->roles[$rid] = $role_list[$rid];
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Action: Remove roles from a given user.
 */
function rules_action_user_remove_role($account, $roles) {
  if ($account->uid) {
    foreach ($roles as $rid) {
      // If the user has this role, remove it.
      if (isset($account->roles[$rid])) {
        unset($account->roles[$rid]);
      }
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Action: Block a user.
 */
function rules_action_user_block($account) {
  $account->status = 0;
  drupal_session_destroy_uid($account->uid);
}

/**
 * Action: Unblock a user.
 */
function rules_action_user_unblock($account) {
  $account->status = 1;
}

/**
 * @}
 */

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal user.eval.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.