2019-11-20 06:41:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-20 06:41:49 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-04-15 00:37:27 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyToken;
|
2020-03-12 21:38:22 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2020-01-23 21:35:00 +01:00
|
|
|
use App\Jobs\Util\StartMigration;
|
2020-04-14 00:20:54 +02:00
|
|
|
use App\Mail\ExistingMigration;
|
2020-04-15 00:37:27 +02:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-20 06:41:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-03-03 23:44:42 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-04-15 00:37:27 +02:00
|
|
|
use App\Console\Commands\ImportMigrations;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2019-11-20 06:41:49 +01:00
|
|
|
|
|
|
|
class MigrationController extends BaseController
|
|
|
|
{
|
|
|
|
use DispatchesJobs;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Purge Company
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/purge/{company}",
|
|
|
|
* operationId="postPurgeCompany",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Attempts to purge a company record and all its child records",
|
|
|
|
* description="Attempts to purge a company record and all its child records",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="company",
|
|
|
|
* in="path",
|
|
|
|
* description="The Company Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-20 06:41:49 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function purgeCompany(Company $company)
|
|
|
|
{
|
2020-05-06 13:49:42 +02:00
|
|
|
|
2020-08-13 23:15:15 +02:00
|
|
|
$account = $company->account;
|
|
|
|
$company_id = $company->id;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$company->delete();
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2020-08-13 23:15:15 +02:00
|
|
|
/*Update the new default company if necessary*/
|
|
|
|
if($company_id == $account->default_company_id && $account->companies->count() >= 1)
|
|
|
|
{
|
|
|
|
|
|
|
|
$new_default_company = $account->companies->first();
|
|
|
|
|
|
|
|
if($new_default_company){
|
|
|
|
$account->default_company_id = $new_default_company->id;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
return response()->json(['message' => 'Company purged'], 200);
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Purge Company but save settings
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/purge_save_settings/{company}",
|
|
|
|
* operationId="postPurgeCompanySaveSettings",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Attempts to purge a companies child records but save the company record and its settings",
|
|
|
|
* description="Attempts to purge a companies child records but save the company record and its settings",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="company",
|
|
|
|
* in="path",
|
|
|
|
* description="The Company Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-20 06:41:49 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-20 06:41:49 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-08-14 02:40:59 +02:00
|
|
|
public function purgeCompanySaveSettings(Request $request, Company $company)
|
2019-11-20 06:41:49 +01:00
|
|
|
{
|
2020-08-14 02:40:59 +02:00
|
|
|
|
2020-08-24 00:20:57 +02:00
|
|
|
$company->clients()->forceDelete();
|
|
|
|
$company->products()->forceDelete();
|
2020-08-19 04:44:25 +02:00
|
|
|
|
2019-11-20 21:55:16 +01:00
|
|
|
$company->save();
|
2019-11-20 06:41:49 +01:00
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
return response()->json(['message' => 'Settings preserved'], 200);
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|
2020-01-23 21:35:00 +01:00
|
|
|
|
2020-01-27 21:56:48 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Start the migration from V1
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/migration/start",
|
|
|
|
* operationId="postStartMigration",
|
|
|
|
* tags={"migration"},
|
|
|
|
* summary="Starts the migration from previous version of Invoice Ninja",
|
|
|
|
* description="Starts the migration from previous version of Invoice Ninja",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
2020-03-03 23:44:42 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Password"),
|
2020-01-27 21:56:48 +01:00
|
|
|
* @OA\Parameter(
|
|
|
|
* name="migration",
|
2020-07-30 05:27:00 +02:00
|
|
|
* in="query",
|
2020-01-27 21:56:48 +01:00
|
|
|
* description="The migraton file",
|
|
|
|
* example="migration.zip",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
2020-02-12 10:18:56 +01:00
|
|
|
* type="object",
|
2020-01-27 21:56:48 +01:00
|
|
|
* format="file",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-27 21:56:48 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-02-24 22:15:07 +01:00
|
|
|
public function startMigration(Request $request, Company $company)
|
2020-01-23 21:35:00 +01:00
|
|
|
{
|
2020-03-12 21:38:22 +01:00
|
|
|
$user = auth()->user();
|
2020-04-19 12:29:58 +02:00
|
|
|
|
2020-04-29 15:24:52 +02:00
|
|
|
if (app()->environment() === 'local') {
|
|
|
|
info([
|
|
|
|
'Company key' => $company->company_key,
|
|
|
|
'Request key' => $request->company_key,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:20:54 +02:00
|
|
|
$existing_company = Company::where('company_key', $request->company_key)->first();
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
$checks = [
|
|
|
|
'same_keys' => $request->company_key == $company->company_key,
|
|
|
|
'existing_company' => (bool) $existing_company,
|
|
|
|
'with_force' => (bool) ($request->has('force') && !empty($request->force)),
|
|
|
|
];
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
// If same company keys, and force provided.
|
|
|
|
if ($checks['same_keys'] && $checks['with_force']) {
|
|
|
|
info('Migrating: Same company keys, with force.');
|
2020-04-15 00:37:27 +02:00
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
if($company)
|
|
|
|
$this->purgeCompany($company);
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
$account = auth()->user()->account;
|
2020-04-14 00:20:54 +02:00
|
|
|
$company = (new ImportMigrations())->getCompany($account);
|
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
2020-04-15 00:37:27 +02:00
|
|
|
$company_token = new CompanyToken();
|
|
|
|
$company_token->user_id = $user->id;
|
|
|
|
$company_token->company_id = $company->id;
|
|
|
|
$company_token->account_id = $account->id;
|
|
|
|
$company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
|
2020-07-21 07:03:04 +02:00
|
|
|
$company_token->is_system = true;
|
2020-04-15 00:37:27 +02:00
|
|
|
$company_token->save();
|
2020-04-14 00:20:54 +02:00
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
// If keys are same and no force has been provided.
|
|
|
|
if ($checks['same_keys'] && !$checks['with_force']) {
|
|
|
|
info('Migrating: Same company keys, no force provided.');
|
|
|
|
|
|
|
|
Mail::to($user)->send(new ExistingMigration());
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'_id' => Str::uuid(),
|
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If keys ain't same, but existing company without force.
|
|
|
|
if (!$checks['same_keys'] && $checks['existing_company'] && !$checks['with_force']) {
|
|
|
|
info('Migrating: Different keys, existing company with the key without the force option.');
|
|
|
|
|
|
|
|
Mail::to($user)->send(new ExistingMigration());
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'_id' => Str::uuid(),
|
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If keys ain't same, but existing company with force.
|
|
|
|
if (!$checks['same_keys'] && $checks['existing_company'] && $checks['with_force']) {
|
|
|
|
info('Migrating: Different keys, exisiting company with force option.');
|
2020-03-12 21:38:22 +01:00
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
if($company)
|
|
|
|
$this->purgeCompany($company);
|
2020-01-27 21:56:48 +01:00
|
|
|
|
2020-04-29 15:24:52 +02:00
|
|
|
$account = auth()->user()->account;
|
2020-03-12 21:38:22 +01:00
|
|
|
$company = (new ImportMigrations())->getCompany($account);
|
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
|
2020-04-15 00:37:27 +02:00
|
|
|
$company_token = new CompanyToken();
|
|
|
|
$company_token->user_id = $user->id;
|
|
|
|
$company_token->company_id = $company->id;
|
|
|
|
$company_token->account_id = $account->id;
|
|
|
|
$company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
|
2020-07-21 07:03:04 +02:00
|
|
|
$company_token->is_system = true;
|
|
|
|
|
2020-04-15 00:37:27 +02:00
|
|
|
$company_token->save();
|
2020-03-12 21:38:22 +01:00
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
// If keys ain't same, but with force.
|
|
|
|
if (!$checks['same_keys'] && $checks['with_force']) {
|
|
|
|
info('Migrating: Different keys with force.');
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
if($existing_company)
|
|
|
|
$this->purgeCompany($existing_company);
|
2020-04-14 00:20:54 +02:00
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
$account = auth()->user()->account;
|
|
|
|
$company = (new ImportMigrations())->getCompany($account);
|
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
$company_token = new CompanyToken();
|
|
|
|
$company_token->user_id = $user->id;
|
|
|
|
$company_token->company_id = $company->id;
|
|
|
|
$company_token->account_id = $account->id;
|
|
|
|
$company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
|
2020-07-21 07:03:04 +02:00
|
|
|
$company_token->is_system = true;
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
$company_token->save();
|
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If keys ain't same, fresh migrate.
|
|
|
|
if (!$checks['same_keys'] && !$checks['with_force']) {
|
|
|
|
info('Migrating: Vanilla, fresh migrate.');
|
|
|
|
|
|
|
|
$account = auth()->user()->account;
|
|
|
|
$company = (new ImportMigrations())->getCompany($account);
|
|
|
|
|
|
|
|
$company_token = new CompanyToken();
|
|
|
|
$company_token->user_id = $user->id;
|
|
|
|
$company_token->company_id = $company->id;
|
|
|
|
$company_token->account_id = $account->id;
|
|
|
|
$company_token->name = $request->token_name ?? Str::random(12);
|
|
|
|
$company_token->token = $request->token ?? \Illuminate\Support\Str::random(64);
|
2020-07-21 07:03:04 +02:00
|
|
|
$company_token->is_system = true;
|
|
|
|
|
2020-04-30 00:01:44 +02:00
|
|
|
$company_token->save();
|
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
|
|
|
'permissions' => '',
|
|
|
|
'settings' => null,
|
|
|
|
]);
|
2020-04-14 00:20:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
$migration_file = $request->file('migration')
|
|
|
|
->storeAs('migrations', $request->file('migration')->getClientOriginalName());
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (app()->environment() == 'testing') {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 10:54:22 +02:00
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
StartMigration::dispatch(base_path("storage/app/public/$migration_file"), $user, $company)->delay(now()->addSeconds(60));
|
2020-01-23 21:35:00 +01:00
|
|
|
|
2020-03-03 23:44:42 +01:00
|
|
|
return response()->json([
|
2020-03-05 21:30:32 +01:00
|
|
|
'_id' => Str::uuid(),
|
2020-03-03 23:44:42 +01:00
|
|
|
'method' => config('queue.default'),
|
|
|
|
'started_at' => now(),
|
|
|
|
], 200);
|
2020-01-23 21:35:00 +01:00
|
|
|
}
|
2019-11-20 06:41:49 +01:00
|
|
|
}
|