/** * Connects to the database. Options are specified in the $config instance variable. * * @return boolean Connected. * @throws MissingConnectionException */ public function connect() { if ($this->connected !== true) { if (Set::check($this->config, 'login')) { $this->config = Set::insert($this->config, 'request.uri.user', Set::get($this->config, 'login')); } if (Set::check($this->config, 'password')) { $this->config = Set::insert($this->config, 'request.uri.pass', Set::get($this->config, 'password')); } try { $this->Socket = new HttpSocket($this->config); $this->connected = true; } catch (SocketException $e) { throw new MissingConnectionException(array('class' => $e->getMessage())); } } return $this->connected; }
/** * Takes in a flat array and returns a nested array * * @param mixed $data * @param array $options Options are: * children - the key name to use in the resultset for children * idPath - the path to a key that identifies each entry * parentPath - the path to a key that identifies the parent of each entry * root - the id of the desired top-most result * @return array of results, nested * @link */ public static function nest($data, $options = array()) { if (!$data) { return $data; } $alias = key(current($data)); $options += array('idPath' => "/{$alias}/id", 'parentPath' => "/{$alias}/parent_id", 'children' => 'children', 'root' => null); $return = $idMap = array(); $ids = Set::extract($data, $options['idPath']); $idKeys = explode('/', trim($options['idPath'], '/')); $parentKeys = explode('/', trim($options['parentPath'], '/')); foreach ($data as $result) { $result[$options['children']] = array(); $id = Set::get($result, $idKeys); $parentId = Set::get($result, $parentKeys); if (isset($idMap[$id][$options['children']])) { $idMap[$id] = array_merge($result, (array) $idMap[$id]); } else { $idMap[$id] = array_merge($result, array($options['children'] => array())); } if (!$parentId || !in_array($parentId, $ids)) { $return[] =& $idMap[$id]; } else { $idMap[$parentId][$options['children']][] =& $idMap[$id]; } } if ($options['root']) { $root = $options['root']; } else { $root = Set::get($return[0], $parentKeys); } foreach ($return as $i => $result) { $id = Set::get($result, $idKeys); $parentId = Set::get($result, $parentKeys); if ($id !== $root && $parentId != $root) { unset($return[$i]); } } return array_values($return); }
/** * Checks if the given $aro has access to action $action in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $action Action (defaults to *) * @return boolean Success (true if ARO has access to action in ACO, false otherwise) * @access public */ function check($aro, $aco, $action = "*") { if ($aro == null || $aco == null) { return false; } $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema()); $aroPath = $this->Aro->node($aro); $acoPath = new Set($this->Aco->node($aco)); if (empty($aroPath) || empty($acoPath)) { trigger_error("DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING); return false; } if ($acoPath->get() == null || $acoPath->get() == array()) { trigger_error("DbAcl::check() - Failed ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING); return false; } $aroNode = $aroPath[0]; $acoNode = $acoPath->get(); $acoNode = $acoNode[0]; if ($action != '*' && !in_array('_' . $action, $permKeys)) { trigger_error(sprintf(__("ACO permissions key %s does not exist in DbAcl::check()", true), $action), E_USER_NOTICE); return false; } $inherited = array(); $acoIDs = $acoPath->extract('{n}.' . $this->Aco->alias . '.id'); for ($i = 0; $i < count($aroPath); $i++) { $permAlias = $this->Aro->Permission->alias; $perms = $this->Aro->Permission->find('all', array('conditions' => array("{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'], "{$permAlias}.aco_id" => $acoIDs), 'order' => array($this->Aco->alias . '.lft' => 'desc'), 'recursive' => 0)); if (empty($perms)) { continue; } else { $perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias); foreach ($perms as $perm) { if ($action == '*') { foreach ($permKeys as $key) { if (!empty($perm)) { if ($perm[$key] == -1) { return false; } elseif ($perm[$key] == 1) { $inherited[$key] = 1; } } } if (count($inherited) === count($permKeys)) { return true; } } else { switch ($perm['_' . $action]) { case -1: return false; case 0: continue; break; case 1: return true; break; } } } } } return false; }
/** * Takes in a flat array and returns a nested array * * @param mixed $data * @param string $idPath * @param string $parentPath * @param string $childrenKey * @return array of results, nested */ public static function nest($data, $idPath = '/id', $parentPath = '/parent_id', $childrenKey = 'children') { if (!$data) { return $data; } $return = $idMap = array(); $ids = Set::extract($data, $idPath); $idKeys = explode('/', trim($idPath, '/')); $parentKeys = explode('/', trim($parentPath, '/')); foreach ($data as $result) { $result[$childrenKey] = array(); $id = Set::get($result, $idKeys); $parentId = Set::get($result, $parentKeys); if (isset($idMap[$id][$childrenKey])) { $idMap[$id] = array_merge($result, (array) $idMap[$id]); } else { $idMap[$id] = array_merge($result, array($childrenKey => array())); } if (!$parentId || !in_array($parentId, $ids, true)) { $return[] =& $idMap[$id]; } else { $idMap[$parentId][$childrenKey][] =& $idMap[$id]; } } return array_values($return); }
/** * Checks if the given $aro has access to action $action in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $action Action (defaults to *) * @return boolean Success (true if ARO has access to action in ACO, false otherwise) * @access public */ function check($aro, $aco, $action = "*") { if ($aro == null || $aco == null) { return false; } $permKeys = $this->_getAcoKeys($this->Aro->Permission->loadInfo()); $aroPath = $this->Aro->node($aro); $acoPath = new Set($this->Aco->node($aco)); if (empty($aroPath) || empty($acoPath)) { trigger_error("DB_ACL::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING); return false; } if ($acoPath->get() == null || $acoPath->get() == array()) { trigger_error("DB_ACL::check() - Failed ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING); return false; } $aroNode = $aroPath[0]; $acoNode = $acoPath->get(); $acoNode = $acoNode[0]; if ($action != '*' && !in_array('_' . $action, $permKeys)) { trigger_error(sprintf(__("ACO permissions key %s does not exist in DB_ACL::check()", true), $action), E_USER_NOTICE); return false; } $inherited = array(); for ($i = 0; $i < count($aroPath); $i++) { $perms = $this->Aro->Permission->findAll(array($this->Aro->Permission->name . '.aro_id' => $aroPath[$i][$this->Aro->name]['id'], $this->Aro->Permission->name . '.aco_id' => $acoPath->extract('{n}.' . $this->Aco->name . '.id')), null, array($this->Aco->name . '.lft' => 'desc'), null, null, 0); if (empty($perms)) { continue; } else { foreach (Set::extract($perms, '{n}.' . $this->Aro->Permission->name) as $perm) { if ($action == '*') { foreach ($permKeys as $key) { if (!empty($perm)) { if ($perm[$key] == -1) { return false; } elseif ($perm[$key] == 1) { $inherited[$key] = 1; } } } if (count($inherited) === count($permKeys)) { return true; } } else { switch ($perm['_' . $action]) { case -1: return false; case 0: continue; break; case 1: return true; break; } } } } } return false; }