Пример #1
0
 /**
  * Handles all GET requests.
  *
  * @return array
  */
 protected function handleGET()
 {
     $this->parent->authenticateAdminUser();
     $groupName = $this->resource;
     $username = $this->request->getParameter('user');
     $fields = $this->request->getParameter(ApiOptions::FIELDS, ApiOptions::FIELDS_ALL);
     $attributes = [];
     if ('*' !== $fields) {
         $attributes = explode(',', $fields);
     }
     if (!empty($username)) {
         $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
         if ($asList) {
             $attributes = ['samaccountname'];
         }
         $resources = $this->provider->getGroups($username, $attributes);
     } else {
         if (empty($groupName)) {
             $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
             if ($asList) {
                 $attributes = ['samaccountname'];
             }
             $resources = $this->provider->listGroup($attributes);
         } else {
             $adGroup = $this->provider->getGroupByCn($groupName);
             $resources = $adGroup->getData($attributes);
         }
     }
     return ResourcesWrapper::cleanResources($resources);
 }
Пример #2
0
 /**
  * Handles DELETE action
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws BadRequestException
  * @throws \Exception
  */
 protected function handleDELETE()
 {
     $this->triggerActionEvent($this->response);
     $modelClass = $this->getModel();
     if (!empty($this->resource)) {
         $result = $modelClass::deleteById($this->resource, $this->request->getParameters());
     } elseif (!empty($ids = $this->request->getParameter(ApiOptions::IDS))) {
         $result = $modelClass::deleteByIds($ids, $this->request->getParameters());
     } elseif ($records = ResourcesWrapper::unwrapResources($this->getPayloadData())) {
         if (isset($records[0]) && is_array($records[0])) {
             $result = $modelClass::bulkDelete($records, $this->request->getParameters());
         } else {
             // this may be a list of ids
             $result = $modelClass::deleteByIds($records, $this->request->getParameters());
         }
     } else {
         throw new BadRequestException('No record(s) detected in request.');
     }
     $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
     $id = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
     $result = ResourcesWrapper::cleanResources($result, $asList, $id, ApiOptions::FIELDS_ALL);
     return $result;
 }
