Beispiel #1
0
 /**
  * @param $name
  *
  * @return BaseRestService
  * @throws ForbiddenException
  * @throws NotFoundException
  */
 public static function getService($name)
 {
     $name = strtolower(trim($name));
     $serviceInfo = Service::getCachedByName($name);
     $serviceClass = ArrayUtils::get($serviceInfo, 'class_name');
     return new $serviceClass($serviceInfo);
 }
Beispiel #2
0
 /**
  * {@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;
 }
Beispiel #3
0
 /**
  * Creates a non-admin user.
  *
  * @param array $data
  *
  * @return \DreamFactory\Core\Models\User
  * @throws \DreamFactory\Core\Exceptions\ForbiddenException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  * @throws \Exception
  */
 public function create(array $data)
 {
     $userService = Service::getCachedByName('user');
     if (!$userService['config']['allow_open_registration']) {
         throw new ForbiddenException('Open Registration is not enabled.');
     }
     $openRegEmailSvcId = $userService['config']['open_reg_email_service_id'];
     $openRegEmailTplId = $userService['config']['open_reg_email_template_id'];
     $openRegRoleId = $userService['config']['open_reg_role_id'];
     /** @type User $user */
     $user = User::create($data);
     if (!empty($openRegEmailSvcId)) {
         $this->sendConfirmation($user, $openRegEmailSvcId, $openRegEmailTplId);
     } else {
         if (!empty($data['password'])) {
             $user->password = $data['password'];
             $user->save();
         }
     }
     if (!empty($openRegRoleId)) {
         User::applyDefaultUserAppRole($user, $openRegRoleId);
     }
     return $user;
 }
Beispiel #4
0
 /**
  * @return array
  */
 protected static function getLoginApi()
 {
     $adminApi = ['path' => 'system/admin/session', 'verb' => Verbs::POST, 'payload' => ['email' => 'string', 'password' => 'string', 'remember_me' => 'bool']];
     $userApi = ['path' => 'user/session', 'verb' => Verbs::POST, 'payload' => ['email' => 'string', 'password' => 'string', 'remember_me' => 'bool']];
     if (class_exists(User::class)) {
         $oauth = static::getOAuthServices();
         $ldap = static::getAdLdapServices();
         $userService = ServiceModel::getCachedByName('user');
         $allowOpenRegistration = $userService['config']['allow_open_registration'];
         $openRegEmailServiceId = $userService['config']['open_reg_email_service_id'];
         return ['admin' => $adminApi, 'user' => $userApi, 'oauth' => $oauth, 'adldap' => $ldap, 'allow_open_registration' => $allowOpenRegistration, 'open_reg_email_service_id' => $openRegEmailServiceId, 'allow_forever_sessions' => config('df.allow_forever_sessions', false)];
     }
     return ['admin' => $adminApi, 'allow_open_registration' => false, 'open_reg_email_service_id' => false];
 }
 protected static function setExceptions()
 {
     if (class_exists(\DreamFactory\Core\User\Services\User::class)) {
         $userService = Service::getCachedByName('user');
         if ($userService['config']['allow_open_registration']) {
             static::$exceptions[] = ['verb_mask' => 2, 'service' => 'user', 'resource' => 'register'];
         }
     }
 }
Beispiel #6
0
 /**
  * @param array $extras
  *
  * @return void
  */
 public function setSchemaFieldExtras($extras)
 {
     if (empty($extras)) {
         return;
     }
     foreach ($extras as $extra) {
         if (!empty($table = ArrayUtils::get($extra, 'table')) && !empty($field = ArrayUtils::get($extra, 'field'))) {
             if (!empty($extra['ref_table']) && empty($extra['ref_service_id'])) {
                 if (!empty($extra['ref_service'])) {
                     // translate name to id for storage
                     $extra['ref_service_id'] = Service::getCachedByName($extra['ref_service'], 'id', $this->getServiceId());
                 } else {
                     // don't allow empty ref_service_id into the database, needs to be searchable from other services
                     $extras['ref_service_id'] = $this->getServiceId();
                 }
             }
             DbFieldExtras::updateOrCreate(['service_id' => $this->getServiceId(), 'table' => $table, 'field' => $field], array_only($extra, ['alias', 'label', 'extra_type', 'description', 'picklist', 'validation', 'client_info', 'db_function', 'ref_service_id', 'ref_table', 'ref_fields', 'ref_on_update', 'ref_on_delete']));
         }
     }
 }
Beispiel #7
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());
     }
 }