/**
  * Authenticates valid user.
  *
  * @return array
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\UnauthorizedException
  */
 protected function handlePOST()
 {
     $serviceName = $this->getPayloadData('service');
     if (empty($serviceName)) {
         $serviceName = $this->request->getParameter('service');
     }
     if (!empty($serviceName)) {
         $service = ServiceHandler::getService($serviceName);
         $serviceModel = Service::find($service->getServiceId());
         $serviceType = $serviceModel->serviceType()->first();
         $serviceGroup = $serviceType->group;
         if (!in_array($serviceGroup, [ServiceTypeGroups::OAUTH, ServiceTypeGroups::LDAP])) {
             throw new BadRequestException('Invalid login service provided. Please use an OAuth or AD/Ldap service.');
         }
         if ($serviceGroup === ServiceTypeGroups::LDAP) {
             $credentials = ['username' => $this->getPayloadData('username'), 'password' => $this->getPayloadData('password')];
             return $service->handleLogin($credentials, $this->getPayloadData('remember_me'));
         } elseif ($serviceGroup === ServiceTypeGroups::OAUTH) {
             $oauthCallback = $this->request->getParameterAsBool('oauth_callback');
             if (!empty($oauthCallback)) {
                 return $service->handleOAuthCallback();
             } else {
                 return $service->handleLogin($this->request->getDriver());
             }
         }
     } else {
         $credentials = ['email' => $this->getPayloadData('email'), 'password' => $this->getPayloadData('password'), 'is_sys_admin' => false];
         return $this->handleLogin($credentials, boolval($this->getPayloadData('remember_me')));
     }
 }
 /**
  * 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());
     }
 }
Beispiel #3
0
 /**
  * Package schemas for export.
  *
  * @return bool
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  */
 private function packageSchemas()
 {
     if (!empty($this->exportSchemas)) {
         $schemas = [];
         foreach ($this->exportSchemas as $serviceName => $component) {
             if (is_array($component)) {
                 $component = implode(',', $component);
             }
             if (is_numeric($serviceName)) {
                 /** @type Service $service */
                 $service = Service::find($serviceName);
             } else {
                 /** @type Service $service */
                 $service = Service::whereName($serviceName)->whereDeletable(1)->first();
             }
             if (!empty($service) && !empty($component)) {
                 if ($service->type === 'sql_db') {
                     $schema = ServiceHandler::handleRequest(Verbs::GET, $serviceName, '_schema', ['ids' => $component]);
                     $schemas[] = ['name' => $serviceName, 'table' => $this->resourceWrapped ? $schema[$this->resourceWrapper] : $schema];
                 }
             }
         }
         if (!empty($schemas) && !$this->zip->addFromString('schema.json', json_encode(['service' => $schemas], JSON_UNESCAPED_SLASHES))) {
             throw new InternalServerErrorException("Can not include database schema in package file.");
         }
         return true;
     }
     return false;
 }