YouTubevideo· Jun 15, 2024· 4:02· 1,800 views on YouTube
React at a moment you like. The best moments light up here.
So, I've kind of been on an authorization kick recently. I released a Laravel official video on authorization, what it actually is, and then just recently I uploaded a is Livewire secure video where I dived in a little bit more into what Laravel Livewire has in the sense of security and and features that you might utilize.
And a Tix MT in a comment talked about global scopes on models, and this is something that I actually didn't know too much about. I had heard of them, but I hadn't actually used them. Because I learned something, I want to share it with you as well. So, let's go ahead and take a look at our application. I have a just basic to-do list that has a bunch of different users who have their own to-dos. Now, on this main view here, I'm not actually scoping that with an a eloquent query. I'm just calling to do all. You can see that here. To-dos, to do all.
Now, if I was to take a look at this list, of course I can't delete them because that's unauthorized. As I do have a a policy to be able to say, "Hey, um I can only view a particular user's to-dos um if they're my to-dos." And then the user can only delete their own to-dos as well. So, if I was to go back to all to-dos, I can't actually view this one cuz it's unauthorized.
So, how can we make that so we don't actually have to add the eloquent query here because for for some reason, maybe in our to-do manager Livewire component, we're saying uh I forgot to add the auth user to-dos get. So, I'm just grabbing my own or in this case the user's to-dos. Well, there's actually a way to do that. You might think, "Okay, the to-do policy has that because it has a view any." And that's not actually the case. This is just to view everything on a list of models.
This would be saying, "Okay, an admin, if I want to authorize the use case of seeing everything, an admin can do that."
That'd be a perfect use case for view any policy. But, it doesn't necessarily give us a scoped condition to only view the users' to-do's. So, how can we do that? Well, here's what we're going to do. We can actually create a global scope within Laravel to say within the to-do's, I only want to ever show the users' to-do's. So, we're going to call PHP Artisan make scope, and we're just going to call this user to-do's.
Now, in our user to-do's scope, what we're going to call is the supply function. We're just going to say this is going to first check if someone is authenticated, and then this builder says where the user ID is associated with the off ID. Now, this is just a simple scope to say I want to apply this to the user, but now we actually have to add this to our to-do model.
So, in our to-do model, we can now create a new protected static function. We say we want to boot everything that this model is already associated with the parent boot, but now we can add the static global scope.
And this is basically saying anytime we're calling to-do's, we want to make sure it's only the to-do's associated with the user. Now, of course, you might have to override this and create a new scope if you're wanting to show all to-do's for a team or something like that. But, in this particular case where we just have users, this would be perfect for us. And now, if we were to go back into our to-do manager, we still have Let's go ahead and bring this back to to-do all and but in our actual application, we're still only going to see the users' to-do's.
And we can see that in actually um the first off the the uh query here where we say we're selecting the users, and then the to-do's where the user ID equals three. That's something that has been set because of the scope that we created. And we can of course have all the functionality that we would normally.