diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 167e22548423..159b8433dcab 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -63,8 +63,17 @@ class CreateTestData extends Command $this->warmCache(); + $this->createSmallAccount(); + $this->createMediumAccount(); + $this->createLargeAccount(); - $this->info('Creating Account and Company'); + } + + + private function createSmallAccount() + { + + $this->info('Creating Small Account and Company'); $account = factory(\App\Models\Account::class)->create(); $company = factory(\App\Models\Company::class)->create([ @@ -75,13 +84,13 @@ class CreateTestData extends Command $account->default_company_id = $company->id; $account->save(); - $user = User::whereEmail('user@example.com')->first(); + $user = User::whereEmail('small@example.com')->first(); if(!$user) { $user = factory(\App\Models\User::class)->create([ // 'account_id' => $account->id, - 'email' => 'user@example.com', + 'email' => 'small@example.com', 'confirmation_code' => $this->createDbHash(config('database.default')) ]); } @@ -101,7 +110,7 @@ class CreateTestData extends Command 'is_owner' => 1, 'is_admin' => 1, 'is_locked' => 0, - 'permissions' => json_encode([]), + 'permissions' => '', 'settings' => json_encode(DefaultSettings::userSettings()), ]); @@ -115,6 +124,121 @@ class CreateTestData extends Command $this->createClient($company, $user); } + + } + + private function createMediumAccount() + { + $this->info('Creating Medium Account and Company'); + + $account = factory(\App\Models\Account::class)->create(); + $company = factory(\App\Models\Company::class)->create([ + 'account_id' => $account->id, + 'domain' => 'ninja.test:8000', + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::whereEmail('medium@example.com')->first(); + + if(!$user) + { + $user = factory(\App\Models\User::class)->create([ + // 'account_id' => $account->id, + 'email' => 'medium@example.com', + 'confirmation_code' => $this->createDbHash(config('database.default')) + ]); + } + + $token = \Illuminate\Support\Str::random(64); + + $company_token = CompanyToken::create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'account_id' => $account->id, + 'name' => 'test token', + 'token' => $token, + ]); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'is_locked' => 0, + 'permissions' => '', + 'settings' => json_encode(DefaultSettings::userSettings()), + ]); + + $this->count = $this->count*10; + + $this->info('Creating '.$this->count. ' clients'); + + + for($x=0; $x<$this->count; $x++) { + $z = $x+1; + $this->info("Creating client # ".$z); + + $this->createClient($company, $user); + } + + } + + private function createLargeAccount() + { + $this->info('Creating Large Account and Company'); + + $account = factory(\App\Models\Account::class)->create(); + $company = factory(\App\Models\Company::class)->create([ + 'account_id' => $account->id, + 'domain' => 'ninja.test:8000', + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::whereEmail('large@example.com')->first(); + + if(!$user) + { + $user = factory(\App\Models\User::class)->create([ + // 'account_id' => $account->id, + 'email' => 'large@example.com', + 'confirmation_code' => $this->createDbHash(config('database.default')) + ]); + } + + $token = \Illuminate\Support\Str::random(64); + + $company_token = CompanyToken::create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'account_id' => $account->id, + 'name' => 'test token', + 'token' => $token, + ]); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'is_locked' => 0, + 'permissions' => '', + 'settings' => json_encode(DefaultSettings::userSettings()), + ]); + + $this->count = $this->count*100; + + $this->info('Creating '.$this->count. ' clients'); + + + for($x=0; $x<$this->count; $x++) { + $z = $x+1; + $this->info("Creating client # ".$z); + + $this->createClient($company, $user); + } + } private function createClient($company, $user) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 0e0f89cc1f01..bd47e4e7e814 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -71,6 +71,17 @@ class AccountController extends BaseController * description="Attempts a new account signup and returns a CompanyUser object on success", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="token_name", + * in="path", + * description="A custom name for the user company token", + * example="Daves iOS Device", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), * @OA\RequestBody( * description="Signup credentials", * required=true, diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 50a50ef05af5..fdec90c3c914 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -228,7 +228,9 @@ class CompanyController extends BaseController /* * Create token */ - $company_token = CreateCompanyToken::dispatchNow($company, auth()->user(), request()->server('HTTP_USER_AGENT')); + $user_agent = request()->input('token_name') ?: request()->server('HTTP_USER_AGENT'); + + $company_token = CreateCompanyToken::dispatchNow($company, auth()->user(), $user_agent); $this->entity_transformer = CompanyUserTransformer::class; $this->entity_type = CompanyUser::class; diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index b27639cba6c5..bcaa70a3da74 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -136,7 +136,7 @@ class MigrationController extends BaseController $company->client->delete(); $company->save(); - return response()->json(['message'=>'Setting preserved'], 200); + return response()->json(['message'=>'Settings preserved'], 200); } diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index ce3787b76210..de73e58928ef 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -81,7 +81,9 @@ class CreateAccount /* * Create token */ - $company_token = CreateCompanyToken::dispatchNow($company, $user, $this->request['user_agent']); + $user_agent = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT'); + + $company_token = CreateCompanyToken::dispatchNow($company, $user, $user_agent); /* * Fire related events diff --git a/app/Jobs/Company/CreateCompanyToken.php b/app/Jobs/Company/CreateCompanyToken.php index 236c6810323d..b9d8ce2d73cf 100644 --- a/app/Jobs/Company/CreateCompanyToken.php +++ b/app/Jobs/Company/CreateCompanyToken.php @@ -29,19 +29,19 @@ class CreateCompanyToken implements ShouldQueue protected $user; - protected $user_agent; + protected $custom_token_name; /** * Create a new job instance. * * @return void */ - public function __construct(Company $company, User $user, string $user_agent) + public function __construct(Company $company, User $user, string $custom_token_name) { $this->company = $company; $this->user = $user; - $this->user_agent = $user_agent; + $this->custom_token_name = $custom_token_name; } /** @@ -51,14 +51,14 @@ class CreateCompanyToken implements ShouldQueue */ public function handle() : ?CompanyToken { + $this->custom_token_name = $this->custom_token_name ?: $this->user->first_name. ' '. $this->user->last_name; $ct = CompanyToken::create([ 'user_id' => $this->user->id, 'account_id' => $this->company->account->id, 'token' => Str::random(64), - 'name' => $this->user->first_name. ' '. $this->user->last_name, + 'name' => $this->custom_token_name ?: $this->user->first_name. ' '. $this->user->last_name, 'company_id' => $this->company->id, - 'user_agent' => $this->user_agent, ]); return $ct; diff --git a/app/Transformers/CompanyTokenTransformer.php b/app/Transformers/CompanyTokenTransformer.php index 93af56c99159..e68494b9a0a2 100644 --- a/app/Transformers/CompanyTokenTransformer.php +++ b/app/Transformers/CompanyTokenTransformer.php @@ -44,7 +44,6 @@ class CompanyTokenTransformer extends EntityTransformer return [ 'token' => $company_token->token, 'name' => $company_token->name ?: '', - 'user_agent' => $company_token->user_agent ?: 'Unidentified', ]; } diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 05ac7746fa3a..aa5a588e1a14 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -273,7 +273,6 @@ class CreateUsersTable extends Migration $table->unsignedInteger('user_id'); $table->string('token')->nullable(); $table->string('name')->nullable(); - $table->string('user_agent')->nullable(); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');