Skip to content

Commit fd1b6d7

Browse files
authored
Assorted bits (#57)
* Rename and move layout file * Add ability to send error and warning notices * Fix Horizon gate not allowing super admins access * Add styling to select elements * Remove button component * Move form styling to classes * Install Pulse migrations * Update AccountControllerTest.php * Improvements to getting users in tests
1 parent 2f36060 commit fd1b6d7

File tree

24 files changed

+262
-129
lines changed

24 files changed

+262
-129
lines changed

app/Http/Controllers/AccountController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function update(AccountUpdate $request)
2020
$request->user()->update($request->only('first_name', 'last_name', 'email'));
2121
$request->user()->updatePassword($request->validated('password'));
2222

23-
\session()->flash('message', \__('account.updated'));
23+
\session()->flash('success', \__('account.updated'));
2424

2525
return \redirect()->back();
2626
}

app/Http/Controllers/EmailVerificationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function update(Request $request)
2323
{
2424
$request->user()->sendEmailVerificationNotification();
2525

26-
\session()->flash('message', \__('account.verification-resent'));
26+
\session()->flash('success', \__('account.verification-resent'));
2727

2828
return \back();
2929
}

app/Http/Controllers/OrganisationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function update(OrganisationUpdate $request)
1919
{
2020
$request->user()->organisation->update($request->only('name'));
2121

22-
\session()->flash('message', \__('organisation.updated'));
22+
\session()->flash('success', \__('organisation.updated'));
2323

2424
return \back();
2525
}

app/Http/Controllers/ResetPasswordController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function store(ResetPasswordStore $request)
2828
]);
2929
}
3030

31-
\session()->flash('message', \__('passwords.sent'));
31+
\session()->flash('success', \__('passwords.sent'));
3232

3333
return \redirect()->route('login');
3434
}
@@ -59,7 +59,7 @@ public function update(ResetPasswordUpdate $request)
5959
]);
6060
}
6161

62-
\session()->flash('message', \__('passwords.reset'));
62+
\session()->flash('success', \__('passwords.reset'));
6363

6464
return \redirect()->route('login');
6565
}

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88

99
class HandleInertiaRequests extends Middleware
1010
{
11+
protected $rootView = 'layouts/inertia';
12+
1113
public function share(Request $request): array
1214
{
1315
return \array_merge(parent::share($request), [
1416
'auth' => [
1517
'loggedIn' => \auth()->check(),
1618
'user' => $request->user() ? UserResource::make($request->user()) : [],
1719
],
18-
'message' => $request->session()->get('message'),
20+
'success' => $request->session()->get('success'),
21+
'error' => $request->session()->get('error'),
22+
'warning' => $request->session()->get('warning'),
1923
]);
2024
}
2125
}

app/Providers/HorizonServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
use App\Enums\Role;
6+
use App\Models\User;
57
use Illuminate\Support\Facades\Gate;
68
use Laravel\Horizon\Horizon;
79
use Laravel\Horizon\HorizonApplicationServiceProvider;
@@ -27,10 +29,8 @@ public function boot(): void
2729
*/
2830
protected function gate(): void
2931
{
30-
Gate::define('viewHorizon', function ($user) {
31-
return in_array($user->email, [
32-
'seb@sebkay.com',
33-
]);
32+
Gate::define('viewHorizon', function (User $user) {
33+
return $user->hasRole(Role::SUPER_ADMIN->value);
3434
});
3535
}
3636
}

database/factories/UserFactory.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@ public function unverified()
3131
]);
3232
}
3333

34-
public function admin()
34+
public function superAdmin(?string $email = null)
3535
{
36-
return $this->afterCreating(function (User $user) {
37-
$user->assignRole(Role::ADMIN->value);
38-
});
36+
return $this
37+
->state(fn (array $attributes) => [
38+
'email' => $email ?: \env('SEED_SUPER_ADMIN_EMAIL'),
39+
])
40+
->afterCreating(function (User $user) {
41+
$user->assignRole(Role::SUPER_ADMIN->value);
42+
});
3943
}
4044