Пример #3
0
 /**
  * Handles GET action
  *
  * @return array
  * @throws NotFoundException
  */
 protected function handleGET()
 {
     if (empty($this->resource)) {
         $service = $this->request->getParameter('service');
         $type = $this->request->getParameter('type');
         $onlyScripted = $this->request->getParameterAsBool('only_scripted');
         if ($onlyScripted) {
             switch ($type) {
                 case 'process':
                     $scripts = EventScript::where('affects_process', 1)->lists('name')->all();
                     break;
                 case 'broadcast':
                     $scripts = EventScript::where('affects_process', 0)->lists('name')->all();
                     break;
                 default:
                     $scripts = EventScript::lists('name')->all();
                     break;
             }
             return ResourcesWrapper::cleanResources(array_values(array_unique($scripts)));
         }
         $results = $this->getEventMap();
         $allEvents = [];
         switch ($type) {
             case 'process':
                 $results = ArrayUtils::get($results, 'process', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             case 'broadcast':
                 $results = ArrayUtils::get($results, 'broadcast', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             default:
                 foreach ($results as $type => $services) {
                     foreach ($services as $serviceKey => $apis) {
                         if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                             unset($results[$type][$serviceKey]);
                         } else {
                             foreach ($apis as $path => $operations) {
                                 foreach ($operations['verb'] as $method => $events) {
                                     $allEvents = array_merge($allEvents, $events);
                                 }
                             }
                         }
                     }
                 }
                 break;
         }
         if (!$this->request->getParameterAsBool(ApiOptions::AS_LIST)) {
             return $results;
         }
         return ResourcesWrapper::cleanResources(array_values(array_unique($allEvents)));
     }
     $related = $this->request->getParameter(ApiOptions::RELATED);
     if (!empty($related)) {
         $related = explode(',', $related);
     } else {
         $related = [];
     }
     //	Single script by name
     $fields = [ApiOptions::FIELDS_ALL];
     if (null !== ($value = $this->request->getParameter(ApiOptions::FIELDS))) {
         $fields = explode(',', $value);
     }
     if (null === ($foundModel = EventScript::with($related)->find($this->resource, $fields))) {
         throw new NotFoundException("Script not found.");
     }
     return ResponseFactory::create($foundModel->toArray(), $this->nativeFormat);
 }
Пример #4
0
 /**
  * Handles DELETE actions.
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  * @throws BadRequestException
  */
 protected function handleDELETE()
 {
     $force = $this->request->getParameterAsBool('force', false);
     if (empty($this->folderPath)) {
         // delete just folders and files from the container
         if (!empty($content = ResourcesWrapper::unwrapResources($this->request->getPayloadData()))) {
             $result = $this->deleteFolderContent($content, '', $force);
         } else {
             throw new BadRequestException('No resources given for delete.');
         }
     } else {
         if (empty($this->filePath)) {
             // delete directory of files and the directory itself
             // multi-file or folder delete via post data
             if (!empty($content = ResourcesWrapper::unwrapResources($this->request->getPayloadData()))) {
                 $result = $this->deleteFolderContent($content, $this->folderPath, $force);
             } else {
                 $this->driver->deleteFolder($this->container, $this->folderPath, $force);
                 $result = ['name' => basename($this->folderPath), 'path' => $this->folderPath];
             }
         } else {
             // delete file from permanent storage
             $this->driver->deleteFile($this->container, $this->filePath);
             $result = ['name' => basename($this->filePath), 'path' => $this->filePath];
         }
     }
     return ResourcesWrapper::cleanResources($result);
 }
Пример #5
0
 /**
  * Handles GET action
  *
  * @return array
  * @throws NotFoundException
  */
 protected function handleGET()
 {
     $data = null;
     $related = $this->request->getParameter(ApiOptions::RELATED);
     if (!empty($related)) {
         $related = explode(',', $related);
     } else {
         $related = [];
     }
     $meta = [];
     if (!empty($this->resource)) {
         //	Single resource by ID
         $data = $this->retrieveById($this->resource, $related);
     } else {
         if (!empty($ids = $this->request->getParameter(ApiOptions::IDS))) {
             $data = $this->retrieveByIds($ids, $related);
         } else {
             if (!empty($records = ResourcesWrapper::unwrapResources($this->getPayloadData()))) {
                 if (isset($records[0]) && is_array($records[0])) {
                     $data = $this->retrieveByRecords($records, $related);
                 } else {
                     // this may be a list of ids
                     $data = $this->retrieveByIds($ids, $related);
                 }
             } else {
                 $data = $this->retrieveByRequest($related);
                 if ($this->request->getParameterAsBool(ApiOptions::INCLUDE_COUNT)) {
                     $meta['count'] = count($data);
                 }
             }
         }
     }
     if ($this->request->getParameterAsBool(ApiOptions::INCLUDE_SCHEMA)) {
         /** @var BaseSystemModel $model */
         $model = $this->getModel();
         $meta['schema'] = $model->getTableSchema()->toArray();
     }
     $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
     $id = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
     $data = ResourcesWrapper::cleanResources($data, $asList, $id, ApiOptions::FIELDS_ALL, !empty($meta));
     if (!empty($meta)) {
         $data['meta'] = $meta;
     }
     return $data;
 }
Пример #6
0
<?php

if (\Request::method() === 'POST') {
    $pin = \Request::input('pin');
    $state = \Request::input('state');
    if (empty($pin) && $pin !== 0 || empty($state) && $state !== 0) {
        throw new \DreamFactory\Core\Exceptions\BadRequestException('Please provide pin and state data');
    }
    $gpio = new \a15lam\RpiGpio\GPIO();
    $result = $gpio->write($pin, $state);
    $result = empty($result) ? ['success' => true] : $result;
    return \DreamFactory\Core\Utility\ResourcesWrapper::cleanResources($result);
} else {
    throw new \DreamFactory\Core\Exceptions\BadRequestException('Only POST is accepted');
}
Пример #7
0
 /**
  * @return array
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  * @throws \DreamFactory\Core\Exceptions\RestException
  */
 protected function handleDelete()
 {
     if (empty($this->resource)) {
         // not currently supported, maybe batch opportunity?
         return false;
     }
     $this->triggerActionEvent($this->response);
     if (false === ($tableName = $this->doesTableExist($this->resource, true))) {
         throw new NotFoundException('Table "' . $this->resource . '" does not exist in the database.');
     }
     if (!empty($this->resourceId)) {
         return $this->deleteRecordById($tableName, $this->resourceId, $this->options);
     }
     $ids = ArrayUtils::get($this->options, ApiOptions::IDS);
     if (!empty($ids)) {
         $result = $this->deleteRecordsByIds($tableName, $ids, $this->options);
     } else {
         $records = ResourcesWrapper::unwrapResources($this->payload);
         if (!empty($records)) {
             $result = $this->deleteRecords($tableName, $records, $this->options);
         } else {
             $filter = ArrayUtils::get($this->options, ApiOptions::FILTER);
             if (!empty($filter)) {
                 $params = ArrayUtils::get($this->options, ApiOptions::PARAMS, []);
                 $result = $this->deleteRecordsByFilter($tableName, $filter, $params, $this->options);
             } else {
                 if (!ArrayUtils::getBool($this->options, ApiOptions::FORCE)) {
                     throw new BadRequestException('No filter or records given for delete request.');
                 }
                 return $this->truncateTable($tableName, $this->options);
             }
         }
     }
     $meta = ArrayUtils::get($result, 'meta');
     unset($result['meta']);
     $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
     $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
     $result = ResourcesWrapper::cleanResources($result, $asList, $idField, ApiOptions::FIELDS_ALL, !empty($meta));
     if (!empty($meta)) {
         $result['meta'] = $meta;
     }
     return $result;
 }
Пример #8
0
 /**
  * @return array|bool
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  */
 protected function handleDELETE()
 {
     $payload = $this->request->getPayloadData();
     $fields = $this->request->getParameter(ApiOptions::FIELDS);
     if (empty($this->resource)) {
         $tables = $this->request->getParameter(ApiOptions::IDS);
         if (empty($tables)) {
             $tables = ResourcesWrapper::unwrapResources($payload);
         }
         if (empty($tables)) {
             throw new BadRequestException('No data in schema delete request.');
         }
         $result = $this->deleteTables($tables);
         $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
         $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
         $result = ResourcesWrapper::cleanResources($result, $asList, $idField, $fields);
     } else {
         if (false === ($tableName = $this->doesTableExist($this->resource, true))) {
             throw new NotFoundException('Table "' . $this->resource . '" does not exist in the database.');
         }
         if (empty($this->resourceId)) {
             $this->deleteTable($tableName);
             $result = ['success' => true];
         } else {
             $this->deleteField($tableName, $this->resourceId);
             $result = ['success' => true];
         }
     }
     return $result;
 }
Пример #9
0
 /**
  * Handles GET action
  *
  * @return mixed
  */
 protected function handleGET()
 {
     $resources = $this->getResources();
     if (is_array($resources)) {
         $includeAccess = $this->request->getParameterAsBool(ApiOptions::INCLUDE_ACCESS);
         $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
         $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
         $fields = $this->request->getParameter(ApiOptions::FIELDS);
         if (!$asList && $includeAccess) {
             foreach ($resources as &$resource) {
                 if (is_array($resource)) {
                     $name = ArrayUtils::get($resource, $idField);
                     $resource['access'] = VerbsMask::maskToArray($this->getPermissions($name));
                 }
             }
         }
         return ResourcesWrapper::cleanResources($resources, $asList, $idField, $fields);
     }
     return $resources;
 }