Updating User Permissions0:00
In the previous episode, we implemented permissions, but we didn't implement anything as far as managing those permissions. Now, off-screen, I've taken the liberty of, well, implementing the permission management because, well, it was repetitive. I basically just copied what I did with the roles and renamed it to permissions, and that's it. It's basically the same, and I'm sure you didn't want to see me do it because I sure didn't want to see me do it, but there is a distinct difference, especially when it comes to updating the User. Because in the form, we of course have the username and then we have the permissions,
comes to updating the User. Because in the form, we of course have the username and then we have the permissions, but we store these permissions differently now. With roles, we had a pivot table. With our permissions, we just have a single column in the user table. So when it comes to updating the User, what we had before really isn't going to work. So what we will need to do is, first of all, get the permissions that we want to assign to the User, and we can call the whereIn method where the ID is in the permissions that we get from the request, and then we will need to update the users. So that's, we will specify the name coming from the request name, but then we will update
that we get from the request, and then we will need to update the users. So that's, we will specify the name coming from the request name, but then we will update the permissions with the permissions that we retrieve from the database, but that's not going to be good enough because in that database column, we have a JSON array, and it simply has the names of the auth code, and that's it. There's no other information about those permissions. So we want to pluck the auth code, and that's going to be sufficient so that whenever we save a user, now we will save the permissions that are assigned to that user. And we can easily test that. Let's just take our author.
And we can easily test that. Let's just take our Author. Let's say that we want them to be able to create a User. We save it, we go back, and it's there, but no, that's a horrible decision. We will take that permission away so that the Author can only do author things. But when it comes to the permissions, well, everything is in place. We can create, we can edit, and we can delete. But the same logic applies that we use with the roles. The auth code is read-only, we cannot change that after it is created, and well, everything else was just like the roles.
Creating Group Model2:20
The auth code is read-only, we cannot change that after it is created, and well, everything else was just like the roles. So there we go. Now we need to implement our Group. And to do that, we need a model for our Group. So let's make a new model. And I'm going to just call this Group because, well, that's a lot easier to type. Migration Group would probably be a better term, but that's a lot of keystrokes, and I'm just wanting something simple. So we will create our Group.
Building Group-Permission Migrations2:50
I'm just wanting something simple. So we will create our Group. We want the migration there, but we also want another migration because we are going to use a pivot table in this case so that we can have many Permissions belong to many Groups and vice versa. So we need a pivot table here. So our migration will be create_group_permission_table. And let's start with that migration because this is something that we have done before. We just need two foreign keys here. So the first will be the group_id, which will be constrained.
We just need two foreign keys here. So the first will be the group_id, which will be constrained. Let's go ahead and let's have the onDelete set to cascade. Then we just need essentially the same thing, but for the permission_id. So we will have that other column, and that's going to be it for this table. It's a pivot table. It's very simple. So we can close that. But let's open up the group migration because we need to define this table, and I think we can just get away with having a name.
But let's open up the group_migration because we need to define this table, and I think we can just get away with having a name. Now you could make the argument that we need some kind of unique identifier, kind of like the auth code. And for a very large system, I'd say, yeah, let's do that, but let's just keep it simple in this case. We'll stick with the name and that's going to be good enough for us. So with those migrations done, before I forget to do that, let's run the migrations and our database will be updated. So we're good there.
Defining Model Relationships4:18
database will be updated. So we're good there. Let's open up the Group model and the Permission model because we need to make some changes here. As far as our Permission is concerned, we didn't do the fillable array. So let's do that. Sometimes I like autocomplete. Sometimes I don't. In this case, we have the what? We have the auth_code and the description.
In this case, we have the what? We have the auth code and the description. So that's going to be good for the permission. But we need to go ahead and set up the relationship here so that we can get the groups. And this is a belongsToMany relationship so that we will simply return this belongsToMany group. That's essentially what we need to do inside of the Group model. So I'm just going to copy and paste. We don't have a description. We don't have an auth code, but we can change that to a name.
Adding Group Routes and Navigation5:04
We don't have a description. We don't have an auth code, but we can change that to a name. Then our method name will be simply permissions, which is a belongsToMany. And then the class will be our Permission model. So we are good to go as far as our models are concerned. And let's open up the navigation view. Let's also open up our routes because I have a confession. I've implemented the group management, at least most of it. There's still some things that we need to implement. But again, it's almost identical to the role management.
There's still some things that we need to implement. But again, it's almost identical to the role management. And I didn't want to see me type that. I'm sure you... I'm getting violent with the microphone, and I'm sure you didn't want to see me type all of that out. So we have our routes, which is going to be for our groups. And that is going to be for the GroupsController. And yeah, that's going to be just fine as far as our routes are concerned. So when it comes to our navigational links, we just need to add another link here.
And yeah, that's going to be just fine as far as our routes are concerned. So when it comes to our navigational links, we just need to add another link here. But for our groups, the routes will be the groups.index, and we want that active if the route is groups.index. So inside of the browser, we see our groups link. We have our index here. Says roles. And you know what? I'm going to change that because that's going to annoy me. It probably annoys you.
I'm going to change that because that's going to annoy me. It probably annoys you. So that was inside of index. We have groups. Where does it say roles? Right there. Groups. Okay. So we're good there. Now we can go to the create view, and we are going to see that it gives us the option for
So we're good there. Now we can go to the create view, and we are going to see that it gives us the option for the name, but we need to be able to assign the permissions that we want that group to have. So let's open up the create view. And really, let's open up the edit view for the users because we can take this select box, and we can use it inside of the create view for our groups. We do need to make a couple of changes though because this right here, we don't really care about if a group has a permission because we are creating in this case. There is no permissions assigned.
about if a group has a permission because we are creating in this case. There is no permissions assigned. So we just want a straight up select box so that we can choose the permissions. Everything else should be okay. Let's go ahead and open up the edit view for our, not our permissions, but for our groups because I'm going to paste in that same thing. But this is where we do need to be concerned here because we will have permissions for this group. And we could implement this hasPermission method on the Group model. I don't necessarily want to do that.
And we could implement this hasPermission method on the Group model. I don't necessarily want to do that. Instead, what we could do is something like this to where we will have our group. And then we will use permissions, contains, and then the provided permission. That's going to get us exactly what we need. And yeah, that's going to work just fine. So now back inside of the browser, we can see that we can create our group. We have the permissions that we would need to select. The only thing that we need to do now is implement that functionality. So let's open up the GroupsController.
Syncing Group Permissions8:16
The only thing that we need to do now is implement that functionality. So let's open up the GroupsController. And let's see right here inside of store. Now I have this so that it's already creating this, but let's not do that. Instead, we will set the name to input name, but we're going to save this. And I'm sure that there's a better way to do this. This is just how I'm going to do this so that we will create that group so that then we can take the permissions. We don't want to attach. We want to sync the permissions that we get from the request and that will assign the
We don't want to attach. We want to sync the permissions that we get from the request and that will assign the permissions there. And I already have that implemented for update. So we are updating the group name, then we sync the permissions, and everything should work as the roles and everything else. So back in the browser, let's create a new group. This will be article authors. And we want those authors to be able to create, to update, and delete so that whenever we save this, we have our group.
And we want those authors to be able to create, to update, and delete so that whenever we save this, we have our group. We can edit that and we will, well, there we go. We see that those permissions are assigned. But then we decide, okay, articles can create, they can update any article, and delete any article. If we update that and view again, we can see that we are properly setting those permissions. So now we can manage users. We can create and manage permissions and assign those permissions to users. We can create groups and we can assign permissions to those groups.
We can create and manage permissions and assign those permissions to users. We can create groups and we can assign permissions to those groups. Now we just need to be able to assign users to groups. It sounds very complicated, and it kind of is, but it's going to be worth it. So in the next episode, we will implement that functionality and start to see how all of this is going to work together.
