Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 3c673af

Browse files
committed
- Generate personal access token from the console
- Validate user creation on installation service - Bcrypt user password on creation from the installation service.
1 parent 449e643 commit 3c673af

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

app/Console/Kernel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class Kernel extends ConsoleKernel
2525
*/
2626
protected function schedule(Schedule $schedule)
2727
{
28-
// $schedule->command('inspire')
29-
// ->hourly();
28+
3029
}
3130

3231
/**

app/Services/Installation/InstallAppHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Entities\User;
77
use App\Entities\Permission;
88
use App\Services\Installation\Events\ApplicationWasInstalled;
9+
use Illuminate\Validation\ValidationException;
910

1011
/**
1112
* Class InstallAppHandler
@@ -98,14 +99,22 @@ public function createPermissions()
9899
/**
99100
* @param array $attributes
100101
* @return $this
102+
* @throws ValidationException
101103
*/
102104
public function createAdminUser(array $attributes = [])
103105
{
106+
$validator = validator($attributes, [
107+
'name' => 'required',
108+
'email' => 'required|email|unique:users,email',
109+
'password' => 'required|min:8|confirmed'
110+
]);
111+
if($validator->fails()) {
112+
throw new ValidationException($validator);
113+
}
104114
$this->adminUser = User::create([
105115
'name' => $attributes['name'],
106116
'email' => $attributes['email'],
107-
'password' => $attributes['password'],
108-
'password_confirmation' => $attributes['password_confirmation']
117+
'password' => bcrypt($attributes['password'])
109118
]);
110119
return $this;
111120
}

routes/console.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
|
1414
*/
1515

16-
Artisan::command('inspire', function () {
17-
$this->comment(Inspiring::quote());
18-
})->describe('Display an inspiring quote');
16+
Artisan::command('dev:generate-personal-token {userId}', function ($userId) {
17+
$user = \App\Entities\User::find($userId);
18+
$this->info('Token for user '.$user->name);
19+
$token = $user->createToken('Personal Access Token')->accessToken;
20+
$this->info($token);
21+
})->describe('Generates a personal access token for a user');

tests/Unit/Services/Installation/InstallAppHandlerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Unit\Services\Installation;
44

5+
use Illuminate\Validation\ValidationException;
56
use Tests\TestCase;
67
use Illuminate\Foundation\Testing\DatabaseMigrations;
78

@@ -60,6 +61,22 @@ function test_it_creates_admin_user()
6061
$this->assertNotNull($user->uuid);
6162
}
6263

64+
function test_it_validates_input_for_creating_user()
65+
{
66+
$handler = $this->makeHandler();
67+
try{
68+
$handler->createAdminUser([
69+
'name' => 'Jose Fonseca',
70+
]);
71+
$this->fail('Validation to create user did not run.');
72+
} catch (ValidationException $e) {
73+
$this->assertDatabaseMissing('users', [
74+
'name' => 'Jose Fonseca',
75+
'email' => '[email protected]',
76+
]);
77+
}
78+
}
79+
6380
function test_it_assigns_admin_role_to_admin_user()
6481
{
6582
$handler = $this->makeHandler();

0 commit comments

Comments
 (0)