/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     if (!class_exists('DreamFactory\\Core\\ADLdap\\Services\\ADLdap')) {
         $this->error('Command unavailable. Please install \'dreamfactory/df-adldap\' package to use this command.');
         return;
     }
     try {
         $serviceName = $this->argument('service');
         $username = $this->option('username');
         $password = $this->option('password');
         /** @type ADLdap $service */
         $service = ServiceHandler::getService($serviceName);
         $serviceModel = Service::find($service->getServiceId());
         $serviceType = $serviceModel->serviceType()->first();
         $serviceGroup = $serviceType->group;
         if ($serviceGroup !== ServiceTypeGroups::LDAP) {
             throw new BadRequestException('Invalid service name [' . $serviceName . ']. Please use a valid Active Directory service');
         }
         $this->line('Contacting your Active Directory server...');
         $service->authenticateAdminUser($username, $password);
         $this->line('Fetching Active Directory groups...');
         $groups = $service->getDriver()->listGroup(['dn', 'description']);
         $roles = [];
         foreach ($groups as $group) {
             $dfRole = RoleADLdap::whereDn($group['dn'])->first();
             if (empty($dfRole)) {
                 $role = ['name' => static::dnToRoleName($group['dn']), 'description' => $group['description'], 'is_active' => true, 'role_adldap_by_role_id' => [['dn' => $group['dn']]]];
                 $this->info('|--------------------------------------------------------------------');
                 $this->info('| DN: ' . $group['dn']);
                 $this->info('| Role Name: ' . $role['name']);
                 $this->info('| Description: ' . $role['description']);
                 $this->info('|--------------------------------------------------------------------');
                 $roles[] = $role;
             }
         }
         $roleCount = count($roles);
         if ($roleCount > 0) {
             $this->warn('Total Roles to import: [' . $roleCount . ']');
             if ($this->confirm('The above roles will be imported into your DreamFactroy instance based on your Active Directory groups. Do you wish to continue?')) {
                 $this->line('Importing Roles...');
                 $payload = ResourcesWrapper::wrapResources($roles);
                 ServiceHandler::handleRequest(Verbs::POST, 'system', 'role', ['continue' => true], $payload);
                 $this->info('Successfully imported all Active Directory groups as Roles.');
             } else {
                 $this->info('Aborted import process. No Roles were imported');
             }
         } else {
             if (count($groups) > 0 && $roleCount === 0) {
                 $this->info('All groups found on the Active Directory server are already imported.');
             } else {
                 $this->warn('No group was found on Active Directory server.');
             }
         }
     } catch (RestException $e) {
         $this->error($e->getMessage());
         if ($this->option('verbose')) {
             $this->error(print_r($e->getContext(), true));
         }
     } catch (\Exception $e) {
         $this->error($e->getMessage());
     }
 }
