hello and welcome to bitfumes and we are on laravel permission tutorial so
let's continue and in this episode we are going to see how we can restrict a
user by using the rule or the permission user have so let's see for saving the
time and for better understanding I have seeded some post so if I go to sequel
Pro on the database I have posts table 10 post are here just having the title
in body nothing else and you have seen here that we can edit a post so likewise
this one is already edited so if I remove this edited part submit it yeah
it is updated and similarly we can create a new a simple crud operations in
the vs code if I can see yeah you can have all these crud related routes it's
just a crud related routes nothing new here and for again to save time I have
four different users sarthak John gene and Alex and each one has the name at
which films.com add in it simple again I have created
four different roles so to have the better understanding of how these things
are there so yeah I have permissions also right post edit post so how these
things are connected let me show you by having this presentation okay so as I
have told you I have roles here which is writer editor publisher admin I have
permissions here write post edit post publish post then I have users san
thirteen John and Alex so because alex is a admin he can do
anything write post add post published post
anything but gene is just a publisher so she can only publish the post likewise
John is just a editor so he can edit the post and sarthak is a writer so just can
write post so if you are getting these yeah these relationships are already
there on our database that means on sequel Pro you
can see role has permission so one is related to one the role of writer is
connected to the permission one which is a right post so I think you get the idea
because we know these chart okay so how we can connect it again you can see
these things are connected so how we can move on with this episode so as you can
see I am not logged in so let's log in here and I'm going to log in as let's
say John so John add bit films.com and password
is secret so I am here let's move to post yeah so because John is editor so
he can edit it yeah that's fine but he cannot create a new post because you can
see here John is the editor he is not a writer he cannot write so how we can
restrict this so on the permission you can see we have a role system so if we
want to restrict something or we want to show or hide something we can use this
blade syntax super easy let's go to vs code and open the blade part that means
the post index where we are showing all the post yeah yeah here we can see we
have yeah this one the new okay so what I'm going to say act role I just want to
show this when the logged in user is having a role of writer so let's end
this and roll simple so let's just indent it so if I go here refresh this
page yeah you can see it's gone the new link is not there so this means John
cannot see but we have we have said here that okay if they write if the role is
writer then show that good so we know writer is SATA is writer so Santa has
idea of one so when I login lsat logout and and South ahead bit films.com that
has to be like this and yeah I am logged it so why not redirected to a director
post page so login controller and redirect to post yeah this is good okay
so let's go to host and yeah writer can see new part but again writer can not
see this edit part so how we can do that again as we have done here we can use it
for the Edit part so if the rule is editor then only I am going to say yeah
show this edit part or edit button refresh yeah you can see edit and the
edit button is gone so similarly if I cou login from here again as John John
can see edit but not the new and the reverse for sarthak what if for let's
say gene we have so that's a gene gene cannot see anything gene can just
publish it so maybe if we have publish button we can show that but what about
the Alex Alex is the admin so what I am going to do I'm going to log in F Alex
okay Alexis Edmund he can see the buttons the edit and new he he can
easily because he is a admin so how we can convert it so that even the admin
can see and we want to have this part also that means we want to have the
editor and the admin also so in that case you can see here if we are getting
this yeah so you can just write one or you can check for any role so just like
this writer and admin so we just need to use the pipe sign
to separate this so either one is good so maybe if the logged in user have
editor oh he's admin yeah he can show this he
can see this or not you okay so refresh and yeah alex is an admin he can see
these things super cool and likewise you can have a you can check for has all
roads yeah I think you get the better idea how you can use these things but
you know the problem is let's login with salsa so let's log in as Sal Turk what I
want to do is that because Santa is a writer so maybe we want the satyr to
edit post also in den fee maybe when when satiric is writing
something he missed something or he he won't edit something so maybe he can
edit also so this is not possible because the writer can only write post
we need to give a separate permission for Sultan to edit post hmm this seems
good so now what I am going to do I am going to add a permission I'm going to
add a permission to Saturn so permission of edit post we want to apply for Santa
which is having a user ID of one so how we can add yeah we can create a UI
system where we can add things but for now I'm going to add it here directly on
the database so permission ID discovered it permission ID is two and here model
type is app user and and that has to be small P oops that has to be double P and
model ID is one so now our user photic can have permission to edit post but if
you to see this and you can see it's still
not working why is not working we have done correct things so the problem here
is first which is we are checking for role instead of checking for role here
on the Edit part we can check for permission and to check for permission
we just need to use can part like can bleed syntax it's just just a native
system of native blade syntax of level so we can use hair instead of role so
I'm going to make it Ken and here I can say right not right edit post good so
let's now go here and refresh so yes because samantha is now have the
permission to edit the post he can easily edit it let's verify on the John
part because John is a simple editor yeah and Shawn is editor not a writer so
he cannot see the new part so this is very cool and now let's check for Alex
because alex is the admin and we have made some changes here so we need to
check it so Alex and like hmmm yeah it is working for LX also why because on
the role has permission you can see the fourth one is assigned to every role so
forth one is Alex which is admin and admin have all the rules that means the
role of writer editor admin and publisher so I think we have
four one two three oh yeah permission sorry he not the rules so having the
admin have the permission for everything so role has permission yeah that's why
it's working fine so this is how you can use these bleed syntax to hide or show
something let me do let me show you one problem with this kind of system so I'm
going to login as John
so John cannot create any post but let me do one thing so if I go to new tab
and I will say post slash create and hit enter
hmm you can see the problem yes John cannot see the new button
but he can directly go to the page to create a post this is not we want so now
we need to use the middle verse defined in the permission package and that we
will do in the next episode so if you have liked these episodes please go and
share with your friends and if you have not subscribed to this channel go and
subscribe to this channel for more level related updates and like betras on
Facebook Twitter and Instagram we will meet in the next episode till then good
bye