From 0b9dcfdc448af058ca9edee41bb8bdd7588d2e44 Mon Sep 17 00:00:00 2001 From: Pawel K <39682172+Pawel-IT@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:46:41 -0700 Subject: [PATCH 1/9] Show logged in user roles Before it was showing the first user role no matter which user was logged in which was confusing. --- app/Http/Controllers/ExamplesController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ExamplesController.php b/app/Http/Controllers/ExamplesController.php index 7c3d228..107353b 100644 --- a/app/Http/Controllers/ExamplesController.php +++ b/app/Http/Controllers/ExamplesController.php @@ -9,9 +9,8 @@ class ExamplesController extends Controller { public function show_my_roles() { -// $user = auth()->user(); -// or - $user = User::first(); + + $user = auth()->user(); $roles = $user->getRoleNames(); return var_export($roles, true); From 22d68520a8909a11e4bdc15e3eeaa4581b5c07e9 Mon Sep 17 00:00:00 2001 From: Pawel K <39682172+Pawel-IT@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:13:26 -0700 Subject: [PATCH 2/9] Fixed it so "member" with no roles can edit own post --- app/Policies/PostPolicy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Policies/PostPolicy.php b/app/Policies/PostPolicy.php index 60015ad..4748435 100644 --- a/app/Policies/PostPolicy.php +++ b/app/Policies/PostPolicy.php @@ -70,8 +70,8 @@ public function create(User $user) */ public function update(User $user, Post $post) { - if ($user->can('edit own posts')) { - return $user->id == $post->user_id; + if ($user->id == $post->user_id) { + return true; } if ($user->can('edit all posts')) { From d9851fa432d92e96960351e6704e0fd75076bb3c Mon Sep 17 00:00:00 2001 From: Pawel K <39682172+Pawel-IT@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:14:30 -0700 Subject: [PATCH 3/9] Made interface a bit more user friendly Added various notes about the role you're logged in with and what permissions you have/don't have. Also added all the links to the various routes. --- resources/views/permissions-demo.blade.php | 17 +++++++--- resources/views/posts/edit.blade.php | 2 ++ resources/views/posts/index.blade.php | 12 ++++++- resources/views/posts/show.blade.php | 11 ++++++- resources/views/welcome.blade.php | 38 ++++++++++++++++++++-- 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/resources/views/permissions-demo.blade.php b/resources/views/permissions-demo.blade.php index 9577137..b1dc8f9 100644 --- a/resources/views/permissions-demo.blade.php +++ b/resources/views/permissions-demo.blade.php @@ -1,11 +1,20 @@ -@hasrole('writer') -
You have been assigned the [writer] role.
+@hasrole('author') +You have been assigned the [author] role.
@else -You do NOT have the writer role.
+You do NOT have the author role.
@endhasrole -@can('edit articles') +@can('edit all posts')You have permission to [edit articles].
@elseSorry, you may NOT edit articles.
@endcan + + +If you didn't have the edit articles PERMISSION that the "Writer" ROLE provides you would get a 403 error instead of this page. Try that out by logging in as admin account which won't have this permission and will return a 403 error.
diff --git a/resources/views/posts/index.blade.php b/resources/views/posts/index.blade.php index 395c798..71fba58 100644 --- a/resources/views/posts/index.blade.php +++ b/resources/views/posts/index.blade.php @@ -1,3 +1,13 @@ +@can('edit all posts') +You have permission to [edit all posts]. Clicking Edit Post below will return a edit page
+@else +You do NOT have permission to [edit all posts]. Clicking edit below will return a 403 Error UNLESS you own the post
+@endcan + @foreach($posts as $p) -{{ $p->id }}. {{ $p->title }}
+{{ $p->id }}. {{ $p->title }} (Edit Post)
@endforeach + + diff --git a/resources/views/posts/show.blade.php b/resources/views/posts/show.blade.php index 6dcdee1..5aaa12c 100644 --- a/resources/views/posts/show.blade.php +++ b/resources/views/posts/show.blade.php @@ -1 +1,10 @@ -{{ $post->title }} +Title: {{ $post->title }}
+{{ $post->body }}
+ +@can('edit articles') + +@endcan + + diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 4352a54..6c70f3f 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -16,7 +16,7 @@ -You have been assigned the [admin] role.
+ @else +You do NOT have the admin role.
+ @endhasrole + @can('edit all posts') +You have permission to [edit all posts].
+ @else +Sorry, you may NOT edit [edit all posts].
+ @endcan + +User: admin@example.com
+Password: password
+User: author@example.com
+Password: password
User: member@example.com
+Password: password
++ View Demo +
You have permission to [edit all posts]. Clicking Edit Post below will return a edit page
@else -You do NOT have permission to [edit all posts]. Clicking edit below will return a 403 Error UNLESS you own the post
+You do NOT have permission to [edit all posts]. Clicking edit below will return a 403 Error UNLESS you own the post. For guests they will get a redirect to login page
@endcan @foreach($posts as $p) diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 6c70f3f..d4377d0 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -80,6 +80,10 @@ View Demo + +