Laravel Super Admin: 2 Ways to Override Roles and Permissions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys if you use roles and permissions in your laral project with or without any package what if you want to Define some kind of super admin or so-called god mode or whatever you call it to overwrite any roles and permissions so there's rols and permission system and then someone else logs in with super admin who has access to everything all the current function and the functions in the future I will show you two ways in this short video first was written on laral daily quite a while ago in 2022 so if you use Gates Define the gates of updating post for example this comes from laral documentation it used to be in O service provider now in larl 11 it's most likely in the app service provider because there's no off service provider by default but you can still create that manually if you wish it's a personal preference anyway so if you define Gates like this one you can overwrite any gate with this syntax gate gate before which has two parameters of current user and ability which is gate name and no matter the ability for example you can do something like this which would overwrite all the abilities with current user by some condition like Ro ID or something or for example another case if you use like spoty media library or any package or any method about permissions or roles the same thing just return true with gate before so that's one way and another way I saw while reviewing the project genealogy which I will mention a few more times in upcoming videos on this channel so while browsing around I saw this variable god mode true in EnV so this is an interesting use case not sure I would use that but I want to show you as one of the options so if your EnV contains some kind of variable for example Allowing God mode on local environment in EnV on your machine but it is false on any other server by default for example then you can check god mode in any parts of your application like this in this case it's adding or removing Global scope for user is developer and the same condition is in the blade as well and in other model and in EnV example so you can Define that overwriting super admin per environment again I'm not sure about the implementation here but I like the idea that you can enable super admin for your local or staging server or something like that the only thing I would change here it's kind of a bad practice to use EnV function EnV helper directly in the code in the blade or in the model instead of that so here I open the project the same model instead of EnV you should use config values for example you need to Define in config app or in config whatever Define the variable with EnV as the default place where to take the data from then default value fallback value defined in the config this is done for all the other values in config app this is kind of a default standard practice so you just add your own one more variable and then in the code whenever you need that instead of doing this you do config app god mode like this and then you don't need the default because it is defined already in the config and it's not only shorter it also allows to Cache the config if use EnV directly in the code and run PHP Artisan config cache the EnV values will not be cached but whatever uses the config would be cached successfully so yeah kind of a two-in-one tip on how to override the gates with super admin or good mode have you implemented something like that in your projects maybe in some other way let's discuss in the comments below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 4,068
Rating: undefined out of 5
Keywords:
Id: LaulI5GTJek
Channel Id: undefined
Length: 3min 56sec (236 seconds)
Published: Thu Apr 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.