Laravel spatie/laravel-permission #can directive - laravel

I'm currently working with Spatie roles and permissions library.
I have multiple guards which are admin and web. My admin have permission to 'manage authentication' with guard_name = admin.
When I try to run the syntax below it return true.
current_user()->hasPermissionTo('manage authentication', 'admin')
But when I use #can directives the span tag not appear.
#can('manage authentication')
<span>Hello</span>
#endcan
I tried this, but still the span tag is not appearing.
#can('manage authentication', 'admin')
<span>Hello</span>
#endcan
Am I doing it wrongly?
Update:
I dont have any roles / permission related property and function inside my user model

#can not will work with multi guards
You can use this :
#if(auth('guard-name')->check() && auth('guard-name')->user()->can('permission-name'))
after that run :
php artisan cache:forget spatie.permission.cache
then :
php artisan cache:clear

Related

Laravel 8: error when routing a GET to any view and receiving a error "Trying to get property 'user_id' of non-object"

I've created a new Laravel 8 app with Jetstream and Livewire. When I make a GET request to any view an ErrorException is launched with message
Trying to get property 'user_id' of non-object
on navigation-dropdown.blade.php which is automatically generated by Jetstream\Livewire. I've looked for this user_id and didn't found on any part of the code.
Check that if user has team in the blade view.
resources/views/navigation-dropdown.blade.php
In line 61:
- #if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
+ #if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->current_team_id)
In line 162:
- #if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
+ #if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->current_team_id)
Fortify adds it's own auth routes, but old routes in config/auth.php will still be used (in my case)
Simple solution is to comment out all routes in config/auth.php (if using jetstream with fortify)
and run :
php artisan route:cache
this will make sure that auth routes use fortify actions, so each new account you create will have team created

Laravel Telescope is not working in production with Laravel Voyager. How to secure /telescope/* routes in Laravel 7?

I'm using Laravel with Auth, Auth UI, Voyager admin panel, Telescope. Everything works fine untill I change APP_ENV=local to APP_ENV=production in .env file. When I change the .env file to production then I get the below error. screen shot of my issue is here.
Trying to get property 'name' of non-object
Basically I want to protect /telescope/*routes in production. But before doing that I am getting this error.
I read the same issue in github similar issue github link. I think the answer is present in the link but im unable to digest it as im a newbie.
Any help to fix this, and protecting telescope routes in production is much appreciated. I tried to create a Policy and give read permissions to the user via policy. But somehow im not able to fix it.
ok: its too easy - the only thing you have to do is:
php artisan make:policy -m User UserPolicy
thats it.
You can pass the array of email into the gate method in the TelescopeServiceProvider.php
Gate::define('viewTelescope', function ($user) {
return in_array($user->email, [
'username#domain.com'
]);
});
This will block everyone except the mentioned email of logged-in user.

Laravel DataTables queries and Entrust

I am using Laravel 5.3 with Laravel Datatables and Entrust
On my index action I display a list of records in a datatable. Now I need to integrate entrust in to it. If the user is an admin they can see all leads. However, if the user is a normal user the can only see the records that belong to them.
I'm not sure how to approach this, do I put this query in an if statement checking for roles or is there a better way?
public function query()
{
$leads = Lead::query()
->select([
'leads.id as id',
'leads.parent_id as parent_id',
'statuses.name as status',
'leads.title as title',
'leads.first_name as first_name',
'leads.last_name as last_name',
'leads.opt_in as opt_in',
'leads.created_at as created_at',
'leads.user_id as user_id',
])
->leftJoin('statuses', 'leads.status_id', '=', 'statuses.id');
return $this->applyScopes($leads);
}
Too long I don't use Entrust, because Laravel has Policies. But at your problem, I saw that Entrust has method
Auth::user()->can('permission-name');
You must create permission and save into DB which create by run command
php artisan entrust:migration
After create permission, apply it to user, it may have Admin permission and Normal User permission
Please read more at: https://github.com/Zizaco/entrust
Hope I can help. If can use Entrust, let's research about Policies of Laravel, it's very good. Learn about Policies at https://mattstauffer.co/blog/acl-access-control-list-authorization-in-laravel-5-1#policies

Laravel JWTAuth Custom User Model

I'm developing web using Laravel and JWTAuth with default user model at
App\User
Now i'm changin the path of my models to
App\Models
App\Models\User
and i change the jwt config to this
'user' => 'App\Models\User',
but when i try to access my login page, it's show error like this
FatalErrorException in EloquentUserProvider.php line 126:
Class 'App\User' not found
what happening with this? i've changing the setting to App\Models but it's still load to App\ directory?
I re-serve the php artisan, and do composer dump-autoload
It's the same error.
I've resolved my error,
there's one config called auth.php that must updated too
Thanks

Laravel - The [login] attribute is required Sentry2 Auth

Any idea's why i get the following error
The [login] attribute is required - what does it mean?
when trying to login with Sentry 2 & Laravel.?
If you need to use anything different from login as attribute in your users table, email for example, first publish the Sentry config:
php artisan config:publish cartalyst/sentry
Then open file app\config\packages\cartalyst\sentry\config.php and edit:
'login_attribute' => 'email',

Resources