Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Develop #118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
use DreamFactory\Core\Utility\DataFormatter;
use DreamFactory\Core\Utility\JWTUtilities;
use DreamFactory\Core\Utility\Session;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Validator;

use Illuminate\Support\Facades\Http;
use Carbon\Carbon;
/**
* User
*
Expand Down Expand Up @@ -628,6 +632,21 @@ public static function createFirstAdmin(array &$data)

return false;
} else {
// Send request to create the customer in updates.dreamfactory.com and get the customer key
$res = self::createCustomer($data);

// Extract the key from the response and save it in the config file and .env file
$customerKey = $res['data'];
$path = base_path('.env');
$app = app();
$key = $app['config']['app.license_key'];
if (file_exists($path)) {
file_put_contents($path, str_replace(
'DF_LICENSE_KEY='.$key, 'DF_LICENSE_KEY='.$customerKey, file_get_contents($path)
));
}
$app['config']['app.license_key'] = $customerKey;

/** @type User $user */
$attributes = array_only($data, ['name', 'first_name', 'last_name', 'email', 'username', 'phone']);
$attributes['is_active'] = 1;
Expand All @@ -648,6 +667,36 @@ public static function createFirstAdmin(array &$data)
}
}

/**
* @param array $data
* @return array
* @throws \Exception
*/
public static function createCustomer(array $data)
{
try {
$token = hash_hmac('sha256', $data['email'], config('services.marketplace.secret'));
$response = Http::withHeaders([
'X-Marketplace-Token' => $token,
])->post(config('services.df_updates.endpoint'), [
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
]);

if ($response->status() !== 200 || $response->status() !== 201) {
Log::error("Error creating customer in DF updates: " . $response->body());
throw new \Exception();
}

return $response->json();
} catch (\Throwable $exception) {
Log::error($exception->getMessage());
Log::error($exception->getTraceAsString());
throw new \Exception('Error creating customer in DF updates');
}
}

public function save(array $options = [])
{
if ($this->exists) {
Expand Down