Esempio n. 2
0
 /**
  * Handles GET action
  *
  * @return array
  * @throws NotFoundException
  */
 protected function handleGET()
 {
     $wrapper = ResourcesWrapper::getWrapper();
     $ids = $this->request->getParameter(ApiOptions::IDS);
     $records = ResourcesWrapper::unwrapResources($this->getPayloadData());
     $data = null;
     $related = $this->request->getParameter('related');
     if (!empty($related)) {
         $related = explode(',', $related);
     } else {
         $related = [];
     }
     /** @var BaseSystemModel $modelClass */
     $modelClass = $this->getModel();
     /** @var BaseSystemModel $model */
     $model = new $modelClass();
     $pk = $model->getPrimaryKey();
     //	Single resource by ID
     if (!empty($this->resource)) {
         $foundModel = $modelClass::with($related)->find($this->resource);
         if ($foundModel) {
             $data = $foundModel->toArray();
         }
     } else {
         if (!empty($ids)) {
             /** @var Collection $dataCol */
             $dataCol = $modelClass::with($related)->whereIn($pk, explode(',', $ids))->get();
             $data = $dataCol->toArray();
             $data = ResourcesWrapper::wrapResources($data);
         } else {
             if (!empty($records)) {
                 $pk = $model->getPrimaryKey();
                 $ids = [];
                 foreach ($records as $record) {
                     $ids[] = ArrayUtils::get($record, $pk);
                 }
                 /** @var Collection $dataCol */
                 $dataCol = $modelClass::with($related)->whereIn($pk, $ids)->get();
                 $data = $dataCol->toArray();
                 $data = ResourcesWrapper::wrapResources($data);
             } else {
                 //	Build our criteria
                 $criteria = $this->getSelectionCriteria();
                 $data = $model->selectByRequest($criteria, $related);
                 $data = ResourcesWrapper::wrapResources($data);
             }
         }
     }
     if ($this->request->getParameterAsBool(ApiOptions::INCLUDE_COUNT)) {
         if (isset($data[$wrapper])) {
             $data['meta']['count'] = count($data[$wrapper]);
         } elseif (!empty($data)) {
             $data['meta']['count'] = 1;
         }
     }
     if (!empty($data) && $this->request->getParameterAsBool(ApiOptions::INCLUDE_SCHEMA)) {
         $data['meta']['schema'] = $model->getTableSchema()->toArray();
     }
     return ResponseFactory::create($data, $this->nativeFormat);
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 protected function handleGET()
 {
     if ($this->request->getParameterAsBool(ApiOptions::AS_ACCESS_LIST)) {
         return ResourcesWrapper::wrapResources($this->getAccessList());
     }
     return parent::handleGET();
 }
Esempio n. 4
0
 /**
  * Handles GET actions.
  *
  * @return \DreamFactory\Core\Utility\ServiceResponse
  */
 protected function handleGET()
 {
     if (empty($this->folderPath) && empty($this->filePath) && $this->request->getParameterAsBool(ApiOptions::AS_ACCESS_LIST)) {
         return ResourcesWrapper::wrapResources($this->getAccessList());
     }
     if (empty($this->filePath)) {
         //Resource is the root/container or a folder
         if ($this->request->getParameterAsBool('zip')) {
             $zipFileName = $this->driver->getFolderAsZip($this->container, $this->folderPath);
             FileUtilities::sendFile($zipFileName, true);
             unlink($zipFileName);
             // output handled by file handler, short the response here
             $this->setNativeFormat(null);
             $result = null;
         } elseif ($this->request->getParameterAsBool('include_properties')) {
             $result = $this->driver->getFolderProperties($this->container, $this->folderPath);
         } else {
             $result = $this->driver->getFolder($this->container, $this->folderPath, $this->request->getParameterAsBool('include_files', true), $this->request->getParameterAsBool('include_folders', true), $this->request->getParameterAsBool('full_tree', false));
             $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST);
             $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier());
             $fields = $this->request->getParameter(ApiOptions::FIELDS, ApiOptions::FIELDS_ALL);
             $result = ResourcesWrapper::cleanResources($result, $asList, $idField, $fields, true);
         }
     } else {
         //Resource is a file
         if ($this->request->getParameterAsBool('include_properties', false)) {
             // just properties of the file itself
             $content = $this->request->getParameterAsBool('content', false);
             $result = $this->driver->getFileProperties($this->container, $this->filePath, $content);
         } else {
             $download = $this->request->getParameterAsBool('download', false);
             // stream the file, exits processing
             $this->streamFile($this->container, $this->filePath, $download);
             // output handled by file handler, short the response here
             $this->setNativeFormat(null);
             $result = null;
         }
     }
     return $result;
 }
 /**
  * @return array|bool
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  */
 protected function handleGET()
 {
     $refresh = $this->request->getParameterAsBool(ApiOptions::REFRESH);
     if (empty($this->resource)) {
         $tables = $this->request->getParameter(ApiOptions::IDS);
         if (empty($tables)) {
             $tables = ResourcesWrapper::unwrapResources($this->request->getPayloadData());
         }
         if (!empty($tables)) {
             $result = $this->describeTables($tables, $refresh);
             $result = ResourcesWrapper::wrapResources($result);
         } else {
             $result = parent::handleGET();
         }
     } 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)) {
             $result = $this->describeTable($tableName, $refresh);
         } else {
             $result = $this->describeField($tableName, $this->resourceId, $refresh);
         }
     }
     return $result;
 }
Esempio n. 6
0
 /**
  * Returns CSV payload data
  *
  * @param null $key
  * @param null $default
  *
  * @return array|mixed|null
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  */
 protected function csv($key = null, $default = null)
 {
     if (!empty($this->contentAsArray)) {
         if (null === $key) {
             return $this->contentAsArray;
         } else {
             return ArrayUtils::get($this->contentAsArray, $key, $default);
         }
     }
     $content = $this->getContent();
     $data = DataFormatter::csvToArray($content);
     if (!empty($content) && empty($data)) {
         throw new BadRequestException('Invalid CSV payload supplied.');
     }
     $payload = ResourcesWrapper::wrapResources($data);
     // Store the content so that formatting is only done once.
     $this->contentAsArray = empty($payload) ? [] : $payload;
     $this->content = $content;
     if (null === $key) {
         if (empty($payload)) {
             return [];
         } else {
             return $payload;
         }
     } else {
         if (empty($payload)) {
             return $default;
         } else {
             return ArrayUtils::get($payload, $key, $default);
         }
     }
 }
Esempio n. 7
0
 /**
  *
  * @return array
  */
 public static function listServices()
 {
     return ResourcesWrapper::wrapResources(Service::available());
 }