You are here

function eck_permissions_eck_access in Entity Construction Kit (ECK) 7.3

File

modules/eck_permissions/eck_permissions.module, line 229

Code

function eck_permissions_eck_access($op, $object_type, $object, $account) {
  $allow = FALSE;

  //we are only dealing with object specific permissions, general permission are

  //handled by eck
  if (isset($object)) {
    $roles = $account->roles;
    $perms = array();

    //first we want the user specific permissions
    $new = ECKPermission::loadAllByUID($account->uid);
    $perms = array_merge($perms, $new);
    foreach ($roles as $rid => $role) {
      $new = ECKPermission::loadAllByRole($rid);
      $perms = array_merge($perms, $new);
    }
    foreach ($perms as $p) {
      $ops = array(
        $op,
        "*",
      );
      foreach ($ops as $op) {
        $perm = $p->permission;
        $object_id = eck_permissions_object_id($object_type, $object);
        $cperm = "{$op} {$object_type}:{$object_id}";
        if ($object_type == "bundle") {
          watchdog("eck_permissions", "{$perm} -- {$cperm}");
        }
        $code = strcmp($perm, $cperm);
        if ($code == 0) {
          $allow = TRUE;
          watchdog("eck_permissions", "Allowed");
          break;
        }
        if ($object_type == "bundle") {
          watchdog("eck_permissions", $code);
        }
        if (!$allow) {

          //we also want to deal with general options of permissions
          $pieces = explode("|", $object_id);

          //how many possible permissions can be generated
          $binary_string = "";
          for ($i = 0; $i < count($pieces); $i++) {
            $binary_string .= 1;
          }
          $total_permissions = bindec($binary_string);
          for ($i = 0; $i <= $total_permissions; $i++) {
            $binary = decbin($i);
            $binary = strrev($binary);
            $pc = $pieces;
            for ($j = 0; $j < count($pieces); $j++) {
              $bit = substr($binary, $j, 1);
              if ($bit == 1) {
                $pc[$j] = "*";
              }
            }
            $new_object_id = implode("|", $pc);
            $cperm = "{$op} {$object_type}:{$new_object_id}";
            if (strcmp($perm, $cperm) == 0) {
              $allow = TRUE;
              break;
            }
          }
        }
      }
    }

    //should behaviors care about anything but entity permissions?

    //I guess we'll play it by ear
    if ($object_type == "entity" && get_class($object) == "ECKEntity" && !$allow) {
      $entity_type_name = $object
        ->entityType();
      $entity_type = EntityType::loadByName($entity_type_name);
      $behavior_access = eck_property_behavior_invoke_plugin($entity_type, 'permissions', array(
        'op' => $op,
        'entity' => $object,
        'permissions' => $perms,
        'account' => $account,
      ));
      foreach ($behavior_access as $access) {
        if ($access) {
          $allow = TRUE;
        }
      }
    }
  }
  return array(
    $allow,
  );
}