public static function validateConfig($config, $create = true) { if (null === ArrayUtils::get($config, 'dsn', null, true)) { if (null === ArrayUtils::getDeep($config, 'options', 'db', null, true)) { throw new BadRequestException('Database name must be included in the \'dsn\' or as an \'option\' attribute.'); } } return true; }
/** * Gets DN by username * * @param $username * @param $uidField * * @return string */ public function getUserDn($username, $uidField = 'uid') { $baseDn = $this->baseDn; $connection = $this->connection; $search = ldap_search($connection, $baseDn, '(' . $uidField . '=' . $username . ')'); $result = ldap_get_entries($connection, $search); $dn = ArrayUtils::getDeep($result, 0, 'dn'); return $dn; }
/** * Magic method to fetch any user value. * * @param string $method * @param array $args * * @return mixed * @throws NotFoundException */ public function __call($method, $args) { $key = strtolower(substr($method, 3)); $data = $this->getData(); if (in_array($key, ['dn', 'count'])) { return ArrayUtils::get($data, $key); } return ArrayUtils::getDeep($data, $key, 0); }
protected function getGroupByPrimaryGroupId($id) { $groups = $this->search("(&(objectCategory=group)(objectClass=group))", ['*', 'primarygrouptoken']); array_shift($groups); foreach ($groups as $group) { if (ArrayUtils::getDeep($group, 'primarygrouptoken', 0) === $id) { return new ADGroup($group); } } throw new NotFoundException('Group not found by primarygrouptoken [' . $id . ']'); }
/** * Give a normalized event, put any changed data from the payload back into the event * * @param PlatformEvent $event * @param array $exposedEvent * * @return $this */ public static function updateEventFromHandler(PlatformEvent &$event, array $exposedEvent = []) { // Did propagation stop? if (ArrayUtils::get($exposedEvent, 'stop_propagation', false)) { $event->stopPropagation(); } $request = ArrayUtils::getDeep($exposedEvent, 'request', 'body'); $response = ArrayUtils::get($exposedEvent, 'response', false); if (!$response) { // Log::debug( 'No response in exposed event' ); } if ($request) { if (!$event->isPostProcessScript()) { $event->setData($request); } $event->setRequestData($request); } if ($response) { if ($event->isPostProcessScript()) { $event->setData($response); } $event->setResponseData($response); } return $event; }
/** * @param string $name * @param mixed $value * @param array $validations * @param bool $for_update * @param ColumnSchema|null $field_info * * @return bool * @throws InternalServerErrorException * @throws BadRequestException */ protected static function validateFieldValue($name, $value, $validations, $for_update = false, $field_info = null) { if (is_array($validations)) { foreach ($validations as $key => $config) { $config = ArrayUtils::clean($config); $onFail = ArrayUtils::get($config, 'on_fail'); $throw = true; $msg = null; if (!empty($onFail)) { if (0 == strcasecmp($onFail, 'ignore_field')) { $throw = false; } else { $msg = $onFail; } } switch ($key) { case 'api_read_only': if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' is read only."; } throw new BadRequestException($msg); } return false; break; case 'create_only': if ($for_update) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' can only be set during record creation."; } throw new BadRequestException($msg); } return false; } break; case 'not_null': if (is_null($value)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value can not be null."; } throw new BadRequestException($msg); } return false; } break; case 'not_empty': if (!is_null($value) && empty($value)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value can not be empty."; } throw new BadRequestException($msg); } return false; } break; case 'not_zero': if (!is_null($value) && empty($value)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value can not be empty."; } throw new BadRequestException($msg); } return false; } break; case 'email': if (!empty($value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value must be a valid email address."; } throw new BadRequestException($msg); } return false; } break; case 'url': $sections = ArrayUtils::clean(ArrayUtils::get($config, 'sections')); $flags = 0; foreach ($sections as $format) { switch (strtolower($format)) { case 'path': $flags &= FILTER_FLAG_PATH_REQUIRED; break; case 'query': $flags &= FILTER_FLAG_QUERY_REQUIRED; break; } } if (!empty($value) && !filter_var($value, FILTER_VALIDATE_URL, $flags)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value must be a valid URL."; } throw new BadRequestException($msg); } return false; } break; case 'int': $min = ArrayUtils::getDeep($config, 'range', 'min'); $max = ArrayUtils::getDeep($config, 'range', 'max'); $formats = ArrayUtils::clean(ArrayUtils::get($config, 'formats')); $options = []; if (is_int($min)) { $options['min_range'] = $min; } if (is_int($max)) { $options['max_range'] = $max; } $flags = 0; foreach ($formats as $format) { switch (strtolower($format)) { case 'hex': $flags &= FILTER_FLAG_ALLOW_HEX; break; case 'octal': $flags &= FILTER_FLAG_ALLOW_OCTAL; break; } } $options = ['options' => $options, 'flags' => $flags]; if (!is_null($value) && false === filter_var($value, FILTER_VALIDATE_INT, $options)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is not in the valid range."; } throw new BadRequestException($msg); } return false; } break; case 'float': $decimal = ArrayUtils::get($config, 'decimal', '.'); $options['decimal'] = $decimal; $options = ['options' => $options]; if (!is_null($value) && !filter_var($value, FILTER_VALIDATE_FLOAT, $options)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is not an acceptable float value."; } throw new BadRequestException($msg); } return false; } break; case 'boolean': if (!is_null($value) && !filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is not an acceptable boolean value."; } throw new BadRequestException($msg); } return false; } break; case 'match': $regex = ArrayUtils::get($config, 'regexp'); if (empty($regex)) { throw new InternalServerErrorException("Invalid validation configuration: Field '{$name}' has no 'regexp'."); } $regex = base64_decode($regex); $options = ['regexp' => $regex]; if (!empty($value) && !filter_var($value, FILTER_VALIDATE_REGEXP, $options)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is invalid."; } throw new BadRequestException($msg); } return false; } break; case 'picklist': if (is_null($field_info) || empty($values = $field_info->picklist)) { throw new InternalServerErrorException("Invalid validation configuration: Field '{$name}' has no 'picklist' options in schema settings."); } if (!empty($value) && false === array_search($value, $values)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is invalid."; } throw new BadRequestException($msg); } return false; } break; case 'multi_picklist': if (is_null($field_info) || empty($values = $field_info->picklist)) { throw new InternalServerErrorException("Invalid validation configuration: Field '{$name}' has no 'picklist' options in schema settings."); } if (!empty($value)) { $delimiter = ArrayUtils::get($config, 'delimiter', ','); $min = ArrayUtils::get($config, 'min', 1); $max = ArrayUtils::get($config, 'max'); $value = DbUtilities::validateAsArray($value, $delimiter, true); $count = count($value); if ($count < $min) { if (empty($msg)) { $msg = "Field '{$name}' value does not contain enough selections."; } throw new BadRequestException($msg); } if (!empty($max) && $count > $max) { if (empty($msg)) { $msg = "Field '{$name}' value contains too many selections."; } throw new BadRequestException($msg); } foreach ($value as $item) { if (false === array_search($item, $values)) { if ($throw) { if (empty($msg)) { $msg = "Field '{$name}' value is invalid."; } throw new BadRequestException($msg); } return false; } } } break; } } } return true; }
/** * @param array $record * @param string|array $include List of keys to include in the output record * @param string|array $id_field Single or list of identifier fields * * @return array */ protected static function cleanRecord($record = [], $include = '*', $id_field = self::DEFAULT_ID_FIELD) { if ('*' == $include) { return $record; } // Check for $record['_id'] $id = ArrayUtils::get($record, $id_field, ArrayUtils::get($record, 'id'), false); // Check for $record['_rev'] $rev = ArrayUtils::get($record, static::REV_FIELD, ArrayUtils::get($record, 'rev', ArrayUtils::getDeep($record, 'value', 'rev'), false), false); $out = [$id_field => $id, static::REV_FIELD => $rev]; if (empty($include)) { return $out; } if (!is_array($include)) { $include = array_map('trim', explode(',', trim($include, ','))); } foreach ($include as $key) { if (0 == strcasecmp($key, $id_field) || 0 == strcasecmp($key, static::REV_FIELD)) { continue; } $out[$key] = ArrayUtils::get($record, $key); } return $out; }
/** * {@inheritdoc} */ public function getName() { return ArrayUtils::getDeep($this->getData(), 'name', 0); }