Finally, we get to the actual point of this small application: Task management.
Compared to the Task Model in previous lessons of this course, we added a few more fields: assigned_to_user_id (clinic doctor/staff) and patient_id:
Tasks Migration:
$table->foreignId('assigned_to_user_id')->constrained('users');$table->foreignId('patient_id')->constrained('users');
Then, I added them to the Model, too:
app/Models/Task.php
use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;use Illuminate\Database\Eloquent\Factories\HasFactory; class Task extends Model{ use HasFactory; protected $fillable = [ 'name', 'due_date', 'assigned_to_user_id', 'patient_id', 'team_id', ]; public function assignee(): BelongsTo { return $this->belongsTo(User::class, 'assigned_to_user_id'); } public function patient(): BelongsTo { return $this->belongsTo(User::class, 'patient_id'); }}
Then, we also changed the Factory with the new columns in mind.
database/factories/TaskFactory.php
use App\Models\User;use Illuminate\Database\Eloquent\Factories\Factory; class TaskFactory extends Factory{ public function definition(): array { $randomAssignee = collect([ User::factory()->doctor(), User::factory()->staff(), ])->random(); return [ 'name' => fake()->text(30), 'due_date' => now()->addDays(rand(1, 100)), 'assigned_to_user_id' => $randomAssignee, 'patient_id' => User::factory()->patient(), ]; }}
Now, who can manage tasks? Traditionally, let's start with Policy:
app/Policies/TaskPolicy.php
use App\Enums\Role;use App\Models\Task;use App\Models\User;use App\Enums\Permission; class TaskPolicy{ public function viewAny(User $user): bool { return $user->hasPermissionTo(Permission::LIST_TASK); } public function create(User $user): bool { return $user->hasPermissionTo(Permission::CREATE_TASK); } public function update(User $user, Task $task): bool { return $user->hasPermissionTo(Permission::EDIT_TASK); } public function delete(User $user, Task $task): bool { return $user->hasPermissionTo(Permission::DELETE_TASK); }}
You don't see the filter by team here, right? The approach we took here is to filter them on the Eloquent level, with global scope.
In fact, it's a 2-in-1 scope...
When I run the test command on ssh, I keep getting the same errors.
I'm not using sqlite, I'm using mysql.
First - why are you running this on SSH? Live servers should never have tests running in them :)
Secondly, there seems to be some things missing. For example - the
is_admincolumn. This could say that there's an issue with migrations (they either never ran or there's a missing one). As for the route not found - double check that the route was defined in your routes list by usingphp artisan route:listI'm seeing an issue when trying to edit a task. auth()->user() seems to have dropped the roles/permissions in addGlobalScope. The line
if (auth()->user()->hasRole(Role::Patient)) {is not working as expected - there are no roles or permissions attached to auth()->user() so the result is unauthorized.The tests don't see this. It thinks it is OK.
It fails even you dummy the line like:
$dummy = auth()->user()->hasRole(Role::Patient);orif ( ! auth()->user()->hasRole(Role::Patient)) {What I think may need to happen is to set the session with
setPermissionsTeamId:Alternatively, I found that the middleware in chapter 10 needs to refresh the cache - app/Http/Middleware/TeamsPermissionMiddleware.php:
poss doing both, since the middleware may not be run in certain situations.
Hi, do you also find it unusual that roles and permissions are being lost without a clear reason? I’m facing the same issue and feeling a bit confused about what the standard solution should be. Ideally, we should be able to trace the root cause properly to prevent this from happening again.
Hi Team,
I’ve noticed that roles and permissions are being lost without a clear reason, and I find this quite unusual. I’m currently facing the same issue and feeling a bit unsure about what the standard solution should be in such cases.
Ideally, we should be able to trace the root cause properly to prevent this from recurring. Would it be possible for someone to take a look and help identify the underlying reason?
Thanks in advance for your support.
One solution which I found is refresh the roles and permission, public function update(User $user, Task $task): bool { $freshUser = User::with('roles.permissions')->find($user->id); return $freshUser->hasPermissionTo(Permission::EDIT_TASK); }
and It is not ideal to load permission again and again, so kindly assist and guide.