41-
public function user()
45+
public function admin(?string $email = null)
4246
{
43-
return $this->afterCreating(function (User $user) {
44-
$user->assignRole(Role::USER->value);
45-
});
47+
return $this
48+
->state(fn (array $attributes) => [
49+
'email' => $email ?: \env('SEED_ADMIN_EMAIL'),
50+
])
51+
->afterCreating(function (User $user) {
52+
$user->assignRole(Role::ADMIN->value);
53+
});
4654
}
4755
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Laravel\Pulse\Support\PulseMigration;
6+
7+
return new class extends PulseMigration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
if (! $this->shouldRun()) {
15+
return;
16+
}
17+
18+
Schema::create('pulse_values', function (Blueprint $table) {
19+
$table->id();
20+
$table->unsignedInteger('timestamp');
21+
$table->string('type');
22+
$table->mediumText('key');
23+
match ($this->driver()) {
24+
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
25+
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
26+
'sqlite' => $table->string('key_hash'),
27+
};
28+
$table->mediumText('value');
29+
30+
$table->index('timestamp'); // For trimming...
31+
$table->index('type'); // For fast lookups and purging...
32+
$table->unique(['type', 'key_hash']); // For data integrity and upserts...
33+
});
34+
35+
Schema::create('pulse_entries', function (Blueprint $table) {
36+
$table->id();
37+
$table->unsignedInteger('timestamp');
38+
$table->string('type');
39+
$table->mediumText('key');
40+
match ($this->driver()) {
41+
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
42+
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
43+
'sqlite' => $table->string('key_hash'),
44+
};
45+
$table->bigInteger('value')->nullable();
46+
47+
$table->index('timestamp'); // For trimming...
48+
$table->index('type'); // For purging...
49+
$table->index('key_hash'); // For mapping...
50+
$table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries...
51+
});
52+
53+
Schema::create('pulse_aggregates', function (Blueprint $table) {
54+
$table->id();
55+
$table->unsignedInteger('bucket');
56+
$table->unsignedMediumInteger('period');
57+
$table->string('type');
58+
$table->mediumText('key');
59+
match ($this->driver()) {
60+
'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'),
61+
'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'),
62+
'sqlite' => $table->string('key_hash'),
63+
};
64+
$table->string('aggregate');
65+
$table->decimal('value', 20, 2);
66+
$table->unsignedInteger('count')->nullable();
67+
68+
$table->unique(['bucket', 'period', 'type', 'aggregate', 'key_hash']); // Force "on duplicate update"...
69+
$table->index(['period', 'bucket']); // For trimming...
70+
$table->index('type'); // For purging...
71+
$table->index(['period', 'type', 'aggregate', 'bucket']); // For aggregate queries...
72+
});
73+
}
74+
75+
/**
76+
* Reverse the migrations.
77+
*/
78+
public function down(): void
79+
{
80+
Schema::dropIfExists('pulse_values');
81+
Schema::dropIfExists('pulse_entries');
82+
Schema::dropIfExists('pulse_aggregates');
83+
}
84+
};

database/seeders/UsersSeeder.php

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

33
namespace Database\Seeders;
44

5-
use App\Enums\Role;
65
use App\Models\User;
76
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
87
use Illuminate\Database\Seeder;
@@ -13,20 +12,7 @@ class UsersSeeder extends Seeder
1312

1413
public function run()
1514
{
16-
User::factory()
17-
->afterCreating(function (User $user) {
18-
$user->assignRole(Role::SUPER_ADMIN->value, Role::ADMIN->value);
19-
})
20-
->create([
21-
'email' => \env('SEED_SUPER_ADMIN_EMAIL'),
22-
]);
23-
24-
User::factory()
25-
->afterCreating(function (User $user) {
26-
$user->assignRole(Role::ADMIN->value);
27-
})
28-
->create([
29-
'email' => \env('SEED_ADMIN_EMAIL'),
30-
]);
15+
User::factory()->superAdmin()->create();
16+
User::factory()->admin()->create();
3117
}
3218
}

resources/js/Components/Button.vue

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)