/** * Handle a request * * @param ManifestJob $command * * * @return bool * @throws \Exception */ public function handle(ManifestJob $command) { \Log::debug('[dfe:manifest] begin'); if ($command->showManifest()) { $_manifest = ClusterManifest::createFromFile(base_path() . DIRECTORY_SEPARATOR . ConsoleDefaults::CLUSTER_MANIFEST_FILE); $_result = !$_manifest->existed() ? ErrorPacket::create() : SuccessPacket::make($_manifest->toArray()); // And then show it... if ($_manifest->existed()) { \Log::debug(' * Manifest found: ' . print_r($_manifest->all(), true)); } else { \Log::info(' * No manifest file found. Nothing to show.'); } } else { try { $_key = $command->noKeys() ? false : AppKey::createKey($command->getOwnerId(), $command->getOwnerType()); if ($_key) { $command->getOutput()->getVerbosity() == OutputInterface::VERBOSITY_VERBOSE && $command->getOutput()->writeln(' - generated client-id and secret: ' . $_key->client_id); } if ($command->createManifest()) { // Create a new manifest... $_manifest = ClusterManifest::make(base_path(), ['cluster-id' => config('dfe.cluster-id'), 'default-domain' => config('provisioning.default-domain'), 'signature-method' => config('dfe.signature-method'), 'storage-root' => config('provisioning.storage-root'), 'console-api-url' => config('dfe.security.console-api-url'), 'console-api-key' => config('dfe.security.console-api-key'), 'client-id' => !$_key ? null : $_key->client_id, 'client-secret' => !$_key ? null : $_key->client_secret]); $command->setResult($_result = SuccessPacket::make($_manifest->toArray(), Response::HTTP_CREATED)); } } catch (\Exception $_ex) { $command->setResult($_result = ErrorPacket::create(Response::HTTP_BAD_REQUEST)); } } \Log::debug('[dfe:manifest] end'); return $command; }
/** * Handle a request * * @param RegisterJob $command * * @return mixed * @throws \Exception */ public function handle(RegisterJob $command) { $_key = config('dfe.security.console-api-key'); try { $_owner = $command->getOwnerInfo(); // Generate the key $_key = AppKey::createKey($_owner->id, $_owner->type, ['server_secret' => $_key]); $this->debug('[dfe:register] Successfully created app key "' . $_key->client_id . '"'); $_result = SuccessPacket::make($_key->toArray(), Response::HTTP_CREATED); } catch (\Exception $_ex) { $this->error('[dfe:register] Exception while creating key: ' . $_ex->getMessage()); $_result = ErrorPacket::create(Response::HTTP_BAD_REQUEST, $_ex); } $command->publishResult($command->getJobId(), $_result); return $_result; }
/** * @param \stdClass|object $result * * @return array */ protected function createResponseFromCallResult($result) { // All out failure if (false === $result) { Flasher::setIf('Unable to reach console.', false); return ErrorPacket::create(); } if (is_object($result)) { $result = (array) $result; } // An array? if (is_array($result)) { if (array_key_exists('error', $result)) { Flasher::setIf(data_get($result['error'], 'message', 'An error occurred during the request.'), false); return ErrorPacket::create($result['error'], data_get($result['error'], 'code')); } if (array_key_exists('response', $result)) { if (false === data_get($result, 'success')) { Flasher::setIf('The request failed to complete.', false); return ErrorPacket::create($result['response']); } Flasher::setIf('Your request completed successfully.'); return SuccessPacket::create($result['response']); } } return ErrorPacket::create($result, Response::HTTP_SERVICE_UNAVAILABLE, 'Invalid response from console.'); }
/** * @param mixed|null $contents * @param int $code * * @return array */ public function success($contents = null, $code = Response::HTTP_OK) { return SuccessPacket::create($contents, $code); }
/** * Standardized user creation method * * @param \Illuminate\Http\Request $request * @param bool $validate If false, no validation is done. * * @return \DreamFactory\Enterprise\Common\Packets\ErrorPacket|\DreamFactory\Enterprise\Common\Packets\SuccessPacket */ public static function register(Request $request, $validate = true) { $_email = $request->input('email', $request->input('email_addr_text')); $_first = $request->input('firstname', $request->input('first_name_text')); $_last = $request->input('lastname', $request->input('last_name_text')); $_password = $request->input('password', $request->input('password_text')); $_nickname = $request->input('nickname', $request->input('nickname_text', $_first)); $_company = $request->input('company', $request->input('company_name_text')); $_phone = $request->input('phone', $request->input('phone_text')); if ($validate) { if (empty($_email) || empty($_password) || empty($_first) || empty($_last)) { /** @noinspection PhpUndefinedMethodInspection */ Log::error('missing required fields from partner post', ['payload' => $request->input()]); throw new \InvalidArgumentException('Missing required fields'); } if (false === filter_var($_email, FILTER_VALIDATE_EMAIL)) { /** @noinspection PhpUndefinedMethodInspection */ Log::error('invalid email address "' . $_email . '"', ['payload' => $request->input()]); throw new \InvalidArgumentException('Email address invalid'); } } // See if we know this cat... if (null !== ($_user = User::byEmail($_email)->first())) { // Existing user found, don't add to database... $_values = $_user->toArray(); unset($_values['password_text'], $_values['external_password_text']); /** @noinspection PhpUndefinedMethodInspection */ Log::info('existing user attempting registration through api', ['user' => $_values]); return $_user; } // Create a user account try { /** @type User $_user */ /** @noinspection PhpUndefinedMethodInspection */ $_user = DB::transaction(function () use($request, $_first, $_last, $_email, $_password, $_nickname, $_phone, $_company) { /** @noinspection PhpUndefinedMethodInspection */ $_user = User::create(['first_name_text' => $_first, 'last_name_text' => $_last, 'email_addr_text' => $_email, 'nickname_text' => $_nickname, 'password_text' => Hash::make($_password), 'phone_text' => $_phone, 'company_name_text' => $_company]); if (null === ($_appKey = AppKey::mine($_user->id, OwnerTypes::USER))) { $_appKey = AppKey::create(['key_class_text' => AppKeyClasses::USER, 'owner_id' => $_user->id, 'owner_type_nbr' => OwnerTypes::USER, 'server_secret' => config('dfe.security.console-api-key')]); } // Update the user with the key info and activate $_user->api_token_text = $_appKey->client_id; $_user->active_ind = 1; $_user->save(); return $_user; }); $_values = $_user->toArray(); unset($_values['password_text'], $_values['external_password_text']); /** @noinspection PhpUndefinedMethodInspection */ Log::info('new user registered', ['user' => $_values]); return $validate ? SuccessPacket::create($_user, Response::HTTP_CREATED) : $_user; } catch (\Exception $_ex) { if (false !== ($_pos = stripos($_message = $_ex->getMessage(), ' (sql: '))) { $_message = substr($_message, 0, $_pos); } /** @noinspection PhpUndefinedMethodInspection */ Log::error('database error creating user from ops-resource post: ' . $_message); return $validate ? ErrorPacket::create(null, Response::HTTP_INTERNAL_SERVER_ERROR, $_message) : null; } }
/** * Remove the specified resource from storage. * * @param int $id * * @return \Illuminate\Http\Response */ public function destroy($id) { $_resource = call_user_func([$this->model, 'find'], $id); if (empty($_resource)) { return ErrorPacket::create(); } return SuccessPacket::create($_resource->delete(), Response::HTTP_OK); }
/** {@InheritDoc} */ public function destroy($id) { try { $_model = call_user_func([$this->model, 'findOrFail'], $id); if (!$_model->delete()) { return ErrorPacket::create(\Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR, 'Delete of id "' . $id . '" failed.'); } return SuccessPacket::make(); } catch (\Exception $_ex) { return ErrorPacket::create(); } }
/** * Converts data to JSON and spits it out * * @param array $data * @param int $totalRows * @param int $totalFiltered * * @return \Illuminate\Http\JsonResponse */ protected function respond($data, $totalRows = null, $totalFiltered = null) { // Don't wrap if there are no totals if (!$this->dataTables || null === $totalRows && null === $totalFiltered) { return SuccessPacket::make($data); } $_response = ['draw' => (int) array_get($_REQUEST, 'draw'), 'recordsTotal' => (int) ($totalRows ?: 0), 'recordsFiltered' => (int) ($totalFiltered ?: $totalRows), 'data' => $this->prepareResponseData($data)]; return \Response::json($_response); }