Spatie Laravel Permission Package Tutorial | User Role and Permission #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome to the laravel permission tutorial and in the last episode we have seen how we can set up this package and use to create the roles and the permissions and obviously the relation between them but in the last episode we have done one thing more which is we have used this has rolled tray 2 on our user model this means the rules and the permissions we have created now can be assigned to the user so let's see how we can do all these things because of this has role crate we can do these things so again let's see I have so PA s for starting the service again aleeah's I have created so it's now started so we'll go to localhost localhost and 8000 so let's now register a new user because I have flushed out the database and now I have this so here if I go to my vs code you can see here on the index function where I have home controller if I return hello and check this on Chrome yeah I have the hell of this means this is the correct file so let's create a rule we have not created rule so in the last episode I have created but I have as I told you fleshed out the database so I need to create one more so create and giving the name as again the writer as in the previous similarly let's create a permission and permission create again the permission name is edit post this is not related to this role so let's go here on Chrome refresh the page no error go to sequel Pro now you can see on the roles table we have right on the permissions table we have edit post but we don't have any relation and we are not going to do that for now we will do later but I want to have something in these two models so model has permission so which model we are going to use obviously the user model so how we can use that so for that you can go to the documentation if you scroll it down or even if you scroll at the top you can see we have the user user give permission user assign rule so simple thing let's go and see how we can do all these things so here we have because I have already created the roles and permissions so I'm going to comment these two and because I am on I am logged in I can say auth user which is the default logged in user so I want to give permission to like here edit post you have seen as I see as I type so user give permission to edit post if I go on the chrome refresh the page no error sequel pro this time on the model has permission table has one entry and you can see it is now connected with this arrow which says it is connected to permissions which is edit post so this is very nice but we can also attach a role okay so we have seen to attach a role to a user we need to say assign rule so I will comment this out because again I have done that so auth user assign role like this and the role is writer simple enough okay so let's once more go to chrome refresh the page so okay so we have to give the assigned okay and refresh no error now comes here and refresh this has model rules or actually model has rules table refresh it yeah again we have and this is the rule which is writer so now these two tables are filled this means now user has some role user has some permission but how we can check the user has something or not okay so if you scroll down on the documentation you can see we have something here yeah we have the permission so user permission so let's check this out so now I will say not user and let's move it like this and permissions let's see how it's going to look and go here on the chrome let's refresh and yeah I have one permission edit post this is obviously because we have from one permission but you know this rule doesn't have any permission because the role commission don't have anything so why not add another why not add another permission to role so what I'm going to do I'm going to create a permission and this permission is like write post because writer can write post then I will say permission is equal to this permission and then I will say the role which is like role is equal to role and defined by ID so find by ID and in the last episode we know how this is going to work so this rule I'm going to assign a permission so give permission to and it's like permission and that has to be inside this permission okay so now if I go here on the chrome refresh this page no error on the sequel Pro roll has permission having one entry now what that mean is that this role of writer is connected to permission so any user has this rule automatically get the permission to write the post so you can see now we have two kind of permission one permission is write post which is wire rule one permission is edit post which is not connected to any role this means because the user has the role of writer he automatically get the permission to write post they are getting it because the user has the role of writer and writer is connected to the right post permission automatically get the right post permission but we have also said that the user our user has the permission to edit post so what if I try to get the permissions for the user so let's comment this out and I'm going to get the permissions so last time we only get one but now Oh still we are getting one what happened we have two permissions one is write post wire role and edit post which is direct so here on the documentation you can get things like get direct permission so let's see what will be the result of this get direct permission okay so if i refresh edit post with direct permission this permission is not related to any rule or not assigned wire rule maybe this can be attached to a role and that role is not assigned to the user but this is direct but permissions wire role how it is going to work this is very very important now if I run this yeah you can see now we are getting a right post instead of edit this is because we have said that okay get me the permission to wire rule but if I say all permissions like this what it will give it will give both of the permissions so this is very important to have concept of permission directly or permission wire rule so in that way you can have the permission and you can use these permission to have some kind of access level on your project so you can also get some users so if you don't know who is having the role of writer so you can simply say return user and I will say get me the user but having the role of writer and like this so get me the one like this so if I say refresh yeah this is the one which is having all these role of writer similarly it's very easy to get permission also and we know right WRI te post this is the permission so again we are getting that same result because we know that we have only single user and this is connected to permission and role both so let's just see one more thing and as usual you can assign multiple permissions like this and I have told you this in the last episode also but it's not so important here the important part is revoke permission and remove role also you can remove the rule or you can revoke the permission so if you want to remove the permission you need to say revoke permission so let's say I will going to get permission and then revoke permission okay and the permission name is right post and let's say edit post because it's it's a direct post and we are revoking it so if we want to revoke the permission why our role we need to remove the roll itself so refresh and it says permission okay so it's actually revoked permission to sorry about that so refresh and yeah there this means if I go to sequel Pro and refresh this model yeah you can see model has permission doesn't have any value so in that way you can also remove the role so you need to say remove remove role and role of writer let's see it's going to work or not okay so remove role is not there so maybe we can find another one assigned role remove rule it's correct what's the problem remove role are e okay so remove role hand yet no error this means model has roles is also no data so I have C I have explained you how you can assign rule how you can remove role and most important the difference between the direct permission and permission wire role so in the next episode we will going to see how we can use the permission to have some kind of access levels on your project so if you have not subscribed to this channel qu and subscribe to bit fumes and don't forgot to hit the bell icon to get instant notification like bit fumes on Facebook Twitter and Instagram we will meet in the next episode till then good bye
Info
Channel: Bitfumes
Views: 80,254
Rating: undefined out of 5
Keywords: laravel permission, spatie laravel-permission tutorial, spatie/laravel-permission, spatie/laravel-permission laravel 5.5, laravel permissions spatie, laravel permissions, laravel permissions roles, laravel roles and permissions 5.6, laravel roles and permissions, laravel acl roles and permissions, laravel acl tutorial, laravel acl, laravel user roles and permissions, laravel user roles, laravel user roles and permissions tutorial, laravel user permissions
Id: nx6eGEKnkwU
Channel Id: undefined
Length: 12min 40sec (760 seconds)
Published: Mon Oct 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.