/**
  * Create a new user instance after a valid registration.
  *
  * @param  array $data
  *
  * @return User
  */
 public function create(array $data)
 {
     return \DB::transaction(function () use($data) {
         $_user = User::create(['first_name_text' => $data['first_name_text'], 'last_name_text' => $data['last_name_text'], 'email_addr_text' => $data['email_addr_text'], 'nickname_text' => $data['nickname_text'], 'password_text' => bcrypt($data['password_text'])]);
         $_appKey = AppKey::create(array('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;
     });
 }
 /**
  * @param ProvisionServiceRequest $request
  *
  * @return array
  * @throws ProvisioningException
  */
 protected function provisionInstance($request)
 {
     $_storagePath = null;
     //	Pull the request apart
     $_instance = $request->getInstance();
     $_name = $this->sanitizeInstanceName($_instance->instance_name_text);
     $this->info('[provisioning] instance "' . $_name . '" begin');
     $_storageProvisioner = $request->getStorageProvisioner();
     $this->setPrivatePath($_privatePath = $_storageProvisioner->getPrivatePath());
     $this->setOwnerPrivatePath($_ownerPrivatePath = $_storageProvisioner->getOwnerPrivatePath());
     //	1. Provision the database
     if (false === ($_dbConfig = Provision::getDatabaseProvisioner($_instance->guest_location_nbr)->provision($request))) {
         throw new ProvisioningException('[provisioning] error during database provisioning.');
     }
     //  2. Generate an app key for the instance
     AppKey::create(['key_class_text' => AppKeyClasses::INSTANCE, 'owner_id' => $_instance->id, 'owner_type_nbr' => OwnerTypes::INSTANCE, 'server_secret' => config('dfe.security.console-api-key')]);
     //  3. Update the instance with new provision info
     try {
         $_instance->fill(['guest_location_nbr' => GuestLocations::DFE_CLUSTER, 'instance_id_text' => $_name, 'instance_name_text' => $_name, 'db_host_text' => $_dbConfig['host'], 'db_port_nbr' => $_dbConfig['port'], 'db_name_text' => $_dbConfig['database'], 'db_user_text' => $_dbConfig['username'], 'db_password_text' => $_dbConfig['password'], 'ready_state_nbr' => InstanceStates::ADMIN_REQUIRED, 'state_nbr' => ProvisionStates::PROVISIONED, 'platform_state_nbr' => OperationalStates::NOT_ACTIVATED, 'start_date' => $_instance->freshTimestamp(), 'end_date' => null, 'terminate_date' => null, 'provision_ind' => true, 'deprovision_ind' => false]);
         //  Create the guest row...
         $_host = $this->getFullyQualifiedDomainName($_name);
         /** @noinspection PhpUndefinedMethodInspection */
         DB::transaction(function () use($_instance, $_host) {
             /** Add guest data if there is a guest record */
             $_instance->guest && $_instance->guest->fill(['base_image_text' => config('provisioning.base-image', ConsoleDefaults::DFE_CLUSTER_BASE_IMAGE), 'vendor_state_nbr' => ProvisionStates::PROVISIONED, 'vendor_state_text' => 'running', 'public_host_text' => $_host])->save();
             //  Save the instance
             $_instance->save();
         });
     } catch (\Exception $_ex) {
         throw new \RuntimeException('[provisioning:instance] error updating instance data: ' . $_ex->getMessage());
     }
     //  Fire off a "provisioned" event...
     \Event::fire('dfe.provisioned', [$this, $request, $_instance->getMetadata()]);
     $this->info('[provisioning:instance] instance "' . $_name . '" provisioned');
     return $_instance->getMetadata();
 }
示例#3
0
 /**
  * @param Instance $instance
  * @param bool     $object If true, the Metadata object is returned instead of the array
  *
  * @return array|\DreamFactory\Enterprise\Common\Support\Metadata
  */
 public static function makeMetadata(Instance $instance, $object = false)
 {
     if (null === ($_key = AppKey::mine($instance->id, OwnerTypes::INSTANCE))) {
         //  Create an instance key
         $_key = AppKey::create(['key_class_text' => AppKeyClasses::INSTANCE, 'owner_id' => $instance->id, 'owner_type_nbr' => OwnerTypes::INSTANCE, 'server_secret' => config('dfe.security.console-api-key')]);
         if (null === $_key) {
             throw new \RuntimeException('Instance is unlicensed.');
         }
     }
     $_cluster = static::_lookupCluster($instance->cluster_id);
     $_md = new Metadata(array_merge(static::$metadataTemplate, ['storage-map' => InstanceStorage::buildStorageMap($instance->user->storage_id_text), 'env' => static::buildEnvironmentMetadata($instance, $_cluster, $_key), 'db' => static::buildDatabaseMetadata($instance), 'paths' => static::buildPathMetadata($instance), 'audit' => static::buildAuditMetadata($instance), 'limits' => static::buildLimitsMetadata($instance)]), $instance->instance_name_text . '.json', $instance->getOwnerPrivateStorageMount());
     return $object ? $_md : $_md->toArray();
 }
示例#4
0
 /**
  * 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;
     }
 }