/** * @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()); } }
public function testPasswordResetUsingConfirmationCode() { if (!$this->serviceExists('mymail')) { $emailService = \DreamFactory\Core\Models\Service::create(["name" => "mymail", "label" => "Test mail service", "description" => "Test mail service", "is_active" => true, "type" => "local_email", "mutable" => true, "deletable" => true, "config" => ["driver" => "sendmail", "command" => "/usr/sbin/sendmail -bs"]]); $userConfig = \DreamFactory\Core\User\Models\UserConfig::find(4); $userConfig->password_email_service_id = $emailService->id; $userConfig->save(); } if (!\DreamFactory\Core\Models\EmailTemplate::whereName('mytemplate')->exists()) { $template = \DreamFactory\Core\Models\EmailTemplate::create(['name' => 'mytemplate', 'description' => 'test', 'to' => $this->user2['email'], 'subject' => 'rest password test', 'body_text' => 'link {link}']); $userConfig = \DreamFactory\Core\User\Models\UserConfig::find(4); $userConfig->password_email_template_id = $template->id; $userConfig->save(); } Arr::set($this->user2, 'email', '*****@*****.**'); $user = $this->createUser(2); Config::set('mail.pretend', true); $rs = $this->makeRequest(Verbs::POST, static::RESOURCE, ['reset' => 'true'], ['email' => $user['email']]); $content = $rs->getContent(); $this->assertTrue($content['success']); /** @var User $userModel */ $userModel = User::find($user['id']); $code = $userModel->confirm_code; $rs = $this->makeRequest(Verbs::POST, static::RESOURCE, ['login' => 'true'], ['email' => $user['email'], 'code' => $code, 'new_password' => '778877']); $content = $rs->getContent(); $this->assertTrue($content['success']); $this->assertTrue(Session::isAuthenticated()); $userModel = User::find($user['id']); $this->assertEquals('y', $userModel->confirm_code); $this->service = ServiceHandler::getService($this->serviceId); $rs = $this->makeRequest(Verbs::POST, 'session', [], ['email' => $user['email'], 'password' => '778877']); $content = $rs->getContent(); $this->assertTrue(!empty($content['session_id'])); }
protected function getRecordExtras() { $emailService = Service::whereName('email')->first(); $emailTemplateInvite = EmailTemplate::whereName('User Invite Default')->first(); $emailTemplatePassword = EmailTemplate::whereName('Password Reset Default')->first(); $emailTemplateOpenReg = EmailTemplate::whereName('User Registration Default')->first(); return ['config' => ['allow_open_registration' => false, 'open_reg_email_service_id' => !empty($emailService) ? $emailService->id : null, 'open_reg_email_template_id' => !empty($emailTemplateOpenReg) ? $emailTemplateOpenReg->id : null, 'invite_email_service_id' => !empty($emailService) ? $emailService->id : null, 'invite_email_template_id' => !empty($emailTemplateInvite) ? $emailTemplateInvite->id : null, 'password_email_service_id' => !empty($emailService) ? $emailService->id : null, 'password_email_template_id' => !empty($emailTemplatePassword) ? $emailTemplatePassword->id : null]]; }
/** * @param array $schema */ protected static function prepareConfigSchemaField(array &$schema) { parent::prepareConfigSchemaField($schema); $roleList = [['label' => '', 'name' => null]]; $emailSvcList = [['label' => '', 'name' => null]]; $templateList = [['label' => '', 'name' => null]]; switch ($schema['name']) { case 'open_reg_role_id': $roles = Role::whereIsActive(1)->get(); foreach ($roles as $role) { $roleList[] = ['label' => $role->name, 'name' => $role->id]; } $schema['type'] = 'picklist'; $schema['values'] = $roleList; $schema['label'] = 'Open Reg Role'; $schema['description'] = 'Select a role for self registered users.'; break; case 'open_reg_email_service_id': case 'invite_email_service_id': case 'password_email_service_id': $label = substr($schema['label'], 0, strlen($schema['label']) - 11); $services = Service::whereIsActive(1)->whereIn('type', ['aws_ses', 'smtp_email', 'mailgun_email', 'mandrill_email', 'local_email'])->get(); foreach ($services as $service) { $emailSvcList[] = ['label' => $service->label, 'name' => $service->id]; } $schema['type'] = 'picklist'; $schema['values'] = $emailSvcList; $schema['label'] = $label . ' Service'; $schema['description'] = 'Select an Email service for sending out ' . $label . '.'; break; case 'open_reg_email_template_id': case 'invite_email_template_id': case 'password_email_template_id': $label = substr($schema['label'], 0, strlen($schema['label']) - 11); $templates = EmailTemplate::get(); foreach ($templates as $template) { $templateList[] = ['label' => $template->name, 'name' => $template->id]; } $schema['type'] = 'picklist'; $schema['values'] = $templateList; $schema['label'] = $label . ' Template'; $schema['description'] = 'Select an Email template to use for ' . $label . '.'; break; } }
/** * @param $id * * @throws NotFoundException * * @return array */ public static function getTemplateDataById($id) { // find template in system db $template = EmailTemplate::whereId($id)->first(); if (empty($template)) { throw new NotFoundException("Email Template id '{$id}' not found"); } return $template->toArray(); }
/** * @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()); } }