/** * @param $user User * @param $emailServiceId * @param $emailTemplateId * @param bool|true $deleteOnError * * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException */ protected static function sendConfirmation($user, $emailServiceId, $emailTemplateId, $deleteOnError = true) { try { if (empty($emailServiceId)) { throw new InternalServerErrorException('No email service configured for user invite. See system configuration.'); } if (empty($emailTemplateId)) { throw new InternalServerErrorException("No default email template for user invite."); } /** @var EmailService $emailService */ $emailService = ServiceHandler::getServiceById($emailServiceId); $emailTemplate = EmailTemplate::find($emailTemplateId); if (empty($emailTemplate)) { throw new InternalServerErrorException("No data found in default email template for user invite."); } try { $email = $user->email; $code = \Hash::make($email); $user->confirm_code = base64_encode($code); $user->save(); $templateData = $emailTemplate->toArray(); $data = array_merge($templateData, ['to' => $email, 'confirm_code' => $user->confirm_code, 'link' => url(\Config::get('df.confirm_register_url')) . '?code=' . $user->confirm_code, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'name' => $user->name, 'email' => $user->email, 'phone' => $user->phone, 'content_header' => ArrayUtils::get($templateData, 'subject', 'Confirm your DreamFactory account.'), 'instance_name' => \Config::get('df.instance_name')]); } catch (\Exception $e) { throw new InternalServerErrorException("Error creating user confirmation.\n{$e->getMessage()}", $e->getCode()); } $emailService->sendEmail($data, $emailTemplate->body_text, $emailTemplate->body_html); } catch (\Exception $e) { if ($deleteOnError) { $user->delete(); } throw new InternalServerErrorException("Error processing user confirmation.\n{$e->getMessage()}", $e->getCode()); } }
/** * {@inheritdoc} */ protected static function sendPasswordResetEmail(User $user) { $email = $user->email; $userService = Service::getCachedByName('user'); $config = $userService['config']; if (empty($config)) { throw new InternalServerErrorException('Unable to load user service configuration.'); } $emailServiceId = $config['password_email_service_id']; if (!empty($emailServiceId)) { try { /** @var EmailService $emailService */ $emailService = ServiceHandler::getServiceById($emailServiceId); if (empty($emailService)) { throw new ServiceUnavailableException("Bad service identifier '{$emailServiceId}'."); } $data = []; $templateId = $config['password_email_template_id']; if (!empty($templateId)) { $data = $emailService::getTemplateDataById($templateId); } if (empty($data) || !is_array($data)) { throw new ServiceUnavailableException("No data found in default email template for password reset."); } $data['to'] = $email; $data['content_header'] = 'Password Reset'; $data['first_name'] = $user->first_name; $data['last_name'] = $user->last_name; $data['name'] = $user->name; $data['phone'] = $user->phone; $data['email'] = $user->email; $data['link'] = url(\Config::get('df.confirm_reset_url')) . '?code=' . $user->confirm_code; $data['confirm_code'] = $user->confirm_code; $emailService->sendEmail($data, ArrayUtils::get($data, 'body_text'), ArrayUtils::get($data, 'body_html')); return true; } catch (\Exception $ex) { throw new InternalServerErrorException("Error processing password reset.\n{$ex->getMessage()}"); } } return false; }
/** * Deletes hosted app files from storage. * * @param $id * @param $storageServiceId * @param $storageFolder */ protected static function deleteHostedAppStorage($id, $storageServiceId, $storageFolder) { $app = AppModel::whereId($id)->first(); if (empty($app) && !empty($storageServiceId) && !empty($storageFolder)) { /** @type BaseFileService $storageService */ $storageService = ServiceHandler::getServiceById($storageServiceId); if ($storageService->driver()->folderExists(null, $storageFolder)) { $storageService->driver()->deleteFolder(null, $storageFolder, true); } } }
/** * @param $id * * @return mixed */ public static function getServiceById($id) { return ServiceHandler::getServiceById($id); }
/** * Package app files for export. * * @param $app * * @return bool * @throws \DreamFactory\Core\Exceptions\ForbiddenException * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException * @throws \DreamFactory\Core\Exceptions\NotFoundException */ private function packageAppFiles($app) { $appName = $app->name; $zipFileName = $this->zipFilePath; $storageServiceId = $app->storage_service_id; $storageFolder = $app->storage_container; if (empty($storageServiceId)) { $storageServiceId = $this->getDefaultStorageServiceId(); } if (empty($storageServiceId)) { throw new InternalServerErrorException("Can not find storage service identifier."); } /** @type BaseFileService $storage */ $storage = ServiceHandler::getServiceById($storageServiceId); if (!$storage) { throw new InternalServerErrorException("Can not find storage service by identifier '{$storageServiceId}''."); } if (empty($storageFolder)) { if ($storage->driver()->containerExists($appName)) { $storage->driver()->getFolderAsZip($appName, '', $this->zip, $zipFileName, true); } } else { if ($storage->driver()->folderExists($storageFolder, $appName)) { $storage->driver()->getFolderAsZip($storageFolder, $appName, $this->zip, $zipFileName, true); } } return true; }
/** * @param $userId * @param bool|false $deleteOnError * * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException * @throws \DreamFactory\Core\Exceptions\NotFoundException * @throws \Exception */ protected static function sendInvite($userId, $deleteOnError = false) { /** @type BaseSystemModel $user */ $user = \DreamFactory\Core\Models\User::find($userId); if (empty($user)) { throw new NotFoundException('User not found with id ' . $userId . '.'); } if ('y' === strtolower($user->confirm_code)) { throw new BadRequestException('User with this identifier has already confirmed this account.'); } try { $userService = Service::getCachedByName('user'); $config = $userService['config']; if (empty($config)) { throw new InternalServerErrorException('Unable to load system configuration.'); } $emailServiceId = $config['invite_email_service_id']; $emailTemplateId = $config['invite_email_template_id']; if (empty($emailServiceId)) { throw new InternalServerErrorException('No email service configured for user invite.'); } if (empty($emailTemplateId)) { throw new InternalServerErrorException("No default email template for user invite."); } /** @var EmailService $emailService */ $emailService = ServiceHandler::getServiceById($emailServiceId); $emailTemplate = EmailTemplate::find($emailTemplateId); if (empty($emailTemplate)) { throw new InternalServerErrorException("No data found in default email template for user invite."); } try { $email = $user->email; $code = \Hash::make($email); $user->confirm_code = base64_encode($code); $user->save(); $templateData = $emailTemplate->toArray(); $data = array_merge($templateData, ['to' => $email, 'confirm_code' => $user->confirm_code, 'link' => url(\Config::get('df.confirm_invite_url')) . '?code=' . $user->confirm_code, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'name' => $user->name, 'email' => $user->email, 'phone' => $user->phone, 'content_header' => ArrayUtils::get($templateData, 'subject', 'You are invited to try DreamFactory.'), 'instance_name' => \Config::get('df.instance_name')]); } catch (\Exception $e) { throw new InternalServerErrorException("Error creating user invite. {$e->getMessage()}", $e->getCode()); } $emailService->sendEmail($data, $emailTemplate->body_text, $emailTemplate->body_html); } catch (\Exception $e) { if ($deleteOnError) { $user->delete(); } throw new InternalServerErrorException("Error processing user invite. {$e->getMessage()}", $e->getCode()); } }