Ejemplo n.º 1
0
 /**
  * @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());
     }
 }
Ejemplo n.º 2
0
 /**
  * @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());
     }
 }